【问题标题】:Open popover if application is launched from notification body after app was terminated如果应用程序终止后从通知正文启动应用程序,则打开弹出窗口
【发布时间】:2016-06-15 12:42:11
【问题描述】:

如果应用程序终止后从通知正文启动应用程序,我正在尝试打开弹出框。我想从AppDelegate 开始。我正在使用LocalNotifications。如果我使用操作按钮,我知道如何打开特定视图,但如果单击通知正文,我不知道如何打开某些内容。

编辑:我的解决方案仅在应用未终止时才有效。

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{,但它没有打开特定视图:

让 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)。

【问题讨论】:

    标签: swift uiviewcontroller appdelegate


    【解决方案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()
    
        }
    

    如果您的应用程序未处于后台模式或活动模式,您可以通过这种方式为通知设置特定的 rootview 控制器。从通知视图控制器返回后,您需要根据应用程序流程更改根视图控制器。

    查看您的示例代码后,我修复了以下更改:

     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
            }
    
        }
    

    【讨论】:

    • @So 如果我理解你正确我的代码现在应该可以工作了。但它不起作用。检查我的编辑。方法对吗?
    • 在 UIApplicationLaunchOptionsRemoteNotificationKey 中是否不需要检查 application.applicationState == UIApplicationState.Active {
    • 我试过没有它,但它仍然无法工作。哦等等。我需要将我的初始 viewcotroller 设置为 "Main" 吗?因为它实际上不是初始 ATM。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    相关资源
    最近更新 更多