让我展示我的项目架构.
这是我的故事板:
当我收到通知时,我想打开一个“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]
因为我的例子里只有一个孩子.
解决方法 我创建了一个带有本地通知而不是远程通知的示例项目,以便于显示功能,但它应该像在app delegate dIDreceiveremote通知中设置窗口的根视图控制器一样简单.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所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)