xcode 打开电话:来自 UIWebView 的链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8102829/
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 22:24:11  来源:igfitidea点击:

Opening tel: links from UIWebView

iphoneiosxcodeuiwebview

提问by Jeremy Vanderburg

I've searched and searched, but I can't seem to find a fix for this problem.

我已经搜索和搜索,但我似乎无法找到解决此问题的方法。

For some reason, I can not get 'tel:' links to work in a UIWebView. When the links are clicked, the message "The URL can't be shown" appears. Clicking on the same link in Safari works perfectly and dials the number.

出于某种原因,我无法获得“电话:”链接以在 UIWebView 中工作。单击链接时,将显示消息“无法显示 URL”。在 Safari 中单击同一个链接可以正常工作并拨打号码。

This problem started with iOS 5. These links worked perfectly in iOS 4.2 and 4.3.

这个问题始于 iOS 5。这些链接在 iOS 4.2 和 4.3 中完美运行。

I'm not sure what other information might be useful, so please let me know if I need to clarify.

我不确定还有哪些其他信息可能有用,所以如果我需要澄清,请告诉我。

Thanks!

谢谢!

EDIT:

编辑:

Here is the actual code in use...

这是正在使用的实际代码...

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *url = request.URL;    

    if ([url.scheme isEqualToString:@"tel"]) {
        return YES;
    }

    if (![url.scheme isEqualToString:@"http"] && ![url.scheme isEqualToString:@"https"]) {
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
            return NO; // Let OS handle this url
        }
    }

    [NSThread detachNewThreadSelector:@selector(startBusy) toTarget:self withObject:nil];
    return YES;
}

If I take out the first if statement, the number gets dialed immediately with no confirmation. I'd really like it to function the way it used to by giving an alert, giving you the option to hit either 'Call' or 'Cancel' before dialing the number.

如果我取出第一个 if 语句,则会立即拨打该号码而无需确认。我真的很希望它能够像以前一样通过发出警报来运行,让您可以选择在拨打号码之前点击“呼叫”或“取消”。

回答by bobwaycott

If launching as an HTML link, the tel URL scheme will be opened if they appear as:

如果作为 HTML 链接启动,tel URL 方案将被打开,如果它们显示为:

<a href="tel:1-408-555-5555">1-408-555-5555</a>

If you are launching from a native URL string (meaning you coded this in Objective-C and are not serving it via a WebView), your URL string should look like this:

如果您从本机 URL 字符串启动(意味着您在 Objective-C 中对其进行编码,并且没有通过 WebView 提供它),您的 URL 字符串应如下所示:

tel:1-408-555-5555

Note:This only works with iOS devices that have the Phone app installed (that means iPhoneonly). iPad & iPod Touch devices will display a warning message.

注意:这仅适用于安装了电话应用程序的 iOS 设备(即仅限iPhone)。iPad 和 iPod Touch 设备将显示警告消息。

