w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How can I use cookies using AFHTTPRequestOperationManager in AFNetworking 2.0+?

Yes. AFNetworking uses the foundation URL Loading system, which handles cookies out of the box.

You can configure NSMutableURLRequest's setHTTPShouldHandleCookies and use NSHTTPCookieStorage to store them.

In Objective-C:

NSArray *cookieStorage = [[NSHTTPCookieStorage
sharedHTTPCookieStorage] cookiesForURL:url];
NSDictionary *cookieHeaders = [NSHTTPCookie
NSMutableURLRequest *request = [myRequestSerializer requestWith…];
for (NSString *key in cookieHeaders) {
    [request addValue:cookieHeaders[key] forHTTPHeaderField:key];

In Swift:

var request = NSMutableURLRequest() // you can use an
AFNetworking Request Serializer to create this

if let cookieStorage =
NSHTTPCookieStorage.sharedHTTPCookieStorage().cookiesForURL(url) {
    for (headerField, cookie) in
NSHTTPCookie.requestHeaderFieldsWithCookies(cookieStorage) {
        request.addValue(cookie, forHTTPHeaderField: headerField)

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.