custom URL scheme does not work when app is not running in background

删除回忆录丶 提交于 2019-12-12 09:48:11

问题


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.


回答1:


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




回答2:


Got to your info.plist and perform the necessary changes

  1. Add new row --> URL types
  2. expand it
  3. in item 0 add new row ---> URL Schemes
  4. expand it
  5. In Item 0 under it, add the custom url you want to use

  6. add one row under item 0 of urlTypes --> URL identifier

  7. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!