在ios中,用H5编写了可以打电话的页面。点击“拨打”按钮后可以进行跳转到打电话的页面(跳转这一块应该是和浏览器有关),跳转之后拨打完电话能够回到之前打电话的页面。
第一步:其实就是发送了一个get请求,url就是:tel:13587454758;与h5交互:获取到电话号码(URL);
第二步:[self openUrl:url];
第三步:
-(void)openUrl:(NSString *)urlStr{
NSURL *url = [NSURL URLWithString:urlStr];
UIApplication *application = [UIApplication sharedApplication];
if (![application canOpenURL:url]) {
NSLog(@"无法打开\"%@\", 请确保此应用正确安装", url);
}else
[[UIApplication sharedApplication]openURL:url];
}
获取网页url方式:
1.UIWebView
方法1:NSString *location =[webViewstringByEvaluatingJavaScriptFromString:@"document.location"];
方法2:NSLog(@"webView location = '%@'", webView.request.URL.absoluteString);
以上两种一定要放在页面成功加载之后才可以的!
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString*currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request
navigationType:(UIWebViewNavigationType)navigationType {
// 判断是否是单击
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSString *currentURL= myWebView.request.URL.absoluteString;
NSLog(@"你单击的网址是:%@", currentURL);
}
return YES;
}
2.WKWebview
#pragma mark 在发送请求之前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
// NSLog(@"%s",__FUNCTION__);
/**
*typedef NS_ENUM(NSInteger, WKNavigationActionPolicy) {
WKNavigationActionPolicyCancel, // 取消
WKNavigationActionPolicyAllow, // 继续
}
*/
NSString *reqUrl = [navigationAction.request.URL.absoluteString stringByRemovingPercentEncoding];
decisionHandler(WKNavigationActionPolicyAllow);
}
网友评论