嗨,我需要修改 cookie,但我正在 iOS 中开发,有人知道如何更改它。也许在 UIWebViewDelegate 中使用 webView:shouldStartLoadWithRequest:navigationType:??
最佳答案
因为 WebKit.framework 不适用于 IOS,所以最好的方法是使用 UIWebView shouldStartLoadWithRequest 和 webViewDidFinishLoad 的事件,我使用 NSHTTPCookieStorage 类作为 cookie 并对其进行如下修改
NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [sharedHTTPCookieStorage cookiesForURL:[NSURL URLWithString:self.webView.request.URL.absoluteString]];
NSEnumerator *enumerator = [cookies objectEnumerator];
NSHTTPCookie *cookie;
while (cookie = [enumerator nextObject])
{
if ([[cookie name] isEqualToString:key])
{
NSString *actcookie = [cookie value];
NSMutableString *newcookiestring = [NSMutableString stringWithFormat:@"%@changes",actcookie];
NSMutableDictionary *propscook = [[NSMutableDictionary alloc] initWithDictionary: [cookie properties]];
[propscook setObject:newcookiestring forKey:NSHTTPCookieValue];
NSHTTPCookie *newcookie = [NSHTTPCookie cookieWithProperties:propscook];
[sharedHTTPCookieStorage setCookie:newcookie];
return [cookie value];
}
}
return nil;
关于objective-c - 如何在 iOS 应用程序中修改来自 NSURLRequest 的 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9321293/