您现在的位置是:首页 > 文章详情

iOS HTML5的JS交互

日期:2017-11-20点击:436

 //webViewH5点击交互走的代理  

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

    NSURL * url = [request URL];

    NSString * urlStr = url.absoluteString;

    NSString * protocolPrefix = @"scheme";//和后台定义好的字符  


    [self H5Js:protocolPrefix url:urlStr];


    return YES;

}


//数组分离参数  开始进行关键字提取以及相关操作

-(void)H5Js:(NSString *)protocolPrefix url:(NSString *)urlStr

{

    if ([[urlStr lowercaseString]hasPrefix:protocolPrefix])

    {

        urlStr = [urlStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        NSArray * components = [urlStr componentsSeparatedByString:@":"];//":"截取字符


        NSArray * shareUrl = [urlStr componentsSeparatedByString:@"?"];




        

//        NSLog(@"%@",components);


        NSString * prefix = components[1];


        if ([prefix isEqualToString:@"awardLogin"])

        {

            isReash = YES;

            [self jumpLogin];

        }

        if ([prefix isEqualToString:@"awardBindMobile"])

        {

            //绑定手机

            ModifyPhoneController * phone = [[ModifyPhoneController alloc]init];

                phone.title=@"绑定手机";

            [self.navigationController pushViewController:phone animated:YES];


        }


        if([prefix rangeOfString:@"awardShare"].location !=NSNotFound)

        {

            NSRange range1 = [urlStr rangeOfString:@"?"];//匹配得到的下标

            //        NSLog(@"rang:%@",NSStringFromRange(range));

            NSString *rangeStr =[urlStr substringFromIndex:range1.location+1];

            NSArray * parameter =  [rangeStr componentsSeparatedByString:@"&"];

            NSLog(@"%@",parameter);

            [dic setObject:[parameter[1] componentsSeparatedByString:@"="][1]  forKey:@"shareTitle"];

            [dic setObject:[parameter[2] componentsSeparatedByString:@"="][1] forKey:@"shareContent"];

            [dic setObject:[parameter[0] componentsSeparatedByString:@"="][1] forKey:@"shareImg"];

            [dic setObject:[parameter[3] componentsSeparatedByString:@"="][1] forKey:@"shareUrl"];


            NSRange range = [parameter[4] rangeOfString:@"="];//匹配得到的下标

            NSLog(@"rang:%@",NSStringFromRange(range));

            NSString *rangeStr1 =[parameter[4] substringFromIndex:range.location+1];


            [dic setObject:rangeStr1 forKey:@"smsContent"];


             [self share:nil];

        }

    }

}



//回调JS代码

 NSString * str =[NSString stringWithFormat:@"lsf_login_callback(%@)",[User shareUser].userId];

        //评论完后刷新html5 页面

        [self.webView stringByEvaluatingJavaScriptFromString:str];











本文转自 卓行天下  51CTO博客,原文链接:http://blog.51cto.com/9951038/1842724,如需转载请自行联系原作者
原文链接:https://yq.aliyun.com/articles/555667
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章