Note 2:Ensure the phone numbers you are passing do not contain spaces or other special characters (such as * and #).

注意 2:确保您传递的电话号码不包含空格或其他特殊字符(例如 * 和 #)。

Code Feedback

代码反馈

Based on your code, things are a bit clearer now. You comment about how nothing happens when you leave the first ifstatement in the shouldStartLoadWithRequestmethod (where you return YES). This is exactly the behavior you shouldsee because your app is not the Phone app. Only the Phone app can handle the tel:URL scheme. By returning YES, you are telling the OS that your app will handle the phone call, but it cannot. You get the call when that conditional is removed because the next block, which checks if ([[UIApplication sharedApplication] canOpenURL:url])allows the sharedApplication(which, in this case, is the Phone app) to launch the call.

根据您的代码,现在事情变得更清楚了。当您将第一条if语句留在shouldStartLoadWithRequest方法中(您返回的位置YES)时,您会评论如何没有任何反应。这正是您应该看到的行为,因为您的应用程序不是电话应用程序。只有电话应用程序可以处理tel:URL 方案。通过 return YES,您告诉操作系统您的应用程序将处理电话,但它不能。当该条件被删除时,您会收到呼叫,因为下一个块检查if ([[UIApplication sharedApplication] canOpenURL:url])允许sharedApplication(在这种情况下,是电话应用程序)启动呼叫。

How Things Work & What You Want

事情如何运作以及你想要什么

The OS is not going to handle showing the Call/Cancel alert dialog for you. That is up to you. It shows up in Safari because the Safari app's shouldStartLoadWithRequestmethod undoubtedly responds to the tel:scheme by showing a UIAlertView. Your conditional for if ([url.scheme isEqualToString:@"tel"])should, when YES, trigger a UIAlertViewwith a Call and Cancel button. On Call, you will tell the sharedApplicationto openURL; on Cancel, you will not issue the call & you will also want to return NOso your app does not attempt to loadWithRequest.

操作系统不会为您显示呼叫/取消警报对话框。那取决于你。它出现在 Safari 中是因为 Safari 应用程序的shouldStartLoadWithRequest方法无疑会tel:通过显示UIAlertView. 您的条件 for if ([url.scheme isEqualToString:@"tel"])should, when YESUIAlertView使用 Call 和 Cancel 按钮触发 a 。随叫随到,你会告诉sharedApplicationopenURL; 在取消时,您不会发出呼叫,并且您还想返回,NO因此您的应用程序不会尝试loadWithRequest

Self-Correcting Edit

自我修正编辑

To be fair about errors in my own thought process, I'm leaving my responses above.

为了公平对待我自己思考过程中的错误,我将在上面留下我的回答。

I believe the Call/Cancel dialog is, in fact, a feature of the OS. Apologies for the inaccuracy.

我相信呼叫/取消对话框实际上是操作系统的一个功能。对不准确之处表示歉意。

I'd also erroneously glanced over your code's passing off URL handling to sharedApplication only occurring when the scheme was httpor https.

我还错误地浏览了您的代码将 URL 处理传递给 sharedApplication 仅在方案为http或时发生的情况https

After another look at the code, I wonder if, by any chance you have debug options on in Safari? I believe this prevents the alert from popping up. Also--just to double-check the obvious--you aren't trying this inside the simulator, correct? What happens if you remove the conditional check for http/httpsand just use the canOpenURLcheck?

再次查看代码后,我想知道您是否有机会在 Safari 中打开调试选项?我相信这可以防止警报弹出。另外——只是为了仔细检查显而易见的——你不是在模拟器中尝试这个,对吗?如果删除http/的条件检查https并只使用canOpenURL检查会发生什么?

However, aside from the error in my comments on the conditional & dialog itself, you still should not be returning YES. To make a phone call, you should only be able to pull that off by passing it to sharedApplication:openURLand ensuring you return NObecause your app is not the Phone app. The only reason you'd want to return YESin this method is if your app is going to handle a tel:link in a special way that doesn't involve sending it to the Phone app.

但是,除了我对条件 & 对话框本身的评论中的错误之外,您仍然不应该返回YES. 要拨打电话,您应该只能通过将其传递给sharedApplication:openURL并确保您返回来完成它,NO因为您的应用程序不是电话应用程序。您希望YES在此方法中返回的唯一原因是您的应用程序是否将以tel:特殊方式处理链接,而不涉及将其发送到电话应用程序。

回答by Jezen Thomas

If you created the UIWebView in a .xib, select the UIWebView and check its attributes in the Attribute Inspector. The first heading should be 'Web View', and under that it provides a list of checkboxes marked 'Detection'. Ensure that 'Phone Numbers' is checked.

如果您在 .xib 中创建了 UIWebView,请选择 UIWebView 并在属性检查器中检查其属性。第一个标题应该是“Web 视图”,在它下面提供了一个标记为“检测”的复选框列表。确保选中“电话号码”。