如果在应用程序终止后从通知正文启动应用程序,我正在尝试打开popover。我想从AppDelegate
开始做。我正在使用LocalNotifications
。我知道如果我使用动作按钮但是如果点击通知主体则不知道如何打开某些视图时如何打开特定视图。
修改:仅当应用未终止时,我的解决方案才有效。
编辑2:这是正确的方法吗?:
为简单起见,我试图在代码中打开viewController
,但实际上我需要提醒信息,因为我正在使用JSSAlertView
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
if let TappedNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? NSDictionary {
print("The notification is \(TappedNotification)")
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
}
return true
}
我试过这个来检测应用程序的状态,但如果应用程序终止并从通知中打开,我无法测试它:
if application.applicationState == UIApplicationState.Active {
print("App already open")
} else {
print("App opened from Notification")
}
我尝试将其添加到else{
但它没有打开特定视图:
让mainStoryboardIpad:UIStoryboard = UIStoryboard(名称:“Main”,bundle:nil)
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
我想要像Twitter或Instagram一样的效果,如果点击通知主体,它会重定向你发布。但在我的情况下,我只想要popover(莫代尔)。
答案 0 :(得分:1)
如果你的应用程序被终止并且你收到通知并点击通知,那么你可以通过以下代码获得此信息,并且需要用didFinishLaunchingWithOptions
方法编写代码:
if let TappedNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
print("The notification is \(TappedNotification)")
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
如果您的应用未处于后台模式或活动模式,您可以通过这种方式为通知设置特定的根视图控制器。然后,一旦从通知视图控制器返回,您需要根据应用程序流程更改rootviewcontroller。
在审核了您的示例代码后,我修复了以下更改:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let tintColor = UIColor(red: 252/255, green: 72/255, blue: 49/255, alpha: 1)
window!.tintColor = tintColor
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert , categories: nil))
let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as! UILocalNotification!
if (notification != nil) {
print("The notification is \(notification)")
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as! NotificationViewController
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
let alert = UIAlertController(title: "Alert", message: "YES NOTIFICITO", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
window!.rootViewController?.presentViewController(alert, animated: true, completion: nil)
return true
}else{
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("first") as UIViewController
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
let alert = UIAlertController(title: "Alert", message: "NO NOTIFICIATION", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
window!.rootViewController?.presentViewController(alert, animated: true, completion: nil)
return true
}
}