直接上代碼
.m文件
#import "ViewController.h"
@interface ViewController () <UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView*webView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad]; //通過本地html文件加載網(wǎng)頁
[self.webView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle]URLForResource:@"test" withExtension:@"html"]]];
}
- (void)call
{ //撥打電話
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];
}
//是否允許加載從webview獲得的請求
/*
*該方法可以實現(xiàn)js調(diào)用OC
*js和OC交互的第三框架可以使用:WebViewJavaScriptBridge
*/
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//獲得html點擊的鏈接
NSString *url = request.URL.absoluteString;
//設(shè)置協(xié)議頭
NSString *scheme = @"zc://";
//判斷獲得的鏈接前面是否包含設(shè)置頭
if([url hasPrefix:scheme])
{
//切割字符串
NSString *methodName = [url substringFromIndex:scheme.length];
//調(diào)用打電話的方法
[self performSelector:NSSelectorFromString(methodName) withObject:nil];
return NO;
}else{
return YES;
}
}
html 文件
640.jpg
storyboard
640.png
運行結(jié)果
640.png