简体   繁体   English

打开电话:来自UIWebView的链接

[英]Opening tel: links from UIWebView

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. 由于某种原因,我无法在UIWebView中使用“ tel:”链接。 When the links are clicked, the message "The URL can't be shown" appears. 单击链接后,将显示消息“无法显示URL”。 Clicking on the same link in Safari works perfectly and dials the number. 在Safari中单击同一链接可以完美地工作并拨打号码。

This problem started with iOS 5. These links worked perfectly in iOS 4.2 and 4.3. 此问题始于iOS5。这些链接在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. 如果我取出第一个if语句,则无需确认立即拨打该号码。 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 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 iPhone only). 注意:这仅适用于已安装“电话”应用程序的iOS设备(仅适用于iPhone )。 iPad & iPod Touch devices will display a warning message. 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 if statement in the shouldStartLoadWithRequest method (where you return YES ). 您评论当在shouldStartLoadWithRequest方法(在此返回YES )中保留第一个if语句时,什么都不会发生。 This is exactly the behavior you should see because your app is not the Phone app. 这正是您应该看到的行为,因为您的应用程序不是“电话”应用程序。 Only the Phone app can handle the tel: URL scheme. 只有“电话”应用程序可以处理tel: URL方案。 By returning YES , you are telling the OS that your app will handle the phone call, but it cannot. 通过返回YES ,您告诉操作系统您的应用程序将处理电话,但不能。 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 ([[UIApplication sharedApplication] canOpenURL:url])允许sharedApplication (在这种情况下为Phone应用程序)发起呼叫。

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 shouldStartLoadWithRequest method undoubtedly responds to the tel: scheme by showing a UIAlertView . 它在Safari中显示是因为Safari应用程序的shouldStartLoadWithRequest方法无疑通过显示UIAlertView来响应tel:方案。 Your conditional for if ([url.scheme isEqualToString:@"tel"]) should, when YES , trigger a UIAlertView with a Call and Cancel button. 您的条件if ([url.scheme isEqualToString:@"tel"])应该在为YES时使用Call and Cancel按钮触发UIAlertView On Call, you will tell the sharedApplication to openURL ; 在Call上,您将告诉sharedApplicationopenURL on Cancel, you will not issue the call & you will also want to return NO so your app does not attempt to loadWithRequest . 在“取消”上,您将不会发出呼叫,并且您还希望返回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 http or https . 我还错误地浏览了您的代码仅在方案为httphttps时才将URL处理传递给sharedApplication。

After another look at the code, I wonder if, by any chance you have debug options on in Safari? 再看一下代码,我想知道您是否有机会在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 / https and just use the canOpenURL check? 如果删除http / https的条件检查并仅使用canOpenURL检查会怎样?

However, aside from the error in my comments on the conditional & dialog itself, you still should not be returning YES . 但是,除了我对条件和对话框本身的注释中的错误之外,您仍然不应该返回YES To make a phone call, you should only be able to pull that off by passing it to sharedApplication:openURL and ensuring you return NO because your app is not the Phone app. 要拨打电话,您只能通过将其传递给sharedApplication:openURL并确保您返回NO因为您的应用程序不是Phone应用程序。 The only reason you'd want to return YES in 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的唯一原因是,如果您的应用程序要处理tel:链接,而无需以特殊方式将其发送到Phone应用程序。

If you created the UIWebView in a .xib, select the UIWebView and check its attributes in the Attribute Inspector. 如果您在.xib中创建了UIWebView,请选择UIWebView并在“属性检查器”中检查其属性。 The first heading should be 'Web View', and under that it provides a list of checkboxes marked 'Detection'. 第一个标题应为“ Web视图”,并在其下提供标记为“检测”的复选框列表。 Ensure that 'Phone Numbers' is checked. 确保选中“电话号码”。

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

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