[objective-c]UIWebViewでリンククリックで処理
UIWebViewでリンククリックで処理
読み込むhtml
<!DOCTYPE html> <html> <head> </head> <body> <a href="js-call://camera">camera</a><br/><br/> <a href="js-call://album">album</a><br/><br/> <a href="js-call://alert">alert</a><br/><br/> </body> </html>
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSLog(@"shouldStartLoadWithRequest"); NSString *requestString = [[request URL] absoluteString]; NSString *protocol = @"js-call://"; if ([requestString hasPrefix:protocol]) { NSString *requestContent = [requestString substringFromIndex:[protocol length]]; NSArray *vals = [requestContent componentsSeparatedByString:@"/"]; if ([vals[0] isEqualToString:@"camera"]){ [self startCamera];//カメラ起動 }else if ([vals[0] isEqualToString:@"album"]){ [self startAlbum];//カメラロール起動 }else { [webView stringByEvaluatingJavaScriptFromString:@"alert('Not defined');"];//jsのalert実行 } return NO; } return YES; }