3

我正在从 safari 调用自定义 URL 来启动应用程序。如果应用程序在后台运行,它工作正常。但是当应用程序没有在后台运行,但已经安装在设备上时,应用程序不会启动。我已经实现了以下两种方法:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

但是当应用程序不在后台运行时,它们都不会被调用。

我也用谷歌搜索,发现当应用程序不在后台运行时,我们可以使用以下代码启动应用程序

if ( [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey] != nil ) {
    NSURL *url =(NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
    [self application:application handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

但不幸的是,didFinishLaunchingWithOptions也没有被触发。有人有任何指示吗?

PS:这个问题只出现在 iPad 上。它在 iPhone 上运行良好。

4

2 回答 2

3

我面临同样的问题。

似乎我们的代码运行得太快了。

插入延迟以运行您的自定义代码将解决该问题。

- (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;
}

希望能帮助到你

于 2015-04-08T01:28:45.523 回答
0

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 enter image description here

I have followed these link. Hope it helps. It works fine for me even if I kill the app from background.

Wayback machine version of that link: https://web.archive.org/web/20170707150909/http://chrisrisner.com/31-Days-of-iOS-Day-18-Opening-your-App-from-a-Website.

于 2014-04-30T12:32:44.690 回答