工作生活iOS表格绘制

WKWebView系列--Cookie问题

2019-07-03  本文已影响0人  JQWONG
什么是Cookie

iOS中管理Cookie
  • NSHTTPCookieStorage
    A container that manages the storage of cookies.Each stored cookie is represented by an instance of the NSHTTPCookie class.
    一个用于储存Cookie的容器,一个NSHTTPCookie单例代表一个Cookie
  • NSHTTPCookie
    A representation of an HTTP cookie.
    HTTP请求Cookie数据对象
UIWebView处理Cookie

NSHTTPCookieStorage *sharedCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

NSDictionary *properties = @{NSHTTPCookieName : @"cookie name",
                                 NSHTTPCookieValue : @"cookie value",
                                 NSHTTPCookieDomain : @"your omain",
                                 NSHTTPCookiePath : @"/"};

NSHTTPCookie *cookie = [[NSHTTPCookie alloc] initWithProperties:properties];

[sharedCookieStorage setCookie: cookie];
WKWebView处理Cookie
if (@available(iOS 11.0, *)) {
    [self.webview.configuration.websiteDataStore.httpCookieStore setCookie:eventCK completionHandler:^{
      @wb_strongify(self)
      NSURLRequest *request = [NSURLRequest requestWithURL:url];
      [self.webview loadRequest:request];
    }];
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:yourURL]];
NSDictionary *headFields = request.allHTTPHeaderFields;
NSString *cookie = headFields[@"cookie"];
if (cookie == nil) {
  [request addValue:[NSString stringWithFormat:@"%@=%@", cookieName, cookieValue] forHTTPHeaderField:@"Cookie"];
}
[self.webView loadRequest:request];

方法二:可以通过document.cookie设置Cookie

//在请求中添加cookie
[self.requestToLoad setValue:[NSString stringWithFormat:@"%@=%@", cookieName, cookieValue] forHTTPHeaderField:@"Cookie"];
NSString *cookieSource = [NSString stringWithFormat:@"document.cookie = '%@=%@';", cookieName, cookieValue];
//写一个js脚本,并将cookieSource放入脚本中
WKUserScript *cookieScrip = [[WKUserScript alloc] initWithSource:cookieSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
//添加js脚本
[self.webView.configuration.userContentController addUserScript:cookieScrip];
  • (instancetype)initWithSource:(NSString *)source injectionTime:(WKUserScriptInjectionTime)injectionTime forMainFrameOnly:(BOOL)forMainFrameOnly;
    Returns an initialized user script that can be added to a user content controller.
    注意第二个参数通常为WKUserScriptInjectionTimeAtDocumentStart表示在加载网页时注入
  • setValue:forHTTPHeaderField
    If the length of your upload body data can be determined automatically (for example, if you provide the body content with an NSData object), then the value of Content-Length is set for you.
  • addValue:forHTTPHeaderField
    This method provides the ability to add values to header fields incrementally. If a value was previously set for the specified field, the supplied value is appended to the existing value using the appropriate field delimiter (a comma).

到此为止WKWebView的Cookie问题已经可以基本解决了
未经授权,禁止转载!
尊重原创,转载请注明出处,谢谢!
参考文章:
https://www.jianshu.com/p/cd0d819b9851
https://www.jianshu.com/p/19e100b0c674
上一篇 下一篇

猜你喜欢

热点阅读