iOS 10消息扩展 - 在Simulator

时间:2016-10-02 03:07:41

标签: ios swift swift3 ios-messages-extension

我正在尝试实现didRecieve()来触发iOS 10扩展中的故事板segue。使用故事板,我可以在使用以下代码创建消息后触发segue:

    @IBAction func createThing(_ sender: AnyObject) {
    self.createMessage()
}

func composeMessage(customMessage: String, caption: String) -> MSMessage {
    var components = URLComponents()

    // create a query item from our custom message
    let item = URLQueryItem(name: "New Thing", value: customMessage)

    // put it into an array of query items
    var items = [URLQueryItem]()
    items.append(item)
    components.queryItems = items

    // tell messages to use the default message template layout
    let layout = MSMessageTemplateLayout()
    layout.caption = caption

    // create a message and tell it the content and layout
    let message = MSMessage()
    message.url = components.url!
    message.layout = layout

    // return it for sending
    return message
}

override func didReceive(_ message: MSMessage, conversation: MSConversation) {

    print("message recieved!!")
    self.pollRecieved = true

    guard let messageURL = message.url else { return }
    guard let urlComponents = NSURLComponents(url: messageURL, resolvingAgainstBaseURL: false), let queryItems = urlComponents.queryItems else { return }

    print("URL Components", urlComponents)
    print("queryItems", queryItems)

    for item in queryItems {
        print("Received \(item.name) with value \(item.value)")
    }

    print("attempting segue!")
    self.performSegue(withIdentifier: "showPollSegue", sender: self)
}

func createMessage() {
    let message = composeMessage(customMessage: "This is really important.", caption: "User-facing caption here")
    self.activeConversation?.insert(message)
    print("message here!!", message)
}

// segue override
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if(segue.identifier == "showThingSegue") {
        let vc = segue.destination as! MessagesViewController
        vc.messageID = "1234567"
    }
}

这很有效。我可以在创建之后使用i消息发送消息,并通过IPhone模拟器接收发送消息的人的segue来接收消息。

当我想在其他人线程中选择消息时,会出现问题。 Apple在模拟器中包含两个互连的消息线程,用于测试在您的扩展用户之间发送和接收消息。

当我在接收的线程中选择消息时,不会触发segue。它只是加载根视图控制器的新副本。

我想要的行为是获取特定消息实例的接收信息,并使用此数据触发segue。 Apple提供了以下实例属性来实现此目的。

请参阅此处的API文档中的selectedMessage:https://developer.apple.com/reference/messages/msconversation/1648186-selectedmessage

问题是这个实例属性是如何实现的?它是否在根视图控制器的onViewDidLoad()中实现了消息扩展? Apple的文档在如何实际实现实例属性以从收到的邮件扩展名末端的所选邮件中搜索收到的邮件数据方面非常模糊。有没有人实现过这个?

谢谢!

1 个答案:

答案 0 :(得分:1)

收件人选择邮件后,会调用willBecomeActive并为您提供MSConversation,其中包含selectedMessage

override func willBecomeActive(with conversation: MSConversation) {
    super.willBecomeActive(with: conversation)
    let message = conversation.selectedMessage
    ...
}

请参阅:Helpful Apple sample