开发者社区> 问答> 正文

iOS WebView 中的 Cookie场景IP直连的几种方法(2)


利用 iOS11 之前的 API 解决 WKWebView 首次请求不携带 Cookie 的问题


通过让所有 WKWebView 共享同一个 WKProcessPool 实例,可以实现多个 WKWebView 之间共享 Cookie(session Cookie and persistent Cookie)数据。不过 WKWebView WKProcessPool 实例在 app 杀进程重启后会被重置,导致 WKProcessPool 中的 Cookie、session Cookie 数据丢失,目前也无法实现 WKProcessPool 实例本地化保存。可以采取 cookie 放入 Header 的方法来做。

  1. [backcolor=transparent] [backcolor=transparent]WKWebView[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] webView [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]WKWebView[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent]];[backcolor=transparent]
  2. [backcolor=transparent] [backcolor=transparent]NSMutableURLRequest[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] request [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]NSMutableURLRequest[backcolor=transparent] requestWithURL[backcolor=transparent]:[[backcolor=transparent]NSURL [backcolor=transparent]URLWithString[backcolor=transparent]:@[backcolor=transparent]"http://xxx.com/login"[backcolor=transparent]]];[backcolor=transparent]
  3. [backcolor=transparent] [backcolor=transparent][[backcolor=transparent]request addValue[backcolor=transparent]:@[backcolor=transparent]"skey=skeyValue"[backcolor=transparent] forHTTPHeaderField[backcolor=transparent]:@[backcolor=transparent]"Cookie"[backcolor=transparent]];[backcolor=transparent]
  4. [backcolor=transparent] [backcolor=transparent][[backcolor=transparent]webView loadRequest[backcolor=transparent]:[backcolor=transparent]request[backcolor=transparent]];

