iOS HTML5的JS交互
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS HTML5的JS交互相关的知识,希望对你有一定的参考价值。
//webView与H5点击交互走的代理
-(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];
[email protected]"绑定手机";
[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];
本文出自 “ZhuoKing” 博客,请务必保留此出处http://9951038.blog.51cto.com/9941038/1842724
以上是关于iOS HTML5的JS交互的主要内容,如果未能解决你的问题,请参考以下文章
iOS JS 和 OC交互 / JS 和 native 相互调用
iOS的webview和JavaScript的交互方法,求个小Demo,
atitit.js 与c# java交互html5化的原理与总结.doc
借助HTML5 details,summary无JS实现各种交互效果