xcode 未在 iPhone 操作系统中使用自定义 url 架构调用 handleOpenURL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2961259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:24:45  来源:igfitidea点击:

handleOpenURL not called using a custom url schema in iPhone OS

iphoneobjective-cxcodeurl-scheme

提问by favo

I have successfuly added my own url schemes to my App. The App correctly launches using the schemes.

我已经成功地将我自己的 url 方案添加到我的应用程序中。应用程序使用方案正确启动。

Now I want to handle the incoming data but the delegate is not called. It is an universal app and I have added the following function to both AppDelegates:

现在我想处理传入的数据,但没有调用委托。它是一个通用应用程序,我已将以下功能添加到两个 AppDelegates:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    if (!url) {  return NO; }

    NSString *URLString = [url absoluteString];
    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:NSLocalizedString(@"test message", nil) 
                          message:URLString
                          delegate:self 
                          cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];     
    [alert release];
    return YES;
}

I am testing with a schema like: myapp://appalarm.com …and would expect to be appalarm.com in the URLString

我正在使用如下模式进行测试:myapp://appalarm.com ...并且希望在 URLString 中是 appalarm.com

What is wrong with it?

它有什么问题?

Thanks for your responses!

感谢您的回复!

回答by Christian Fries

I tried to clarify in another post. The answer of Ashley Clark is only partly correct. Under OS 4.0 the handleOpenURL gets called (at least for file URLs) and you have to implement it to handle open url calls for when the app is in background. Thus, opening the file in both methods may open it twice (if applicationDidFinishLaunchingWithOptions returned YES, which it should). See another post.

我试图在另一篇文章中澄清。Ashley Clark 的回答只是部分正确。在 OS 4.0 下,handleOpenURL 被调用(至少对于文件 URL),你必须实现它来处理应用程序在后台时的打开 url 调用。因此,在这两种方法中打开文件可能会打开它两次(如果 applicationDidFinishLaunchingWithOptions 返回 YES,它应该)。见另一个帖子

回答by Ashley Clark

If your application delegate has implemented 'applicationDidFinishLaunchingWithOptions:' then the 'handleOpenURL:' method will never be called. Look at the options passed in through the other method to determine how your app was launched and what behavior you should implement.

如果您的应用程序委托已经实现了 'applicationDidFinishLaunchingWithOptions:' 那么 'handleOpenURL:' 方法将永远不会被调用。查看通过其他方法传入的选项,以确定您的应用程序的启动方式以及您应该实现的行为。

回答by lee

Try your code into below function

尝试将您的代码转换为以下功能

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if (!url) {  return NO; }

    NSString *URLString = [url absoluteString];
    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:NSLocalizedString(@"test message", nil) 
                          message:URLString
                          delegate:self 
                          cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];     
    [alert release];
    return YES;
}