#import <UIKit/UIKit.h>
#import <JavaScriptCore/JavaScriptCore.h>
@protocol PersonCenterJsObjectProtocol <JSExport>
//使用JSExportAs宏,可以将方法名简化
JSExportAs(toShareDialog,
- (void)toShareDialog:(NSString *)shareTitle :(NSString *)shareUrl :(NSString *)shareContent
);
- (void)toHomePage;
@end
@interface FFEasyLifeRecommendCtrl : UIViewController <UIWebViewDelegate ,PersonCenterJsObjectProtocol>
@property (nonatomic, copy) NSString *webUrl;
@property (nonatomic, copy) NSString *webTitle;
@end
@interface FFEasyLifeRecommendCtrl ()
@property (nonatomic, strong) JSContext *context;
@property (nonatomic, strong) JSManagedValue *managedPlugin;
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) FFEasyLifeShareView *shareWindow;
@end
@implementation FFEasyLifeRecommendCtrl
#pragma mark - LifeCycle
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self unloadColorsPlugin];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.automaticallyAdjustsScrollViewInsets = NO;
[self.view addSubview:self.webView];
[self initWebViewDataWithPath:nil];
}
#pragma mark - 建立js环境
-(void)makeJsEnvirement {
JSContext *context = [_webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[@"dmc"] = self;
_context = context;
self.context.exceptionHandler = ^(JSContext *context, JSValue *exception) {
NSLog(@"异常信息:%@",exception);
context.exception = exception;
};
}
- (void)unloadColorsPlugin {
[_context.virtualMachine removeManagedReference:_managedPlugin withOwner:self];
_managedPlugin = nil;
_context = nil;
}
#pragma mark - PersonCenterJsObjectProtocol
- (void)toShareDialog:(NSString *)shareTitle :(NSString *)shareUrl :(NSString *)shareContent {
dispatch_async(dispatch_get_main_queue(), ^{
self.shareWindow = [[FFEasyLifeShareView alloc] initShareView];
[self.shareWindow shareWithShareTitle:shareTitle Text:shareContent shareImage:[UIImage imageNamed:@"share_recommend"] webUrl:shareUrl delegate:self];
[self.view addSubview:self.shareWindow];
});
}
- (void)toHomePage {
dispatch_async(dispatch_get_main_queue(), ^{
[self.navigationController popToRootViewControllerAnimated:NO];
});
}
#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if([request isKindOfClass:[NSMutableURLRequest class]]) {
}
return true;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
self.webView.userInteractionEnabled = NO;
NSLog(@"title11=%@",title);
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self makeJsEnvirement];
self.webView.userInteractionEnabled = YES;
self.loading.hidden = YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
self.loading.hidden = YES;
self.noDataView.hidden = NO;
NSLog(@"%@",error.description);
}
@end
二
#import <JavaScriptCore/JavaScriptCore.h>
@protocol WebDetailJsObjectProtocol <JSExport>
JSExportAs(toGoodsDetail,
- (void)toGoodsDetail:(NSString *)Goods :(NSString *)callback
);
- (void)goAppHome;
@end
#pragma mark - 建立js环境
-(void)makeJsEnvirement {
JSContext *context = [_webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[@"dmc"] = self;
_context = context;
self.context.exceptionHandler = ^(JSContext *context, JSValue *exception) {
NSLog(@"异常信息:%@",exception);
context.exception = exception;
};
}
- (void)unloadColorsPlugin {
[_context.virtualMachine removeManagedReference:_managedPlugin withOwner:self];
_managedPlugin = nil;
_context = nil;
}
#pragma mark -
#pragma mark - JSObjcDelegate
- (void)toGoodsDetail:(NSString *)Goods :(NSString *)callback{
NSData *goodsData = [Goods dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:goodsData
options:NSJSONReadingMutableContainers
error:&err];
if ([dic isKindOfClass:[NSDictionary class]]) {
FFEasyLifeSimilarRCModel *model = [[FFEasyLifeSimilarRCModel alloc] initWithDic:dic];
dispatch_async(dispatch_get_main_queue(), ^{
FFEasyLifeActivityDetailCtrl *ctrl = [[FFEasyLifeActivityDetailCtrl alloc] initWithNibName:kFFEasyLifeActivityDetailCtrl bundle:nil];
ctrl.idx = [NSString limitStringNotEmpty:model.goodsId];
ctrl.activityId = [NSString limitStringNotEmpty:model.activityId];
ctrl.actType = [NSString limitStringNotEmpty:model.type];
if (self.navigationController) {
[self.navigationController pushViewController:ctrl animated:YES];
} else {
ctrl.isInform = YES;
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:ctrl];
nav.navigationBarHidden = YES;
[self presentViewController:nav animated:YES completion:nil];
}
});
}
}
- (void)goAppHome {
dispatch_async(dispatch_get_main_queue(), ^{
if (self.navigationController) {
[self.navigationController popToRootViewControllerAnimated:YES];
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
});
}