我已经写了以下代码,这是我在SO上的一个问题中找到的。
我仍然感到困惑,因为在完成所有操作后,我仍无法访问PHP
页面上的Cookie。
我在这里做错了什么?
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
NSString *url=@"http://mywebsite.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSMutableURLRequest *nsrequest=[NSMutableURLRequest requestWithURL:nsurl];
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"testCookie" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"someValue123456" forKey:NSHTTPCookieValue];
[cookieProperties setObject:@"www.mywebsite.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"www.mywebsite.com" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
[webview loadRequest:nsrequest];
}
答案 0 :(得分:1)
使用它来设置对象:
nsrequest.addValue(value: "testCookie", forHTTPHeaderField: NSHTTPCookieName)
etc...
抱歉,它在Swift 3 ^^中,但您可以转换为Obj-c
答案 1 :(得分:0)
您已在“www.example.com/”上设置了Cookie,但您尝试加载“my website.com” - 将NSHTTPCookieDomain和NSHTTPCookieOriginURL更改为“mywebsite.com”或尝试加载“www。 example.com“在你的请求中!