I am calling a custom URL from safari to launch an app. It works fine if the app is running in background. But when the app is not running in background , but is already installed on the device, the app does not launch. I have implemented both of the following methods:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
but none of them is called when app is not running in background.
Also I have googled and found that we can launch the app using the following code when app is not running in background
if ( [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey] != nil ) {
NSURL *url =(NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
[self application:application handleOpenURL:url];
}
in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
But unfortunately , didFinishLaunchingWithOptions is also not triggered. Does some one has any pointers?
P.S : This problem occurs only in iPad.It works fine on iPhone.
i was facing the same problem.
Seems that our code was running too fast.
Inserting a delay to run your custom code will resolve the issue.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
[NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(yourCustomActions) userInfo:nil repeats:NO];
return YES;
}
Hope it helps
Got to your info.plist and perform the necessary changes
- Add new row --> URL types
- expand it
- in item 0 add new row ---> URL Schemes
- expand it
In Item 0 under it, add the custom url you want to use
add one row under item 0 of urlTypes --> URL identifier
- provide the bundle id of your app there
eg: anubhab://stackoverflow.com/
for the above custom url you would set the URL Scheme item 0 as: anubhab
see the image attached for detail
I have followed these link. Hope it helps. It works fine for me even if I kill the app from background.
来源:https://stackoverflow.com/questions/23385542/custom-url-scheme-does-not-work-when-app-is-not-running-in-background