#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; } } |