11

Hızlı bir şekilde 3 aldığım bir uyarıya dokunarak başlatma seçeneğini işlemeye ve belirli bir görünüm denetleyicisini açmaya çalışıyorum. Örneğin here gibi benzer bir soru gördüm, ancak yeni hızlı 3 uygulama için hiçbir şey görmedim.Bir bildirim tıklandığında Swift 3'te başlatma seçenekleri nasıl kullanılır? Sözdizimi sorunları alma

var localNotif = (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as! String) 
if localNotif { 
    var itemName = (localNotif.userInfo!["aps"] as! String) 
    print("Custom: \(itemName)") 
} 
else { 
    print("//////////////////////////") 
} 

ama Xcode bana bu hatayı veriyor:

if let launchOptions = launchOptions { 
     var notificationPayload: NSDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary! 

    } 
: Ben de bu çalıştı

Type '[NSObject: AnyObject]?' has no subscript members 

Ben AppDelegate.swift ben didFinishLaunchingWithOptions aşağıdaki adres benzer bir soru izledi (ve)

ve bu hatayı alıyorum:

error: ambiguous reference to member 'subscript' 

Daha önce benzer bir kodun, bir sözlükten anahtar ile bir değer elde etmek için kullanmış olduğum her yerde benzer hatalar yaptım ve kodları değiştirmek zorunda kaldım ve temel olarak sözlüğü güvenli bir şekilde açmak istedim. Ama bu burada işe yaramıyor. Herhangi bir yardım takdir edilecektir. Teşekkürler.

cevap

7

Bu yüzden tüm yöntem imzası değişti ve yeni imzaları uyguladığımda iyi çalıştı. Kod aşağıdadır.

yeni didFinishLaunchingWithOptions yöntemi: Bu yardımcı olur

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { 



//and then 
if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil { 


// Do what you want to happen when a remote notification is tapped. 


} 

} 

Umut.

7

Apple, Swift 3 numaralı belgede çok sayıda değişiklik yaptı ve bunlardan biri. Bunu kullanın:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    //Launched from push notification 
    let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary 
    if remoteNotif != nil { 
     let aps = remoteNotif!["aps" as NSString] as? [String:AnyObject] 
     NSLog("\n Custom: \(String(describing: aps))") 
    } 
    else { 
     NSLog("//////////////////////////Normal launch") 
    } 
} 

Ve daha fazlası üzerinde LaunchOptionKeys için Apple'ın documentation okuyun.

+0

Hala hata, üye subscript için belirsiz başvuru olsun! – TheeBen

+0

Benim hatam, bir hata oldu. şimdi deneyin – Adeel

+0

Hmm, teşekkürler ama cevabınızı güncellediğinizden emin misiniz? Aynı – TheeBen

0

hızlı 3:

 if let notification = launchOptions?[.localNotification] as? NSDictionary{ 

      #if DEBUG 
       print("iOS9 didFinishLaunchingWithOptions notification\n \(notification)") 
      #endif 
0
if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: Any] { 
     if let notification = remoteNotif["aps"] as? [AnyHashable : Any] { 
       //handle PN 
     } 
} 
İlgili konular