美文网首页
ios webview 加载链接携带cookie

ios webview 加载链接携带cookie

作者: 汤昊 | 来源:发表于2017-10-22 09:37 被阅读15次

    ios 中有的请求需要带cookie,  验证登陆等. 

     ios默认是会把url 的cookie保存到本地, 但不是实时保存的, 会有延迟, 一般理解为在系统空闲时去保存 .  所以为了实时给url添加cookie, 我们最好将请求url 后获取到的cookie 保存到本地, 用NSUserDefault即可

    在设置Cookie之前,先让我们了解一下通用Cookie格式,多个Cookie的格式通常为:cookie1=value1; cookie2=value2; cookie3=value3;

    这里特别要注意,多个cookie之间用分号+空格分隔开,不是&也不是单纯的空格。我所了解到的iOS设置Cookie的方法有两种,分别如下。

    方法一:

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",YourURL]]];

    NSMutableString *cookieString = [[NSMutableString alloc]init];

    [cookieString appendFormat:@"userid=%@;",@"xxxx"];

    [cookieString appendFormat:@"sessionid=%@;",@"xxxx"];

    [cookieString appendFormat:@"nickname=%@;",@"xxxx"];

    [req setValue:cookieString forHTTPHeaderField:@"Cookie"];

    方法二:

    NSDictionary *properties1 = [NSDictionary dictionaryWithObjectsAndKeys:

    @"domain.com", NSHTTPCookieDomain,

    @"/", NSHTTPCookiePath,

    @"userid", NSHTTPCookieName,

    @"tangtang", NSHTTPCookieValue, nil];

    NSDictionary *properties2 = [NSDictionary dictionaryWithObjectsAndKeys:

    @"domain.com", NSHTTPCookieDomain,

    @"/", NSHTTPCookiePath,

    @"pid", NSHTTPCookieName,

    @"xiaotang", NSHTTPCookieValue,

    nil];

    NSHTTPCookie *cookie1 = [NSHTTPCookie cookieWithProperties:properties1];

    NSHTTPCookie *cookie2 = [NSHTTPCookie cookieWithProperties:properties2];

    NSArray* cookies = [NSArray arrayWithObjects:cookie1, cookie2, nil];

    NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];

    request.allHTTPHeaderFields = headers; //这句才是关键

    注意: 一定要把cookie设置到request.allHTTPHeaderFields中才会生效.

    网上好多的demo都是这样设置cookie的 ---   [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];         亲测不能用


    update  于当天下午

    现在是这样, 要在每一次请求的时候都要带cookie, 如果要按上面那种写法之有在第一次请求的时候带cookie, 其他请求都带不上了,  

    解决方法 :

    -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

    {

    NSString *urlString = [[request URL] absoluteString];

    NSLog(@"urlString = %@",urlString);

    NSMutableURLRequest *mutableRequest = [request mutableCopy];

    NSDictionary *requestHeaders = request.allHTTPHeaderFields;

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    // 判断请求头是否已包含,如果不判断该字段会导致webview加载时死循环

    if (!requestHeaders[@"Cookie"]) {

    NSString *ticket = [userDefaults objectForKey:MMH_USER_DEFAULT_USER_TICKET];

    NSMutableString *cookieString = [[NSMutableString alloc]init];

    [cookieString appendFormat:@"ESHOP_H5_T=%@;",ticket];

    [mutableRequest setValue:cookieString forHTTPHeaderField:@"Cookie"];

    NSLog(@"ticket  ==  %@", ticket);

    request = [mutableRequest copy];

    [webView loadRequest:request];

    return NO;

    }else{

    return YES:

    }

    相关文章

      网友评论

          本文标题:ios webview 加载链接携带cookie

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