UIWebView设置Cookie标签

作者: iOS乐乐 | 来源:发表于2017-07-27 10:48 被阅读0次
    //拦截加载UIWebView中的Alert中message的事件
    @interface UIWebView (JavaScriptAlert)
    
    - (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(UIWebView *)frame;
    
    @end
    
    @implementation UIWebView (JavaScriptAlert)
    
    - (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(UIWebView *)frame {
    
        NSLog(@"message = %@",message);
        if ([message isEqualToString:@"no_login"])
        {
             //没有登录
            [[NSNotificationCenter defaultCenter] postNotificationName:@"dengluAction" object:nil];
    
            UIAlertView* customAlert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            customAlert.tag = 100;
            [customAlert show];
    
        }
        if ([message isEqualToString:@"no_realname"])
        {
             //没有开通存管
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kaitongcunguan" object:nil];
    
        }
    
    }
    
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (alertView.tag == 100)
        {
            NSLog(@"denglu");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"dengluAction" object:nil];
        }
    }
    
    @end
    
    //我再这里设置的是关于cookie的标签
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.    
     NSString*_urlstr=@"http://www.upstudio.top/upwork_activity/resource/2017/0621/index.html";
        
        NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        /**这个是清楚cookie暂时未用**/
        NSArray *cookieAry = [cookieJar cookiesForURL: [NSURL URLWithString: _urlstr]];
        for (NSHTTPCookie*cookie in cookieAry) {
            DLog(@"%@",cookie);
        }
        DLog(@"数量--->%lu",(unsigned long)cookieAry.count);
        
        self.title = @"活动";
        
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-100)];
        webView.delegate = self;
        webView.backgroundColor = [UIColor whiteColor];
        webView.scrollView.showsVerticalScrollIndicator = NO;
        [webView setScalesPageToFit:YES];
    
        
        NSURL* url = [NSURL URLWithString:_urlstr];
        NSURL *cookieHost = [NSURL URLWithString:_urlstr];
        NSDictionary *dic = @{@"phone":@"18032293220",@"userId":@"10008",@"isRealName":@"1"};
        [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            // 设定 cookie (经过测试每次只能设置一个值所以需要for循环的形式进行重复的创建这样问题就解决了)
            NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:
                                    [NSDictionary dictionaryWithObjectsAndKeys:
                                     [cookieHost host], NSHTTPCookieDomain,
                                     [cookieHost path], NSHTTPCookiePath,
                                     key,NSHTTPCookieName,
                                     obj,NSHTTPCookieValue,
                                     nil]];
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
            
        }];
        
        
        
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
        
        [webView loadRequest:request];
        
        [self.view addSubview:webView];
        
        
        UIButton*button=[[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight-100, ScreenWidth, 100)];
        [button setTitle:@"按钮" forState:UIControlStateNormal];
        button.backgroundColor=[UIColor redColor];
        [button addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.view addSubview:button];
        
        
        
        
    }
    
    -(void)btnClicked
    {
        
        [webView reload];
        
    }
    
    
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    {
        
        NSString * urlDtr =  [[request URL] absoluteString];
        
        NSLog(@"aaaaaaaaaurlDtr = %@",urlDtr);
        
        
        return YES;
        
    }
    
    
    -(void)webViewDidFinishLoad:(UIWebView *)webView
    {
        
        NSArray *cookieAry = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
        
        DLog(@"%@",cookieAry);
        
        for (NSHTTPCookie *cookie in cookieAry)
        {
            
            
        }
        
    }
    
    

    相关文章

      网友评论

        本文标题:UIWebView设置Cookie标签

        本文链接:https://www.haomeiwen.com/subject/ojfzkxtx.html