打开电话:来自UIWebView的链接 [英] Opening tel: links from UIWebView

查看:60
本文介绍了打开电话:来自UIWebView的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了,但是似乎找不到解决此问题的方法.

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

由于某种原因,我无法在UIWebView中使用"tel:"链接来工作.单击链接后,出现消息无法显示URL".在Safari中单击相同的链接可以完美地工作并拨打电话号码.

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.

此问题始于iOS5.这些链接在iOS 4.2和4.3中运行良好.

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

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

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

谢谢!

这是实际使用中的代码...

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语句,该号码将立即拨打而没有确认.我真的很希望它能像以前一样发挥作用,它会发出警报,让您可以选择在拨打号码之前按呼叫"或取消".

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.

推荐答案

如果以HTML链接的形式启动,则将以以下形式打开tel URL方案:

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

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

如果您是从本机URL字符串启动的(这意味着您是在Objective-C中编写的,而不是通过WebView进行提供的),则您的URL字符串应如下所示:

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:

tel:1-408-555-5555

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

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

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

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

代码反馈

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

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). 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. 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.

事物如何运作和您想要什么

操作系统将无法为您显示呼叫/取消"警报对话框.那取决于你.它在Safari中显示是因为Safari应用程序的shouldStartLoadWithRequest方法无疑通过显示UIAlertView来响应tel:方案. if ([url.scheme isEqualToString:@"tel"])的条件,应在YES时使用呼叫并取消"按钮触发UIAlertView.在通话中,您将把sharedApplication告诉openURL;取消后,您将不会发出呼叫&您还需要返回NO,这样您的应用就不会尝试返回loadWithRequest.

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. Your conditional for if ([url.scheme isEqualToString:@"tel"]) should, when YES, trigger a UIAlertView with a Call and Cancel button. On Call, you will tell the sharedApplication to openURL; on Cancel, you will not issue the call & you will also want to return NO so your app does not attempt to loadWithRequest.

自动更正修改

为公平起见,我将自己的回答留在上面.

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.

我还错误地浏览了您的代码仅在方案为httphttps时才将URL处理传递给sharedApplication.

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

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

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/https and just use the canOpenURL check?

但是,除了我对条件&对话框本身,您仍然不应该返回YES.要拨打电话,您只能通过将其传递到sharedApplication:openURL并确保返回NO来实现,因为您的应用程序不是电话"应用程序.您希望通过此方法返回YES的唯一原因是,如果您的应用程序将以特殊的方式处理tel:链接,而无需将其发送到Phone应用程序.

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:openURL and ensuring you return NO because your app is not the Phone app. 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.

这篇关于打开电话:来自UIWebView的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