项目里涉及到JS和OC的交互,之前使用的是UIWebView加载的网页,只有JS调用OC,所以这里就总结一下这两天的测试,MARK一下,下一步打算使用WKWebView:
3种方式:
1)通过拦截网址的方法,网页加载过程中就可以进行交互
2)方法拦截,只有网页加载完毕才可以进行交互
3)JavaScriptCore ,同样只有网页加载完毕才可以交互。
网址拦截
使用到的代理方法为
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString *urlStr = [request.URL absoluteString];
//这里对网址进行判断 返回YES 或NO
return YES;
}
方法拦截
贴出部分代码:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//禁用长按触控操作
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
//禁止用户选择页面元素
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"===URL:>%@>>>>>>>title:%@",currentURL,title);
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
__weak typeof(&*self) weakSelf = self;
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context.exceptionHandler=^(JSContext *context, JSValue *exception){
HHLog(@"JS Error :%@",exception);
[weakSelf setMyAlertViewWithMessage:exception.context.name];
};
context[@"productDetail"] = ^{//商品详情
HHLog(@"商品详情");
NSArray *args = [JSContext currentArguments];
NSInteger productID = [[[args firstObject] toNumber] integerValue];
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf goToGoodsDetailVCWithGoodsID:productID];
});
};
}
JavaScriptCore
1.导入系统的库
#import <JavaScriptCore/JavaScriptCore.h>
2.创建了一个JS与OC交互的模型类
贴上模型类里.h文件的代码
#import <Foundation/Foundation.h>
@protocol HHJSObjcModelDelegate <JSExport>
//商品列表
- (void)category:(NSString *)paramsStr;
//商品详情
- (void)productDetail:(NSString *)goodsIDStr;
//品牌分类
- (void)cateogrymore;
//活动类型
- (void)activity:(NSString *)paramsStr;
@end
@interface HHJSObjcModel : NSObject<HHJSObjcModelDelegate>
@property (nonatomic, strong) JSContext *jsContext;
@property (nonatomic, weak) UIWebView *webView;
@property (nonatomic, weak) UIViewController *webVC;
@end
3.在viewController里的代码
__weak typeof(&*self) weakSelf = self;
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
HHJSObjcModel *model = [[HHJSObjcModel alloc]init];
context[@"android"] = model;
model.jsContext = context;
model.webView = self.webView;
model.webVC = self;
context.exceptionHandler=^(JSContext *context, JSValue *exception){
HHLog(@"JS Error :%@",exception);
[weakSelf setMyAlertViewWithMessage:exception.context.name];
};
使用UIWebView加载网页,多次刷新 内存都在不断的上涨,即使是采用网上的方法设置缓存策略 清理URLCache,问题依然无法得到解决.
[[NSURLCache sharedURLCache] setMemoryCapacity:1024*1024*2];
if (self.webRequest) {
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:self.webRequest];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
if (self.webRequest == nil) {
self.webRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:3];
// [self.webRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
}
[self.webView loadRequest:self.webRequest];
网友评论