网络,本地策略(包括缓存,持久化,ORM等等)

IOS cookie写入问题(花椒H5账号打通)

2017-05-10  本文已影响10人  蜗牛1992
//关于UIWebview的cookie写入问题,仅供参考,花椒账号打通需要服务器返回token和uid,需要写入两个cookie,比较蛋疼。这个只是个参考

#import "zhiboCollectionViewController.h"
#import "HJCline.h"

@interface zhiboCollectionViewController ()<UIWebViewDelegate,UIScrollViewDelegate>
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic ,assign)BOOL goBack;
@end

@implementation zhiboCollectionViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"直播大厅";
    UIButton *returnBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 26)];
    [returnBtn setTitle:@"返回" forState:UIControlStateNormal];
    returnBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    [returnBtn addTarget:self action:@selector(goToHomeView) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *returnBtnItem = [[UIBarButtonItem alloc]initWithCustomView:returnBtn];
    self.navigationItem.leftBarButtonItem = returnBtnItem;
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,W,H - 64)];
    [self.view addSubview:_webView];
    _webView.delegate = self;
    _webView.scrollView.delegate = self;
    _webView.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getHuaJiaoCookie];
        [_webView.scrollView.mj_header endRefreshing];
    }];

    _webView.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self getHuaJiaoCookie];
    //后台返回前台
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getHuaJiaoCookie) name:@"HUAJIAOCOOKIEUPDATE" object:nil];
}
-(void)getHuaJiaoCookie{
    [[HJCline sharedClient] sendHuaJiaoWithcustId:[UserDefaults customerID] custCode:[UserDefaults user] success:^(HJObject *dataObject) {
        NSString *h5_token = dataObject.res.h5_token;
        NSString *h5_uid = dataObject.res.uid;
//        YQLLog(@"cookie后台新请求下来的====token=%@\n",h5_token);
        if (h5_uid.length > 0 && h5_uid.length > 0) {
            [self setCookieWithToken:h5_token uid:h5_uid];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

-(void)setCookieWithToken:(NSString *)token uid:(NSString *)uid
{
    //清除cookie
    NSURL *url = [NSURL URLWithString:@"http://h.open.huajiao.com?channelid=wowifi"];
    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
    for (NSHTTPCookie *cookie in cookies) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
    
    NSHTTPCookie *cookieToken = [NSHTTPCookie cookieWithProperties:@{NSHTTPCookieName:@"h5_token",
                                                                     NSHTTPCookieValue:token,
                                                                     NSHTTPCookiePath:@"/",
                                                                     NSHTTPCookieDomain:@".huajiao.com",
                                                                     NSHTTPCookieOriginURL:@".huajiao.com",
                                                                     NSHTTPCookieExpires:[NSDate dateWithTimeIntervalSinceNow:60*60*24*7]
                                                                     }];
    NSHTTPCookie *cookieUseId = [NSHTTPCookie cookieWithProperties:@{NSHTTPCookieName:@"h5_userid",
                                                                     NSHTTPCookieValue:uid,
                                                                     NSHTTPCookiePath:@"/",
                                                                     NSHTTPCookieDomain:@".huajiao.com",
                                                                     NSHTTPCookieOriginURL:@".huajiao.com",
                                                                     NSHTTPCookieExpires:[NSDate dateWithTimeIntervalSinceNow:60*60*24*7]
                                                                     }];
//    YQLLog(@"cookiesetCookieWith新的token=%@\n===%@",cookieToken,cookieUseId);
    NSArray *cookiesArray=[NSArray arrayWithObjects:cookieToken,cookieUseId,nil];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookiesArray forURL:url mainDocumentURL:nil];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    //    printf("cookie=======222222222222222222222=========");
}


-(void)goToHomeView{
    if (self.goBack==YES) {
        self.goBack=NO;
        [_webView goBack];
    }else{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGEVCINTABBAR4" object:nil];
}
}
#pragma mark WebViewDelegate======
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [SVProgressHUD show];
//屏蔽花椒H5里面的我的界面注销按钮
    // 1.获取页面标题
    NSString *string = @"document.title";
    [webView stringByEvaluatingJavaScriptFromString:string];
    // 2.去掉页面标题
    NSMutableString *str = [NSMutableString string];
    // 5.去掉最底下的一块区域
    [str appendString:@"var btn = document.getElementsByClassName(\"top_bar\")[0];"];
    [str appendString:@"btn.parentNode.removeChild(btn);"];
    
    [webView stringByEvaluatingJavaScriptFromString:str];
    [str appendString:@"var btn = document.getElementsByClassName(\"top_bar\")[1];"];
    [str appendString:@"btn.parentNode.removeChild(btn);"];
    
    [webView stringByEvaluatingJavaScriptFromString:str];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
//屏蔽花椒H5里面的我的界面注销按钮
    // 1.获取页面标题
    NSString *string = @"document.title";
    [webView stringByEvaluatingJavaScriptFromString:string];
    // 2.去掉页面标题
    NSMutableString *str = [NSMutableString string];
    // 5.去掉最底下的一块区域
    [str appendString:@"var btn = document.getElementsByClassName(\"top_bar\")[0];"];
    [str appendString:@"btn.parentNode.removeChild(btn);"];
    
    [webView stringByEvaluatingJavaScriptFromString:str];
    [str appendString:@"var btn = document.getElementsByClassName(\"top_bar\")[1];"];
    [str appendString:@"btn.parentNode.removeChild(btn);"];
    
    [webView stringByEvaluatingJavaScriptFromString:str];
    [SVProgressHUD dismiss];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString *currentUrl = [NSString stringWithFormat:@"%@",request.URL];
//    YQLLog(@"urll===%@",currentUrl);
    if ([currentUrl containsString:@"liveid="]) {
        [self hideExistingTabBar:YES];
        [self.navigationController setNavigationBarHidden:YES animated:YES];
//        self.view.backgroundColor = [Utils GetColor:@"ef8834"];
        _webView.frame = CGRectMake(0,0,W,H);
        //        NSLog(@"urll点击界面二级界面进入直播间");
    }else if([currentUrl containsString:@"http://h.open.huajiao.com/economics/recharge?"]){
        self.goBack=YES;
    }else{
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        _webView.frame = CGRectMake(0, 0,W,H - 64);
        //        NSLog(@"urll直播的第一级主界面直播间之外的界面");
    }
     return YES;
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [SVProgressHUD dismiss];
}
上一篇下一篇

猜你喜欢

热点阅读