码迷,mamicode.com
首页 > 移动开发 > 详细

IOS网络第七天WebView-03js中调用webView中的代码

时间:2015-09-15 11:08:53      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

***********

#import "HMViewController.h"

@interface HMViewController () <UIWebViewDelegate>
@end

@implementation HMViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1.webView
    UIWebView *webView = [[UIWebView alloc] init];
    webView.frame = self.view.bounds;
    webView.delegate = self;
    [self.view addSubview:webView];
    
    // 2.加载网页
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{

}

/**
 *  webView每当发送一个请求之前,都会先调用这个方法(能拦截所有请求)
 */
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *url = request.URL.absoluteString;
    NSRange range = [url rangeOfString:@"hm://"];
    NSUInteger loc = range.location;  //位置
    if (loc != NSNotFound) { // url的协议头是hm
        // 方法名
        NSString *method = [url substringFromIndex:loc + range.length];
        
        // 转成SEL
        SEL sel = NSSelectorFromString(method);// method = “call” 或  ”openCamera“
        [self performSelector:sel withObject:nil];
    }
    return YES;
}

/**
 *  打电话
 */
- (void)call
{
    NSLog(@"call----");
}

/**
 *  打开照相机
 */
- (void)openCamera
{
    NSLog(@"openCamera----");
}

@end

 

IOS网络第七天WebView-03js中调用webView中的代码

标签:

原文地址:http://www.cnblogs.com/ios-g/p/4809387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!