/ objc

SDWebImageでcookieを使う

[[SDWebImageManager sharedManager] downloadImageWithURL:...を使った時、こんな風にした。

/* build cookie */
+ (NSHTTPCookie *)buildCookieWithName:(NSString *)cookieName
                          cookieValue:(NSString *)cookieValue {
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:
                                                               cookieName, NSHTTPCookieName,
                                                               cookieValue, NSHTTPCookieValue,
                                                               CookieDomain, NSHTTPCookieDomain,
                                                               @"/", NSHTTPCookiePath,
                                                               [NSDate distantFuture], NSHTTPCookieExpires,
                                                               nil]];
    return cookie;
}

- (void)foo {
    NSString *value = @"";
    if (value.length) {
        // SDWebImageにcookieを登録
        NSHTTPCookie *cookie = [[self class] buildCookieWithName:@"app_uid"
                                                     cookieValue:value];
        NSDictionary *header = [NSHTTPCookie requestHeaderFieldsWithCookies:@[ cookie ]];
        for (id key in [header allKeys]) {
            id value = header[key];
            [[SDWebImageManager sharedManager].imageDownloader setValue:value
                                                     forHTTPHeaderField:key];
        }
    }
}