{"rsdb":{"rid":"380872","subhead":"","postdate":"0","aid":"266052","fid":"99","uid":"1","topic":"1","content":"
\n

iOS \u548c H5 \u9875\u9762\u4ea4\u4e92\uff08WKWebview \u548c UIWebview cookie \u8bbe\u7f6e\uff09<\/h1> \n
\n

\u4e3b\u8981\u8bb0\u5f55\u5173\u4e8ecookie\u76f8\u5173\u7684\u5751<\/p> \n <\/blockquote> \n

1. UIWebview<\/h2> \n

1. UIWebview \u76f8\u5bf9\u6bd4\u8f83\u7b80\u5355 \u76f4\u63a5\u901a\u8fc7 NSHTTPCookieStorage \u8bbe\u7f6ecookie\u5c31\u80fd\u5b9e\u73b0\u3002<\/h3> \n

\u4ee3\u7801\u90e8\u5206<\/h4> \n
```\n  NSURL *cookieHost = [NSURL URLWithString:self.domain];\n\/\/ \u8bbe\u5b9a cookie\nNSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:\n                        [NSDictionary dictionaryWithObjectsAndKeys:\n                         [cookieHost host], NSHTTPCookieDomain,\n                         [cookieHost path], NSHTTPCookiePath,\n                         self.cookieKey,  NSHTTPCookieName,\n                         self.cookieva lue, NSHTTPCookieva lue,\n                         nil]];\n\/\/ \u52a0\u5165cookie\n[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];\n```<\/code><\/pre> \n 

2. \u5982\u679c\u5728\u7b2c\u4e00\u6b21\u8bf7\u6c42\u7684\u65f6\u5019\u9700\u8981\u5728HTTPRequest \u901a\u8fc7setValueForKey\u8bbe\u7f6e HeaderValue<\/h3> \n

2. WKWebview<\/h3> \n

\u5728\u4f7f\u7528WKWebview\u7684\u65f6\u5019\u4e5f\u662f\u9700\u8981\u5206\u4e24\u79cd\u60c5\u51b5\u4f20\u9012\uff1a<\/p> \n

    \n
  • 1.HTTPRequest \u8bf7\u6c42URL\u7684\u65f6\u5019\u643a\u5e26 \u5982\u540e\u7aefPHP\u83b7\u53d6 cookie<\/li> \n
  • 2.\u6ce8\u5165js \u76ee\u7684\u662f\u8ba9\u524d\u7aef\u4ece\u9875\u9762\u91cc\u8fb9\u83b7\u53d6\u5230cookie \u53ef\u4ee5\u901a\u8fc7\u5728document.cookie \u8bbe\u7f6e \u901a\u8fc7WKWebview \u521d\u59cb\u5316\u65f6\u5019\u628ajs\u4f20\u9012\u8fc7\u53bb<\/p>

    `WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource: cookieva lue injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];<\/code><\/p><\/li> \n

  • 3.NSHTTPCookieStorage \u4f3c\u4e4e\u4e0d\u643a\u5e26\u6ca1\u95ee\u9898\uff0c\u56e0\u4e3a\u6211\u4eec\u76ee\u524d\u6ca1\u6709\u901a\u8fc7\u8fd9\u4e2a\u4f20\u9012cookie<\/p><\/li> \n <\/ul> \n

    \u7f51\u4e0a\u53c2\u8003\u522b\u4eba\u7684\u65b9\u6cd5\u662f\u8981\u5b9e\u73b0\u4e0b\u9762\u51e0\u4e2a\u6b65\u9aa4\uff0c\u4f46\u662f\u6211\u4eec\u9879\u76ee\u5e76\u6ca1\u6709\u6309\u7167\u8fd9\u4e09\u79cd\u5fc5\u8981\u65b9\u5f0f\uff0c\u4f46\u662f\u53ef\u4ee5\u505a\u4e2a\u53c2\u8003\uff1a<\/h3> \n
    \n

    WKWebview\u4e09\u4e2a\u5904\u7406\u6b65\u9aa4\uff1a \uff081\uff09iOS11\uff0cWKHTTPCookieStore \u76f4\u63a5\u4f20\u9012\u3002\uff08\u5982\u679c\u662f\u53ea\u652f\u6301iOS11\uff0c\u4e0b\u9762\u4e24\u6b65\u53ef\u4ee5\u4e0d\u505a\uff09\uff1b \uff082\uff09iOS8-iOS10\uff0c js\u6ce8\u5165\uff1b \uff083\uff09PHP\u643a\u5e26cookie\u65b9\u5f0f<\/p> \n <\/blockquote> \n

    \u76f8\u5173\u4ee3\u7801<\/h4> \n
    #pragma mark - WKWebview\n\/\/ iOS11\n- (void)setWkCookie:(WKWebView *)wkWebview completionHandler:(nullable void (^)(void))comple {\n    \n    NSURL *cookieHost = [NSURL URLWithString:self.domain];\n    \/\/ \u8bbe\u5b9a cookie\n    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:\n                            [NSDictionary dictionaryWithObjectsAndKeys:\n                             [cookieHost host], NSHTTPCookieDomain,\n                             [cookieHost path], NSHTTPCookiePath,\n                             self.cookieKey,  NSHTTPCookieName,\n                             self.cookieva lue, NSHTTPCookieva lue,\n                             \/\/                             [NSDate dateWithTimeIntervalSinceNow:30*60*60],NSHTTPCookieExpires,\n                             nil]];\n    \n    \/\/ \u52a0\u5165cookie\n    \/\/\u53d1\u9001\u8bf7\u6c42\u524d\u63d2\u5165cookie\uff1b\n    if (@available(iOS 11.0, *)) {\n        WKHTTPCookieStore *cookieStore = wkWebview.configuration.websiteDataStore.httpCookieStore;\n        [cookieStore setCookie:cookie completionHandler:^{\n            \n            comple?comple():nil;\n        }];\n    } else {\n        \n        \n    }\n\n}\n\n\/\/ JS\u643a\u5e26cookie\u7684\u5f62\u5f0f\n- (void)setWkJsCookie:(WKUserContentController *)userContentController {\n    \/\/ \u5355\u4e2acookie\uff0c\u591a\u4e2a\u7684\u8bdd\uff0c\u518d\u52a0\u4e0adocument.cookie ='%@=%@';\u4e00\u6b21\n    NSString *cookieStr = [NSString stringWithFormat:@"document.cookie ='%@=%@';",self.cookieKey,self.cookieva lue];\n    WKUserScript * cookieScript = [[WKUserScript alloc] initWithSource: cookieStr injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];\n    [userContentController addUserScript:cookieScript];\n}\n\n\/\/ PHP\u643a\u5e26cookie\u7684\u5f62\u5f0f\n- (void)setWkPHPCookie:(NSMutableURLRequest *)request {\n    \/\/\u901a\u8fc7host\u5173\u8054cookie\u3002\n    NSMutableDictionary *cookieDic = [NSMutableDictionary dictionary];\n    NSMutableString *cookieva lue = [NSMutableString stringWithFormat:@""];\n    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];\n    for (NSHTTPCookie *cookie in [cookieStorage cookies]) {\n        [cookieDic setObject:cookie.value forKey:cookie.name];\n    }\n    if ([cookieDic objectForKey:[CookieManager shareInstance].cookieKey]) {\n        [cookieDic removeObjectForKey:[CookieManager shareInstance].cookieKey];\n    }\n    \n    \/\/ cookie\u91cd\u590d\uff0c\u5148\u653e\u5230\u5b57\u5178\u8fdb\u884c\u53bb\u91cd\uff0c\u518d\u8fdb\u884c\u62fc\u63a5\n    for (NSString *key in cookieDic) {\n        NSString *appendString = [NSString stringWithFormat:@"%@=%@;", key, [cookieDic valueForKey:key]];\n        [cookieva lue appe","orderid":"0","title":"iOS \u548c H5 \u9875\u9762\u4ea4\u4e92\uff08WKWebview \u548c UIWebview cookie \u8bbe\u7f6e\uff09(\u4e00)","smalltitle":"","mid":"0","fname":"iOS","special_id":"0","bak_id":"0","info":"0","hits":"38","pages":"2","comments":"0","posttime":"2019-11-15 18:41:19","list":"1573814479","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"iOS<\/A> \u9875\u9762<\/A> \u4ea4\u4e92<\/A> WKWebview<\/A> UIWebview<\/A> cookie<\/A> \u8bbe\u7f6e<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.17.22.34","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"iOS \u548c H5 \u9875\u9762\u4ea4\u4e92\uff08WKWebview \u548c UIWebview cookie \u8bbe\u7f6e\uff09","lastview":"1576125164","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}