码迷,mamicode.com
首页 > Web开发 > 详细

WKWebView Cookie注入

时间:2019-06-18 12:36:43      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:data   setcookie   source   esc   str   创建   script   cookies   user   

WKUserContentController *userContentController = WKUserContentController.new;
    NSString *cookieSource = @"document.cookie = ‘TeskCookieKey1=TeskCookieValue1‘;document.cookie = ‘TeskCookieKey2=TeskCookieValue2‘;";

    WKUserScript *cookieScript = [[WKUserScript alloc] initWithSource:cookieSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
    [userContentController addUserScript:cookieScript];

    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.userContentController = userContentController;
    self.webview = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];

 以上为WKWebView中创建cookie的方法,亲测有效

网上很多种方法都是基于 WKWebsiteDataStore 的使用,但是和我们的H5配合的时候,怎么都取不到对应的值,只有上面的方法能获取到

WKWebsiteDataStore * webStore = [WKWebsiteDataStore nonPersistentDataStore];
    NSHTTPCookie * cookie = [NSHTTPCookie cookieWithProperties:@{
                                                                                  
                                                                                  NSHTTPCookieName:@"RefresheEpireTime",
                                                                                  NSHTTPCookieValue:@"2019-05-07",
                                                                                  NSHTTPCookiePath:@"/ui/cookie.html",
                                                                                  NSHTTPCookieDomain:@"test.ripx.com"
                                                                                  }];
    [webStore.httpCookieStore setCookie:cookie completionHandler:nil];

  

WKWebView Cookie注入

标签:data   setcookie   source   esc   str   创建   script   cookies   user   

原文地址:https://www.cnblogs.com/tianlin106/p/11044362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!