Я пытаюсь показать определенный viewcontroller, когда получаю удаленное push-уведомление. Я добавил все из моего кода в метод didReceiveRemoteNotification
:Показать uiviewcontroller из удаленного уведомления, когда приложение закрыто
func application(application: UIApplication, didReceiveRemoteNotification userinfo: [NSObject: AnyObject])
Я добавил следующий код:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
let code = (userInfo["aps"] as! [String: AnyObject])
// Call to retrieve blog
if let blog = code["b"] as? NSNumber {
let blogId = blog as! Int
// Show blog from notification
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var controller = mainStoryboard.instantiateViewControllerWithIdentifier("blogCtrl") as! BlogController
controller.blogId = blogId
var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
self.window?.rootViewController = rootController
rootController.pushViewController(controller, animated: true)
self.window?.makeKeyAndVisible()
}
if let tonic = code["t"] as? NSNumber {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var controller = mainStoryboard.instantiateViewControllerWithIdentifier("tonicDetail") as! TonicDetailController
controller.tonicId = tonic as! Int
var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
self.window?.rootViewController = rootController
rootController.pushViewController(controller, animated: true)
self.window?.makeKeyAndVisible()
}
if let gin = code["g"] as? NSNumber {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var controller = mainStoryboard.instantiateViewControllerWithIdentifier("GinDetail") as! GinDetailController
controller.ginId = gin as! Int
var rootController = mainStoryboard.instantiateViewControllerWithIdentifier("navCtrl1") as! UINavigationController
self.window?.rootViewController = rootController
rootController.pushViewController(controller, animated: true)
self.window?.makeKeyAndVisible()
}
}
Когда приложение в фоновом режиме все работает, но когда приложение quited, и Я получаю удаленное уведомление, которое только запускает приложение. Есть ли метод, который можно вызвать, если приложение было отправлено раньше?
В первой строке ваш код, который вы создаете UILocalNotification, но в четвертой строке вы используете его как NSDictionary. Это верно? Потому что, когда я пытаюсь ваш код, у меня есть ошибка: 'var notification = launchOptions? [UIApplicationLaunchOptionsRemoteNotificationKey] as? UILocalNotification если уведомление = ноль { Println ("Был уведомление") self.application (приложение, didReceiveRemoteNotification: уведомление, NSDictionary)}! ' Ошибка говорит:' NSDictionary не неявное преобразование [NSObject: AnyObject] ] ' –
@HannesVandenBerghe вместо 'UILocalNotification' использует 'NSDictionary', который будет работать – Aanabidden
@Aanabidden это действительно решение! Благодаря! –