如果在应用程序终止后从通知正文启动应用程序,我将尝试打开弹出窗口。我想通过 AppDelegate
来完成它。我正在使用 LocalNotifications
。如果我使用操作按钮,我知道如何打开特定 View ,但如果单击通知正文,我不知道如何打开某些内容。
编辑:我的解决方案仅在应用未终止时才有效。
Edit2:这样做是否正确?:
为简单起见,我尝试在代码中打开 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{
中,但它没有打开特定 View :
让 mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "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(modal)。
最佳答案
如果您的应用终止并且您收到通知并点击通知,那么您可以通过以下代码获取此信息,并且您需要在 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()
}
如果您的应用程序不处于后台模式或事件模式,您可以通过这种方式为通知设置特定的 Root View Controller 。然后,一旦您从通知 View Controller 返回,您需要根据您的应用程序流程更改您的 Root View Controller 。
在查看您的示例代码后,我通过以下更改进行修复:
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
}
}
关于swift - 如果在应用程序终止后从通知主体启动应用程序,则打开弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37835796/