自定义输入附件视图通知

时间:2016-12-18 13:47:47

标签: uitextfield swift3 xcode8 inputaccessoryview unusernotificationcenter

Advanced Notification Session 708 WWDC 2016结束时。他们谈到了采取文字输入和同时行动。

从WWDC会话708的24分钟开始。

你对聚会邀请和评论发表评论同时接受或拒绝邀请。我试图这样做但是非常不成功。

class NotificationViewController: UIViewController, UNNotificationContentExtension {

var textField = UITextField(frame: CGRect(x: 0, y: 0, width: 100, height: 10))

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any required interface initialization here.
}

override var canBecomeFirstResponder: Bool {

    return true
}

override var inputAccessoryView: UIView? {

    let customView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 40))

    customView.backgroundColor = UIColor.red

    textField.placeholder = "Type Comment here"

    textField.textColor = UIColor.black

    customView.addSubview(textField)

    print(customView)

    return customView
}

func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {

       if response.actionIdentifier == "comment" {

        becomeFirstResponder()

        textField.becomeFirstResponder()

        print("Its working")

        if let textResponse = response as? UNTextInputNotificationResponse {

            print(textResponse.userText)

            completion(UNNotificationContentExtensionResponseOption.doNotDismiss)
        }

        completion(UNNotificationContentExtensionResponseOption.doNotDismiss)
    }
}

}

1 个答案:

答案 0 :(得分:2)

不确定您的问题究竟是什么,但我最近遇到了类似的任务 - 我已经成功实施了您推荐的WWDC谈话中的解决方案。
我的问题是我在使用自定义inputAccessoryView时不知道如何解除通知。我的解决方案是保存completionHandler,然后在点击自定义inputAccessoryView中的特定按钮时调用它。

首先;创建函数变量来存储completionHandler:

var savedCompletionBlock:(UNNotificationContentExtensionResponseOption) -> Void = { (UNNotificationContentExtensionResponseOption) in }

然后;在func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void)中保存完成块:

savedCompletionBlock = completion

最后;在任何需要的地方调用它(例如点击按钮):

func confirmPressed() {
   savedCompletionBlock(.dismiss)
}

如果这对您没有帮助,请告诉我:)