Find if cookie has a value for variable
In a previous answer I found on stackoverflow (can't find now) someone suggested using NSRange rangeOfString to see if a particular variable had data in it from a cookie:
NSRange开发者_高级运维 range = [[[NSHTTPCookie requestHeaderFieldsWithCookies:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:cookie]]] objectForKey:@"Cookie"] rangeOfString:@"variable"];
if (range.length != NSNotFound) {
//it has data
} else {
//it doesn't
}
From what resources I can find this should work, but it doesn't, even when the cookie is null.
What am I doing wrong?
This is a bit odd. Even though range.length is apparently null if you log the output you can't check if its null and NSNotFound doesn't work as expected. You have to check if the value > 0.
精彩评论