webView2



#import "ActivityWebViewController.h"

@interface ActivityWebViewController ()

@end

@implementation ActivityWebViewController

- (id) initWithLoadingMethod:(NSString *)method

                      userId:(id) userId

                      shopId:(id)shopId

                  activityId:(id)activityId

                   productId:(id)productId

                   presentid:(id)presentId

               suitPresentid:(id)suitPresentId{


    if (self = [super init]) {
        
    
        _loadingMethod = method;
       
        _activityId = activityId;
        
        
        
        if (userId) {
            _userId = userId;
        }else{
            
            _userId = @"0";
        }
        
        if (_shopId) {
            _shopId = shopId;
        }else{
        
        _shopId = @"0";
        }
        if (_productId ) {
            _productId = productId;
        }else{
            
         _productId = @"0";
        }
        
        if (_presentId) {
            _presentId = presentId;
        }else{
        
        _presentId = @"0";
        }
        if (_suitPresentId) {
            _suitPresentId = suitPresentId;
        }else{
        
        _suitPresentId = @"0";
        }
        
        
    }
   

    return self;


}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavTitle];
    [self initWebView];
    
    [self getTMLParameter];
    [self addJavaScript];
    [self loadHTMLFile];
    
}
//初始化导航条
- (void) initNavTitle{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 17)];
    titleLabel.text = self.navTitle;
    titleLabel.font = [UIFont fontWithName:FONT_FZLTH size:17];
    titleLabel.textAlignment = 1;
    titleLabel.textColor = [UIColor colorWithRed:50/255.0 green:39/255.0 blue:23/255.0 alpha:1.0];
    self.navigationItem.titleView = titleLabel;
 

}
//初始化WebView
- (void) initWebView{

    self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    self.webView.backgroundColor = [UIColor clearColor];
    self.webView.delegate = self;
    [self.view addSubview:_webView];
    


}

- (void) loadHTMLFile{
    NSString *htmlPath = [_htmlPath stringByAppendingPathComponent:@"active/info.html"];
    NSLog(@"%@",htmlPath);
    NSString *htmlStr = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
    [_webView loadHTMLString:htmlStr baseURL:[NSURL URLWithString:htmlPath]];
 
}
//在本地沙盒存储的路径
-(void) getTMLParameter{
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"htmlDic"];
    NSString *directoryName = dic[@"name"];
    _htmlPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/htmlContent/%@",directoryName]];
 
    LocalData *data=[LocalData shareInstance];
    self.userId=data.memberId;
    if (!self.userId) {
        self.userId=@"0";
    }

}
- (void)addJavaScript{


    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
                                                          "script.type = 'text/javascript';"
                                                          "script.text = \"function getIOSIdentify() { "//定义myFunction方法
                                                          "return  '%@';"
                                                          "}\";"
                                                          "document.getElementsByTagName('head')[0].appendChild(script);",@true]];
    
    
    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
                                                          "script.type = 'text/javascript';"
                                                          "script.text = \"function getLoadingMehthod() { "//定义myFunction方法
                                                          "return  '%@';"
                                                          "}\";"
                                                          "document.getElementsByTagName('head')[0].appendChild(script);",@"local"]];
    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
                                                          "script.type = 'text/javascript';"
                                                          "script.text = \"function getUserId() { "//定义myFunction方法
                                                          "return  '%@';"
                                                          "}\";"
                                                          "document.getElementsByTagName('head')[0].appendChild(script);",_userId]];
    
    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
                                                          "script.type = 'text/javascript';"
                                                          "script.text = \"function getShopId() { "//定义myFunction方法
                                                          "return  '%@';"
                                                          "}\";"
                                                          "document.getElementsByTagName('head')[0].appendChild(script);",_shopId]];
    
    
    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
                                                          "script.type = 'text/javascript';"
                                                          "script.text = \"function getProductId() { "//定义myFunction方法
                                                          "return  '%@';"
                                                          "}\";"
                                                          "document.getElementsByTagName('head')[0].appendChild(script);",_productId]];
    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
                                                          "script.type = 'text/javascript';"
                                                          "script.text = \"function getPresentId() { "//定义myFunction方法
                                                          "return  '%@';"
                                                          "}\";"
                                                          "document.getElementsByTagName('head')[0].appendChild(script);",_presentId]];
    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
                                                          "script.type = 'text/javascript';"
                                                          "script.text = \"function getSuitPresentId() { "//定义myFunction方法
                                                          "return  '%@';"
                                                          "}\";"
                                                          "document.getElementsByTagName('head')[0].appendChild(script);",_suitPresentId]];
    
    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
                                                          "script.type = 'text/javascript';"
                                                          "script.text = \"function getHtmlPath() { "//定义myFunction方法
                                                          "return  '%@';"
                                                          "}\";"
                                                          "document.getElementsByTagName('head')[0].appendChild(script);",_htmlPath]];
 
    
    
    //add
    [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
                                                          "script.type = 'text/javascript';"
                                                          "script.text = \"function getActivityId() { "//定义myFunction方法
                                                          "return  '%@';"
                                                          "}\";"
                                                          "document.getElementsByTagName('head')[0].appendChild(script);",_activityId]];




}

- (void)showShareBtn{


    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    shareBtn.frame = CGRectMake(0, 0, 35.0*SCREEN_WIDTH / 720, 35 * SCREEN_WIDTH/720);
    [shareBtn addTarget:self action:@selector(shareBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [shareBtn setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    UIBarButtonItem *shareitem = [[UIBarButtonItem alloc] initWithCustomView:shareBtn];
    self.navigationItem.rightBarButtonItem = shareitem;

    
}

- (void)shareBtnClicked:(UIButton *)btn{
    

    NSLog(@"分享按钮被点击");



}

//实现webView的代理方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    NSString *requestAbsStr = request.URL.absoluteString;
    NSString *decodingStr = [requestAbsStr stringByRemovingPercentEncoding];
    if ([decodingStr hasPrefix:@"mobcbiosclient"]) {
        NSArray *strArray=[decodingStr componentsSeparatedByString:@"//"];
        NSString *mainStr=[strArray lastObject];
        if ([mainStr hasPrefix:@"showShareBtn"]) {
            [self showShareBtn];
        }
        else if([mainStr hasPrefix:@"shopGoods"])
        {
//            ProductInShopViewController *goodsVC=[[ProductInShopViewController  alloc]init];
//            goodsVC.shopId=self.shopId;
//            goodsVC.titleStr=@"商品";
//            [self.navigationController pushViewController:goodsVC animated:YES];
            
        }
        
        else if ([mainStr hasPrefix:@"share"])
        {
            
        }
        else if([mainStr hasPrefix:@"shopTelePhone"])
        {
            
            
        }
        else if([mainStr hasPrefix:@"shopAddress"])
        {
            
            
        }
        else if([mainStr hasPrefix:@"shopGoods"])
        {
            
            
        }
        else if([mainStr hasPrefix:@"shopCoupons"])
        {
            
            
        }
        else if([mainStr hasPrefix:@"shopActivitys"])
        {
            
            
            
        }
    }
    
    





    return YES;
}
@end

你可能感兴趣的:(webView2)