美文网首页iOS 菜鸟之旅
基于UIWebView保存和获取cookies

基于UIWebView保存和获取cookies

作者: 涛涛灬灬 | 来源:发表于2017-09-19 16:36 被阅读46次

    网上百度了好多方法,都没有达到自己需要的,最终还是自己搞出来,只要针对保存taoBao的cookies;

    /* 在加载webView的时候 将cookie设置到url中

    • (void)loadWebView {
      NSString *urlString = @"******";

      NSURL * url = [NSURL URLWithString:urlString];

      //获取本地保存的 cookies
      [self getCookiesForUrlWithRequest:url];

      NSURLRequest * request = [NSURLRequest requestWithURL:url];

      [self.webView loadRequest:request];

    }

    • (void)getCookiesForUrlWithRequest:(NSURL * )url {
      NSMutableArray * cookiesLocation = [NSMutableArray arrayWithCapacity:10];
      NSArray *cookDics =[[NSUserDefaults standardUserDefaults] objectForKey:@"cookies"];
      for (NSDictionary *cookies in cookDics ){
      if ([cookies isKindOfClass:[NSDictionary class]]) {
      NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
      [cookieProperties setObject:[NSString stringWithFormat:@"%@",cookies[@"name"]] forKey:NSHTTPCookieName];
      [cookieProperties setObject:[NSString stringWithFormat:@"%@",cookies[@"version"]] forKey:NSHTTPCookieVersion];
      [cookieProperties setObject:[NSString stringWithFormat:@"%@",cookies[@"value"]] forKey:NSHTTPCookieValue];
      [cookieProperties setObject:[NSString stringWithFormat:@"%@",cookies[@"expiresDate"]] forKey:NSHTTPCookieExpires];
      [cookieProperties setObject:[NSString stringWithFormat:@"%@",cookies[@"domain"]] forKey:NSHTTPCookieDomain];
      [cookieProperties setObject:[NSString stringWithFormat:@"%@",cookies[@"path"]] forKey:NSHTTPCookiePath];
      [cookieProperties setObject:[NSString stringWithFormat:@"%@",cookies[@"isSecure"]] forKey:NSHTTPCookieSecure];

          //  将字典转化为cookie
            NSHTTPCookie *cookieuser = [NSHTTPCookie cookieWithProperties:cookieProperties];
            [cookiesLocation addObject:cookieuser];
        }
      

      }
      [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookiesLocation forURL:url mainDocumentURL:NSHTTPCookieAcceptPolicyAlways];
      }
      /// mainDocumentUR 枚举 : NSHTTPCookieAcceptPolicyAlways, NSHTTPCookieAcceptPolicyNever, NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain
      解释参考:http://blog.sina.com.cn/s/blog_4cd8dd130100tsme.html

    //在登录成功之后 保存登录信息 cookies

    • (void)saveCookies:(NSURL )url {
      NSHTTPCookieStorage * nCookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
      NSArray
      cookiesURL = [nCookies cookiesForURL:url];

      NSMutableArray *saveCookieAry = [NSMutableArray arrayWithCapacity:10];
      NSHTTPCookie *cookie;
      for (id c in cookiesURL)
      {
      if ([c isKindOfClass:[NSHTTPCookie class]])
      {
      cookie=(NSHTTPCookie *)c;
      NSNumber *sessionOnly = [NSNumber numberWithBool:cookie.sessionOnly];
      NSNumber *isSecure = [NSNumber numberWithBool:cookie.isSecure];
      ///筛选出需要使用的参数进行保存
      NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:6];
      [dic setObject:[NSString stringWithFormat:@"%@",cookie.name] forKey:@"name"];
      [dic setObject:[NSString stringWithFormat:@"%lu",(unsigned long)cookie.version] forKey:@"version"];
      [dic setObject:[NSString stringWithFormat:@"%@",cookie.value] forKey:@"value"];
      [dic setObject:[NSString stringWithFormat:@"%@",cookie.expiresDate] forKey:@"expiresDate"];
      [dic setObject:[NSString stringWithFormat:@"%@",sessionOnly] forKey:@"sessionOnly"];
      [dic setObject:[NSString stringWithFormat:@"%@",cookie.domain] forKey:@"domain"];
      [dic setObject:[NSString stringWithFormat:@"%@",cookie.path] forKey:@"path"];
      [dic setObject:[NSString stringWithFormat:@"%@",isSecure] forKey:@"isSecure"];
      [saveCookieAry addObject:dic];
      }
      }

      [[NSUserDefaults standardUserDefaults] setObject:saveCookieAry forKey:@"cookies"];
      }

    相关文章

      网友评论

        本文标题:基于UIWebView保存和获取cookies

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