其中对于 skey=skeyValue 这个cookie值的获取,也可以统一通过domain获取,获取的方法,可以参照下面的工具类:
  1. [backcolor=transparent]HTTPDNSCookieManager[backcolor=transparent].[backcolor=transparent]h
  2. [backcolor=transparent]#ifndef[backcolor=transparent] [backcolor=transparent]HTTPDNSCookieManager_h
  3. [backcolor=transparent]#define[backcolor=transparent] [backcolor=transparent]HTTPDNSCookieManager_h
  4. [backcolor=transparent]// URL匹配Cookie规则
  5. [backcolor=transparent]typedef[backcolor=transparent] BOOL [backcolor=transparent](^[backcolor=transparent]HTTPDNSCookieFilter[backcolor=transparent])([backcolor=transparent]NSHTTPCookie[backcolor=transparent] [backcolor=transparent]*,[backcolor=transparent] NSURL [backcolor=transparent]*);
  6. [backcolor=transparent]@interface[backcolor=transparent] [backcolor=transparent]HTTPDNSCookieManager[backcolor=transparent] [backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]NSObject
  7. [backcolor=transparent]+[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]instancetype[backcolor=transparent])[backcolor=transparent]sharedInstance[backcolor=transparent];
  8. [backcolor=transparent]/**
  9. [backcolor=transparent] 指定URL匹配Cookie策略
  10. [backcolor=transparent] @param filter 匹配器
  11. [backcolor=transparent] */
  12. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]void[backcolor=transparent])[backcolor=transparent]setCookieFilter[backcolor=transparent]:([backcolor=transparent]HTTPDNSCookieFilter[backcolor=transparent])[backcolor=transparent]filter[backcolor=transparent];
  13. [backcolor=transparent]/**
  14. [backcolor=transparent] 处理HTTP Reponse携带的Cookie并存储
  15. [backcolor=transparent] @param headerFields HTTP Header Fields
  16. [backcolor=transparent] @param URL 根据匹配策略获取查找URL关联的Cookie
  17. [backcolor=transparent] @return 返回添加到存储的Cookie
  18. [backcolor=transparent] */
  19. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]NSArray[backcolor=transparent]<[backcolor=transparent]NSHTTPCookie[backcolor=transparent] [backcolor=transparent]*>[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]handleHeaderFields[backcolor=transparent]:([backcolor=transparent]NSDictionary[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]headerFields forURL[backcolor=transparent]:([backcolor=transparent]NSURL [backcolor=transparent]*)[backcolor=transparent]URL[backcolor=transparent];
  20. [backcolor=transparent]/**
  21. [backcolor=transparent] 匹配本地Cookie存储,获取对应URL的request cookie字符串
  22. [backcolor=transparent] @param URL 根据匹配策略指定查找URL关联的Cookie
  23. [backcolor=transparent] @return 返回对应URL的request Cookie字符串
  24. [backcolor=transparent] */
  25. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]NSString[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]getRequestCookieHeaderForURL[backcolor=transparent]:([backcolor=transparent]NSURL [backcolor=transparent]*)[backcolor=transparent]URL[backcolor=transparent];
  26. [backcolor=transparent]/**
  27. [backcolor=transparent] 删除存储cookie
  28. [backcolor=transparent] @param URL 根据匹配策略查找URL关联的cookie
  29. [backcolor=transparent] @return 返回成功删除cookie数
  30. [backcolor=transparent] */
  31. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]NSInteger[backcolor=transparent])[backcolor=transparent]deleteCookieForURL[backcolor=transparent]:([backcolor=transparent]NSURL [backcolor=transparent]*)[backcolor=transparent]URL[backcolor=transparent];
  32. [backcolor=transparent]@end
  33. [backcolor=transparent]#endif[backcolor=transparent] [backcolor=transparent]/* HTTPDNSCookieManager_h */
  34. [backcolor=transparent]HTTPDNSCookieManager[backcolor=transparent].[backcolor=transparent]m
  35. [backcolor=transparent]#import <Foundation/Foundation.h>
  36. [backcolor=transparent]#import "HTTPDNSCookieManager.h"
  37. [backcolor=transparent]@implementation[backcolor=transparent] [backcolor=transparent]HTTPDNSCookieManager
  38. [backcolor=transparent]{
  39. [backcolor=transparent]    [backcolor=transparent]HTTPDNSCookieFilter[backcolor=transparent] cookieFilter[backcolor=transparent];
  40. [backcolor=transparent]}
  41. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]instancetype[backcolor=transparent])[backcolor=transparent]init [backcolor=transparent]{
  42. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]self[backcolor=transparent] [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]super[backcolor=transparent] init[backcolor=transparent]])[backcolor=transparent] [backcolor=transparent]{
  43. [backcolor=transparent]        [backcolor=transparent]/**
  44. [backcolor=transparent]            此处设置的Cookie和URL匹配策略比较简单,检查URL.host是否包含Cookie的domain字段
  45. [backcolor=transparent]            通过调用setCookieFilter接口设定Cookie匹配策略,
  46. [backcolor=transparent]            比如可以设定Cookie的domain字段和URL.host的后缀匹配 | URL是否符合Cookie的path设定
  47. [backcolor=transparent]            细节匹配规则可参考RFC 2965 3.3节
  48. [backcolor=transparent]         */
  49. [backcolor=transparent]        cookieFilter [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]^[backcolor=transparent]BOOL[backcolor=transparent]([backcolor=transparent]NSHTTPCookie[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]cookie[backcolor=transparent],[backcolor=transparent] NSURL [backcolor=transparent]*[backcolor=transparent]URL[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  50. [backcolor=transparent]            [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([[backcolor=transparent]URL[backcolor=transparent].[backcolor=transparent]host containsString[backcolor=transparent]:[backcolor=transparent]cookie[backcolor=transparent].[backcolor=transparent]domain[backcolor=transparent]])[backcolor=transparent] [backcolor=transparent]{
  51. [backcolor=transparent]                [backcolor=transparent]return[backcolor=transparent] YES[backcolor=transparent];
  52. [backcolor=transparent]            [backcolor=transparent]}
  53. [backcolor=transparent]            [backcolor=transparent]return[backcolor=transparent] NO[backcolor=transparent];
  54. [backcolor=transparent]        [backcolor=transparent]};
  55. [backcolor=transparent]    [backcolor=transparent]}
  56. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]self[backcolor=transparent];
  57. [backcolor=transparent]}
  58. [backcolor=transparent]+[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]instancetype[backcolor=transparent])[backcolor=transparent]sharedInstance [backcolor=transparent]{
  59. [backcolor=transparent]    [backcolor=transparent]static[backcolor=transparent] id singletonInstance [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]nil[backcolor=transparent];
  60. [backcolor=transparent]    [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]dispatch_once_t[backcolor=transparent] onceToken[backcolor=transparent];
  61. [backcolor=transparent]    dispatch_once[backcolor=transparent](&[backcolor=transparent]onceToken[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]^{
  62. [backcolor=transparent]        [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent](![backcolor=transparent]singletonInstance[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  63. [backcolor=transparent]            singletonInstance [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[[backcolor=transparent]super[backcolor=transparent] allocWithZone[backcolor=transparent]:[backcolor=transparent]NULL[backcolor=transparent]][backcolor=transparent] init[backcolor=transparent]];
  64. [backcolor=transparent]        [backcolor=transparent]}
  65. [backcolor=transparent]    [backcolor=transparent]});
  66. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] singletonInstance[backcolor=transparent];
  67. [backcolor=transparent]}
  68. [backcolor=transparent]+[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]id[backcolor=transparent])[backcolor=transparent]allocWithZone[backcolor=transparent]:([backcolor=transparent]struct[backcolor=transparent] [backcolor=transparent]_NSZone[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]zone [backcolor=transparent]{
  69. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]self[backcolor=transparent] sharedInstance[backcolor=transparent]];
  70. [backcolor=transparent]}
  71. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]id[backcolor=transparent])[backcolor=transparent]copyWithZone[backcolor=transparent]:([backcolor=transparent]struct[backcolor=transparent] [backcolor=transparent]_NSZone[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]zone [backcolor=transparent]{
  72. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]self[backcolor=transparent];
  73. [backcolor=transparent]}
  74. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]void[backcolor=transparent])[backcolor=transparent]setCookieFilter[backcolor=transparent]:([backcolor=transparent]HTTPDNSCookieFilter[backcolor=transparent])[backcolor=transparent]filter [backcolor=transparent]{
  75. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]filter [backcolor=transparent]!=[backcolor=transparent] [backcolor=transparent]nil[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  76. [backcolor=transparent]        cookieFilter [backcolor=transparent]=[backcolor=transparent] filter[backcolor=transparent];
  77. [backcolor=transparent]    [backcolor=transparent]}
  78. [backcolor=transparent]}
  79. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]NSArray[backcolor=transparent]<[backcolor=transparent]NSHTTPCookie[backcolor=transparent] [backcolor=transparent]*>[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]handleHeaderFields[backcolor=transparent]:([backcolor=transparent]NSDictionary[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]headerFields forURL[backcolor=transparent]:([backcolor=transparent]NSURL [backcolor=transparent]*)[backcolor=transparent]URL [backcolor=transparent]{
  80. [backcolor=transparent]    [backcolor=transparent]NSArray[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]cookieArray [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]NSHTTPCookie[backcolor=transparent] cookiesWithResponseHeaderFields[backcolor=transparent]:[backcolor=transparent]headerFields forURL[backcolor=transparent]:[backcolor=transparent]URL[backcolor=transparent]];
  81. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]cookieArray [backcolor=transparent]!=[backcolor=transparent] [backcolor=transparent]nil[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  82. [backcolor=transparent]        [backcolor=transparent]NSHTTPCookieStorage[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]cookieStorage [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]NSHTTPCookieStorage[backcolor=transparent] sharedHTTPCookieStorage[backcolor=transparent]];
  83. [backcolor=transparent]        [backcolor=transparent]for[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]NSHTTPCookie[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]cookie [backcolor=transparent]in[backcolor=transparent] cookieArray[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  84. [backcolor=transparent]            [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]cookieFilter[backcolor=transparent]([backcolor=transparent]cookie[backcolor=transparent],[backcolor=transparent] URL[backcolor=transparent]))[backcolor=transparent] [backcolor=transparent]{
  85. [backcolor=transparent]                [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"Add a cookie: %@"[backcolor=transparent],[backcolor=transparent] cookie[backcolor=transparent]);
  86. [backcolor=transparent]                [backcolor=transparent][[backcolor=transparent]cookieStorage setCookie[backcolor=transparent]:[backcolor=transparent]cookie[backcolor=transparent]];
  87. [backcolor=transparent]            [backcolor=transparent]}
  88. [backcolor=transparent]        [backcolor=transparent]}
  89. [backcolor=transparent]    [backcolor=transparent]}
  90. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] cookieArray[backcolor=transparent];
  91. [backcolor=transparent]}
  92. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]NSString[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]getRequestCookieHeaderForURL[backcolor=transparent]:([backcolor=transparent]NSURL [backcolor=transparent]*)[backcolor=transparent]URL [backcolor=transparent]{
  93. [backcolor=transparent]    [backcolor=transparent]NSArray[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]cookieArray [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]self[backcolor=transparent] searchAppropriateCookies[backcolor=transparent]:[backcolor=transparent]URL[backcolor=transparent]];
  94. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]cookieArray [backcolor=transparent]!=[backcolor=transparent] [backcolor=transparent]nil[backcolor=transparent] [backcolor=transparent]&&[backcolor=transparent] cookieArray[backcolor=transparent].[backcolor=transparent]count [backcolor=transparent]>[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
  95. [backcolor=transparent]        [backcolor=transparent]NSDictionary[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]cookieDic [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]NSHTTPCookie[backcolor=transparent] requestHeaderFieldsWithCookies[backcolor=transparent]:[backcolor=transparent]cookieArray[backcolor=transparent]];
  96. [backcolor=transparent]        [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([[backcolor=transparent]cookieDic objectForKey[backcolor=transparent]:@[backcolor=transparent]"Cookie"[backcolor=transparent]])[backcolor=transparent] [backcolor=transparent]{
  97. [backcolor=transparent]            [backcolor=transparent]return[backcolor=transparent] cookieDic[backcolor=transparent][@[backcolor=transparent]"Cookie"[backcolor=transparent]];
  98. [backcolor=transparent]        [backcolor=transparent]}
  99. [backcolor=transparent]    [backcolor=transparent]}
  100. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]nil[backcolor=transparent];
  101. [backcolor=transparent]}
  102. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]NSArray[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]searchAppropriateCookies[backcolor=transparent]:([backcolor=transparent]NSURL [backcolor=transparent]*)[backcolor=transparent]URL [backcolor=transparent]{
  103. [backcolor=transparent]    [backcolor=transparent]NSMutableArray[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]cookieArray [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]NSMutableArray[backcolor=transparent] array[backcolor=transparent]];
  104. [backcolor=transparent]    [backcolor=transparent]NSHTTPCookieStorage[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]cookieStorage [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]NSHTTPCookieStorage[backcolor=transparent] sharedHTTPCookieStorage[backcolor=transparent]];
  105. [backcolor=transparent]    [backcolor=transparent]for[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]NSHTTPCookie[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]cookie [backcolor=transparent]in[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]cookieStorage cookies[backcolor=transparent]])[backcolor=transparent] [backcolor=transparent]{
  106. [backcolor=transparent]        [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]cookieFilter[backcolor=transparent]([backcolor=transparent]cookie[backcolor=transparent],[backcolor=transparent] URL[backcolor=transparent]))[backcolor=transparent] [backcolor=transparent]{
  107. [backcolor=transparent]            [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"Search an appropriate cookie: %@"[backcolor=transparent],[backcolor=transparent] cookie[backcolor=transparent]);
  108. [backcolor=transparent]            [backcolor=transparent][[backcolor=transparent]cookieArray addObject[backcolor=transparent]:[backcolor=transparent]cookie[backcolor=transparent]];
  109. [backcolor=transparent]        [backcolor=transparent]}
  110. [backcolor=transparent]    [backcolor=transparent]}
  111. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] cookieArray[backcolor=transparent];
  112. [backcolor=transparent]}
  113. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]NSInteger[backcolor=transparent])[backcolor=transparent]deleteCookieForURL[backcolor=transparent]:([backcolor=transparent]NSURL [backcolor=transparent]*)[backcolor=transparent]URL [backcolor=transparent]{
  114. [backcolor=transparent]    [backcolor=transparent]int[backcolor=transparent] delCount [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent];
  115. [backcolor=transparent]    [backcolor=transparent]NSHTTPCookieStorage[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]cookieStorage [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]NSHTTPCookieStorage[backcolor=transparent] sharedHTTPCookieStorage[backcolor=transparent]];
  116. [backcolor=transparent]    [backcolor=transparent]for[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]NSHTTPCookie[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]cookie [backcolor=transparent]in[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]cookieStorage cookies[backcolor=transparent]])[backcolor=transparent] [backcolor=transparent]{
  117. [backcolor=transparent]        [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]cookieFilter[backcolor=transparent]([backcolor=transparent]cookie[backcolor=transparent],[backcolor=transparent] URL[backcolor=transparent]))[backcolor=transparent] [backcolor=transparent]{
  118. [backcolor=transparent]            [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"Delete a cookie: %@"[backcolor=transparent],[backcolor=transparent] cookie[backcolor=transparent]);
  119. [backcolor=transparent]            [backcolor=transparent][[backcolor=transparent]cookieStorage deleteCookie[backcolor=transparent]:[backcolor=transparent]cookie[backcolor=transparent]];
  120. [backcolor=transparent]            delCount[backcolor=transparent]++;
  121. [backcolor=transparent]        [backcolor=transparent]}
  122. [backcolor=transparent]    [backcolor=transparent]}
  123. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] delCount[backcolor=transparent];
  124. [backcolor=transparent]}
  125. [backcolor=transparent]@end

展开
收起
猫饭先生 2017-10-20 11:20:02 1705 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载