OStack程序员社区-中国程序员成长平台

标题: objective-c - 如何在 iOS 应用程序中从 NSURLRequest 修改 cookie [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 19:23
标题: objective-c - 如何在 iOS 应用程序中从 NSURLRequest 修改 cookie

您好,我需要修改一个 cookie,但我正在 iOS 中开发,有人知道如何更改它。也许在 UIWebViewDelegate 中带有 webView:shouldStartLoadWithRequest:navigationType:??



Best Answer-推荐答案


由于IOS不支持WebKit.framework,最好的方法是使用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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4