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

storyboard

運行結果

文章轉自 GentlePrince的簡書