>已启用的cookie:
NShttpcookiestorage *cookieStorage = [NShttpcookiestorage sharedhttpcookiestorage];[cookieStorage setcookieAcceptPolicy:NShttpcookieAcceptPolicyAlways];
>创建NSURLConnection并从响应中提取cookie:
NSArray *cookies = [ NShttpcookie cookiesWithResponseheaderFIElds: [ httpResponse allheaderFIElds ] forURL:response.URL];[[NShttpcookiestorage sharedhttpcookiestorage] setcookies: cookies forURL: response.URL maindocumentURL:nil];
但这都没有帮助.但是,如果我在safari中启动URL,它会成功加载,之后我也可以在UIWebVIEw中加载相同的URL.你能帮我解决这个问题,我该如何为UIWebVIEw启用cookie?
提前致谢
解决方法 创建NSURLRequest后,将sharedhttpcookiestorage中的所有cookie复制到NSURLRequest:NSArray *cookies = [[NShttpcookiestorage sharedhttpcookiestorage] cookies];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];[request sethttpShouldHandlecookies:YES];[self addcookies:cookies forRequest:request];[_webVIEw loadRequest:request];
并添加addcookies:forRequest方法
- (voID)addcookies:(NSArray *)cookies forRequest:(NSMutableURLRequest *)request{ if ([cookies count] > 0) { NShttpcookie *cookie; Nsstring *cookieheader = nil; for (cookie in cookies) { if (!cookieheader) { cookieheader = [Nsstring stringWithFormat: @"%@=%@",[cookie name],[cookie value]]; } else { cookieheader = [Nsstring stringWithFormat: @"%@; %@=%@",cookieheader,[cookie value]]; } } if (cookieheader) { [request setValue:cookieheader forhttpheaderFIEld:@"cookie"]; } }}总结
以上是内存溢出为你收集整理的ios – UIWebview启用cookie全部内容,希望文章能够帮你解决ios – UIWebview启用cookie所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)