大佬教程收集整理的这篇文章主要介绍了swift – 从远程通知打开ViewController,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当我收到通知时,我想打开一个“SimplePostViewController”,所以这是我的appDelegate:
var window: UIWindow? var navigationVC: UINavigationController? func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert,UIUserNotificationType.badge,UIUserNotificationType.sound] let pushNotificationSetTings = UIUserNotificationSetTings(forTypes: notificationTypes,categories: nil) let storyboard = UIStoryboard(name: "Main",bundle: nil) self.navigationVC = storyboard.instantiateViewControllerWithIdentifier("LastestPostsNavigationController") as? UINavigationController application.registerUserNotificationSetTings(pushNotificationSetTings) application.registerForRemoteNotifications() return true } func application(application: UIApplication,didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { if let postId = userInfo["postId"] as? String { print(postId) let api = EVwordpressAPI(wordpressOauth2SetTings: wordpress.wordpressOauth2SetTings,site: wordpress.siteName) api.postById(postId) { post in if (post != nil) { self.navigationVC!.pushViewController(SimplePostViewController(),animated: truE) } else { print("An error occurred") } } } }
我在应用程序启动时保存了我的UINavigationViewController,并在收到通知时尝试推送新的SimplePostViewController.但什么都没发生.
我放置断点并看到我的pushViewController方法已到达,但不是我的SimplePostViewController的ViewWillAppear.
我还使用了“什么是新的”视图添加执行我的segue但也没有发生任何事情.
方案:
for child in (self.rootViewController?.childViewControllers)! { if child.restorationIdentifier == "LastestPostsNavigationController" { let lastestPostsTableViewController = (child.childViewControllers[0]) as! LastestPostsTableViewController let simplePostVC = (self.storyboard?.instantiateViewControllerWithIdentifier("PostViewController"))! as! PostViewController simplePostVC.post = post lastestPostsTableViewController.navigationController?.pushViewController(simplePostVC,animated: truE) } }
我用 :
child.childViewControllers[0]
因为我的例子里只有一个孩子.
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Subscribe for notifications - assume the user chose yes for Now application.registerUserNotificationSetTings(UIUserNotificationSetTings(forTypes: [.Alert,.badge,.sound],categories: nil)) return true } func applicationDidEnterBACkground(application: UIApplication) { //Crete a local notification let notification = UIlocalnotification() notification.alertBody = "This is a fake notification" notification.fireDate = NSDate(timeIntervalSinceNow: 2) UIApplication.sharedApplication().schedulelocalnotification(notification) } func application(application: UIApplication,didReceivelocalnotification notification: UIlocalnotification) { let sb = UIStoryboard(name: "Main",bundle: nil) let otherVC = sb.instantiateViewControllerWithIdentifier("otherVC") as! OtherViewController window?.rootViewController = otherVc; } func application(application: UIApplication,didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { //Your code here }
`
您需要担心管理视图层次结构并向其发送需要从通知用户数据发送的任何内容.
在我的示例中,当您关闭在视图秒后触发的应用程序时,我会创建本地通知.如果您随后从通知中启动应用程序,它将打开“其他视图控制器”,在您的情况下将是“SimplePostViewController”.
另外,请确保在didFinishLaunchWithOptions中注册远程通知.
Github非常简单的样本:https://github.com/spt131/exampleNotificationResponse
以上是大佬教程为你收集整理的swift – 从远程通知打开ViewController全部内容,希望文章能够帮你解决swift – 从远程通知打开ViewController所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。