简体   繁体   English

当应用未在后台运行时,自定义URL方案不起作用

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

I am calling a custom URL from safari to launch an app. 我从safari调用自定义URL来启动应用程序。 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 此外,我用Google搜索,发现我们可以在应用未在后台运行时使用以下代码启动应用

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. 但不幸的是, didFinishLaunchingWithOptions也没有被触发。 Does some one has any pointers? 有人有指针吗?

PS : This problem occurs only in iPad.It works fine on iPhone. PS:此问题仅在iPad中出现。它在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 得到你的info.plist并进行必要的更改

  1. Add new row --> URL types 添加新行 - > URL类型
  2. expand it 扩大它
  3. in item 0 add new row ---> URL Schemes 在第0项中添加新行---> URL方案
  4. expand it 扩大它
  5. In Item 0 under it, add the custom url you want to use 在其下的第0项中,添加您要使用的自定义网址

  6. add one row under item 0 of urlTypes --> URL identifier 在urlTypes - > URL标识符的第0项下添加一行

  7. provide the bundle id of your app there 在那里提供您的应用程序的包ID

eg: anubhab://stackoverflow.com/ 例如:anubhab://stackoverflow.com/

for the above custom url you would set the URL Scheme item 0 as: anubhab 对于上面的自定义URL,您可以将URL Scheme项目0设置为: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. 即使我从后台杀了应用程序,它对我也很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM