webView上面获取打开链接的方式

根据url的scheme通过相应的标识获取url的绝对路径

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"request.scheme:%@,navigationtype:%d",[request URL].scheme,navigationType);
    if ([[request URL].scheme isEqualToString:@"outerurl"] && navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSString* urlAbosute = [[[request URL] absoluteString] stringByReplacingOccurrencesOfString:@"outerurl:" withString:@""];
        NSString *urlstr=[NSString stringWithFormat:@"https://%@",urlAbosute];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlstr]];
    }
    if ([[request URL].scheme isEqualToString:@"qq"] && navigationType == UIWebViewNavigationTypeLinkClicked) {
        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
        /*取qq号码*/
        NSLog(@"qqrequest:%@",[[request URL]absoluteString]);
        NSRange range=[[[request URL]absoluteString] rangeOfString:@"uin="];
        NSString *qqNumber=[[[request URL]absoluteString] substringFromIndex:range.location+range.length];
        /*打开QQ链接*/
        NSString *qqstr=[NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web",qqNumber];
        NSURL *url = [NSURL URLWithString:qqstr];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        webView.delegate = self;
        [webView loadRequest:request];
        [self.view addSubview:webView];
    }
    return YES;
}

你可能感兴趣的:(webView上面获取打开链接的方式)