I have implemented a URL Scheme and use it to pass data to my app by calling method. The entire code is shown as below
- (BOOL)application:(UIApplication *)a
I believe there is a better answer now as,
application:handleOpenURL:
application:openURL:sourceApplication:annotation:
Both are deprecated in ios 9. Apple suggestion is:Use
application:openURL:options:
instead.
application:openURL:options:
has different behaviour than the old ones, as it will be executed in case the app was in background or will launch.
So, you need to handle the URL opening within it only. like below:
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<NSString *,id> *)options {
// Check the calling application Bundle ID
if ([[url scheme] isEqualToString:@"yuvitime"])
{
NSLog(@"URL scheme:%@", [url scheme]);
NSString * yuvitimeRequestValue = [url query];
NSDictionary * userInfor = [[NSDictionary alloc]initWithObjectsAndKeys:yuvitimeRequestValue, @"YuvitimeRequest", nil];
NSNotificationCenter * notificationCentre = [NSNotificationCenter defaultCenter];
[notificationCentre postNotificationName:@"URLSCHEMEACTIVATEDNOTIFICATION" object:self userInfo:userInfor];
return YES;
}
else
return NO;
}
For iOS 10, use
func application(_ app: UIApplication,
open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
Hi when the app is not launched before, the method "handleOpenURL" is never called. You have to check "launchOptions" in didFinishLaunchingWithOptions for object with key "UIApplicationLaunchOptionsURLKey"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
//call function to handle the url like in handleURL, but do not call handleURL directly
}
I landed up with the same issue for an app on iOS 13. Even with the proper implementation of - (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
method, it never got invoked.
From iOS13, the SceneDelegates get invoked instead of the AppDelegate method. Once I implemented the
- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts
on the scene delegate, it would work if the app was already in memory. However, for a cold start, I had to implement the call back
-(void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions
as well.
While implementing
-(void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions
please remember to handle cases when the app is not launched from an URL.
Here is an useful reference: https://forums.developer.apple.com/thread/124132
For me, do not make application(_:open options:)
outside AppDelegate {} scope
I had the same problem when application(_:open:options:)
wasn't called when the app was in background. The reason for this was the Firebase SDK which uses method swizzling.
I solved this problem by setting FirebaseAppDelegateProxyEnabled = NO
in Info.plist
For more detailed information on how it works and what it affects you can read here https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in