1,在webView 的代理方法中截取指定的URL。
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{
NSURL*url=[requestURL];
NSString*strURL=[[urlabsoluteString]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@-------------%@",url,strURL);
//判断strURL
[selfloadMoreComment:webViewstrURL:strURL];
return YES;
}
2,根据截取的URL判断URL中指定的特殊字段,去处理对应的事件。
- (void)loadMoreComment:(UIWebView*)webView strURL:(NSString*)strURL {
//根据返回的url,判断这个url是头条评论详情的url,还是资深评论详情的url,防止资讯详情页面加载评论详情页面的web页
self.stringArrr= [strURLcomponentsSeparatedByString:@"?"];
if([self.stringArrr[0]rangeOfString:@"consult/allcomments"].location!=NSNotFound) {
// do something
}elseif([self.stringArrr[0]rangeOfString:@"Information/deepComments"].location!=NSNotFound){
// do something
}elseif([self.stringArrr[0]rangeOfString:@"Information/deepinformationDetail"].location!=NSNotFound){
// do something
}elseif([self.stringArrr[0]rangeOfString:@"HeadLines/detail"].location!=NSNotFound){
// do something
}else{
}
}
3,将Native数据传给JS端。
#pragma mark -评论过后将用户的数据传给web端
- (void)setUpCommentAfterGiveWeb{
//用户
MemberObject*member = XXXX;
//用户头像
NSString*replyhead = [XLArchiverHelpergetObject:@"headImageMine"];
NSMutableDictionary*dict2 = [NSMutableDictionarydictionary];
[dict2 setObject:member.userNameforKey:@"xxxx"];
[dict2 setObject:replyheadforKey:@"xxxx"];
//评论的id
[dict2setObject:self.replyIDforKey:@"xxxx"];
[dict2setObject:self.KBInputTextforKey:@"xxxx"];
NSString*data = [dict2JSONString];
NSString*addComment = [NSStringstringWithFormat:@"addComment(%@)",data];
NSLog(@"^^^^^^^^^^^^^^^^^^^^^%@",addComment);
[self.commentWebViewstringByEvaluatingJavaScriptFromString:addComment];
}
4,总结
这是JS与Native是最简单的也是最基础的交互操作,几乎可以满足我们OC与JS的交互索要做到事情。