This is my Swift 3 code:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let userInfo : Foundation.NSDictionary = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? Foundation.NSDictionary {
self.setNavigationInfoFromPushNotification(userInfo: userInfo)
navigateFromPushNotification()
}
...
}
It results in a compile-time error that says:
Ambiguous reference to member 'Subscript'
Please can anyone help me with this?
The method signature has changed. See How to handle launch options in Swift 3 when a notification is tapped? Getting syntax problems.
It is now:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
if let userInfo = launchOptions?[.remoteNotification] as? NSDictionary {
setNavigationInfoFromPushNotification(userInfo: userInfo)
navigateFromPushNotification()
}
...
}
Related
var centralManager: CBCentralManager!
#nonobjc func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
**if let centralManagerIdentifiers = launchOptions?[UIApplicationLaunchOptionsBluetoothCentralsKey]** {
NSLog("UIApplicationLaunchOptionsBluetoothCentralsKey = \(centralManagerIdentifiers)")
}
I am getting an error where I do the optional binding of CentralManagerIdentifiers,
can anyone help me with this?
Swift 3.0
Try this.
if let centralManagerIdentifiers = launchOptions?[UIApplicationLaunchOptionsKey.bluetoothCentrals] {
NSLog("UIApplicationLaunchOptionsBluetoothCentralsKey = \(centralManagerIdentifiers)")
}
I am trying to add user authentication functionality into my app using AWS Cognito as a backend.
So far I am getting this error whenever my app tries to build, and when it does it points to these errors in two of my classes.
AWSMobileClient Class:
func didFinishLaunching(application: UIApplication, withOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
print("didFinishLaunching:")
let didFinishLaunching = AWSIdentityManager.defaultIdentityManager().interceptApplication(application, didFinishLaunchingWithOptions: launchOptions)
if (!isInitialized) {
AWSIdentityManager.defaultIdentityManager().resumeSession(completionHandler: {(_ result: AnyObject, _ error: Error) -> Void in
print("result = \(result), error = \(error)")
} as! (Any?, Error?) -> Void) --> ERROR ON THIS LINE <--
isInitialized = true
}
return didFinishLaunching
}
AppDelegate Class:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return AWSMobileClient.sharedInstance.didFinishLaunching(application: application, withOptions: launchOptions as [NSObject : AnyObject]?) --> ERROR ON THIS LINE <--
Looks like AWS MobileHub sample code is not yet swift3 compatible--that is what I'm running in to; perhaps you as well? See here for an answer from AWS, and a hint to use the legacy compiler setting (which I've not sorted out just yet...)
I am trying to get the value for launchOptions from UIApplicationDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
If the launchOptions is a NSDictionary I probably can retrieve value by
launchOptions[launchOptions.UIApplicationLaunchOptionsRemoteNotificationKey]
But the object is [NSObject: AnyObject]. How to retrieve value for this type?
Your variant is not safe, better use if let statement:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let options = launchOptions as? [String: AnyObject],
notifyPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] {
// do smth with notifyPayload
}
return true
}
Swift is full of try and adjust
Here is my own answer.
let notifPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey as String!] as? NSDictionary
I'm building an app with Swift that receives push notifications. I am sending custom values inside the JSON.
I am opening the app through the notification, so I know that I have to do this inside "didFinishLaunchingWithOptions" and read the value from "launchOptions".
How can I read those values and use them on my app.
Many thanks.
Here's what works for me in SWIFT 2 when your app is not launched. The code is not quite elegant because of the optional bindings. But it works.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// if launched from a tap on a notification
if let launchOptions = launchOptions {
if let userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] {
if let action = userInfo["action"], id = userInfo["id"] {
let rootViewController = self.window!.rootViewController as! ViewController
let _ = setTimeout(5.0, block: { () -> Void in
rootViewController.openNotification(action as! String, id: id as! String)
})
}
}
}
return true
}
In the application:didReceiveRemoteNotification:fetchCompletionHandler, The custom data is in passed on to the didReceiveRemoteNotification, which is an NSDictionary. The details that you want to retrieve is probably on the "aps" key of the userInfo.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: NSDictionary!)
{
var notificationDetails: NSDictionary = userInfo.objectForKey("aps") as NSDictionary
}
When the app is not launched, you will need to get it from the application:didFinishedLaunchWithOptions,
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
if let launchOpts = launchOptions {
var notificationDetails: NSDictionary = launchOpts.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary
}
return true
}
EDIT: Remote Notification Fix syntax
This is the Oliver Zhang response which is updated for Swift 5.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// if launched from a tap on a notification
if let launchOptions = launchOptions {
if let userInfo = launchOptions[UIApplication.LaunchOptionsKey.remoteNotification] {
}
}
return true
}
I am finding this error in my AppDelegate.swift file and it appears in the AppDidFinishLaunchingWithOptions function. It is raising the error on a line of code that is from the Parse framework.
PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
The error is appearing on the launchOptions parameter. I will post the whole function to show that it should be correct. Also when I comment out the line of code the error disappears, but I still really want to be able to use the function and track the analytics. Here is the whole function:
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: AnyObject!) -> Bool
{
// Override point for customization after app launches
Parse.setApplicationId("removed on purpose", clientKey: "removed on purpose")
PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
PFFacebookUtils.initializeFacebook()
return true
}
I can't seem to find anything that relates to this error. If anyone has some insight I would really appreciate it!
Since Xcode 6 beta 7, when you want to call application:didFinishLaunchingWithOptions:, you have to replace:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
/* ... */
}
with the following code:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
/* ... */
}
The last parameter of this method is no more a NSDictionary but a Dictionary of type [NSObject: AnyObject]?. Therefore, you must update your code (including your trackAppOpenedWithLaunchOptions: parameter type).
The launchOptions parameter should be declared as NSDictionary! instead of AnyObject!:
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// ...
}