iOS UIWebview 引用URL时存 cookie 方法 并向localStorage中存值

匿名 (未验证) 提交于 2019-12-03 00:22:01


向URL中注入cookie:

-(void)saveCookie

{

NSURL *url= [NSURL URLWithString:self.urlStr];

NSString *hostStr= url.host;

//注入Cookie

NSMutableArray *myMuArr=[NSMutableArray array];

NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];

setObject:@"pId" forKey:NSHTTPCookieName];

setObject:[NSString stringWithFormat:@"%@",pUserID] forKey:NSHTTPCookieValue];

setObject:hostStr forKey:NSHTTPCookieDomain];

setObject:@"/" forKey:NSHTTPCookiePath];

setObject:@"0" forKey:NSHTTPCookieVersion];

NSHTTPCookie *cookie1 = [NSHTTPCookie cookieWithProperties:cookieProperties];

addObject:cookie1];

NSArray *mmmArr=[NSArray arrayWithArray:myMuArr];

NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:mmmArr forURL:url mainDocumentURL:url];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.urlStr]];

self.myWebView loadRequest:request];

}


注意: 需要在注入cookie后再reloadRequest Webview


localStorage 中存值


NSString * userContent = [NSString stringWithFormat:@"%@",pUserID];

NSString *jsString = [NSString stringWithFormat:@"localStorage.setItem('pid', '%@')", userContent];

@"localStorage.removeItem('pid')";

self.myWebView stringByEvaluatingJavaScriptFromString:jsString];


-(void)webViewDidFinishLoad:(UIWebView *)webView;代理方法中;











易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!