XCode : why launchOptions in didFinishLaunchingWithOptions always nil?
I could not find an error in the code you shared. Normally this should work both in the simulator, and on the device. Here is an example that worked for me: First generate a new Single View iPhone app (ARC and Storyboards on). Then change two methods in the AppDelegate
as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:[launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeInterval:10.0 sinceDate:[NSDate date]];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Just some text";
localNotif.alertAction = @"OK";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.userInfo = @{@"test": @YES};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Then start this app, press the home button, then stop the app. If you tap on the local notification which comes in 10 seconds after pressing the home button, you should see something like this, which shows the local notification has been passed to -application:didFinishLaunchingWithOptions:
:
My advice is: First get the example I posted above to work to make sure nothing has gone awry with your setup, then check what you are doing differently in your code.
Edit
This applies to Edit 2 of the question: Local notifications also seem to work for me when waiting for the app to launch (in the simulator, not on the device). Try this:
- Install the sample app described above and launch it with "Wait for MyApp.app to launch" disabled.
- Click on the home button, then stop the app via Xcode or via the task bar.
- Enable "Wait for MyApp.app to launch".
- If you now tap the notification in the notification center, it is shown in the alert view.
SwiftUI willFinishLaunchingWithOptions has launchOptions = nil
You can get notification from SceneDelegate not AppDelegate with connectionOptions.notificationResponse
From
https://dev.to/nemecek_f/ios-13-launchoptions-always-nil-this-is-the-reason-solution-kki
& https://stackoverflow.com/a/61322065/12208004
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let notificationResponse = connectionOptions.notificationResponse {
// you can get notification here
}
let contentView = ContentView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
[UPDATE]
You can get notification from userNotificationCenter
even when the app is not started
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
print("open notification")
let userInfo = response.notification.request.content.userInfo
print(userInfo)
completionHandler()
}
application(_ application: UIApplication, didFinishLaunchingWithOptions) not called on start up
The problem is just the way you "relaunch in the sim". If you kill the app and then tap the app's icon in the simulator, you are no longer running in Xcode; you are running independently. So you don't get any debugging any more; no print
messages appear in the Xcode console, you don't stop at breakpoints, etc.
The solution is: relaunch by telling Xcode to build and run again, not by tapping the app's icon in the simulator.
Related Topics
How to Find Out the Objective-C Generics Type
How to Get MAC Address from Cbperipheral and Cbcenter
Programmatically Creating Uinavigationcontroller in iOS
Apns Push Notifications Not Working on Production
Initializing Swift Properties That Require "Self" as an Argument
Why Is -Force_Load No Longer Required for My Three20 Dependencies in Xcode 4.2
Set the Status Bar to Black Colour
Binding Viewmodel and Textfields with Swiftui
How to Pass Params on Timer Selector
Recording from Remoteio: Resulting .Caf Is Pitch Shifted Slower + Distorted
How to Access the Accelerometer from the Apple Watch
iOS App Getting Throttled from Local Searches
Uisplitviewcontroller on iPad with Storyboards
Programmatically Detect Dark Mode in Swiftui to Display Appropriate Image
How to Create Multi Line Uisegmentedcontrol