I have an iPhone app which is half web-based. I wanna check if a cookie is set (using Objective C) and do something else if it is not. I wanna check if a cookie with a specific name exists. Haven't found any solutions on this anywhere.
Asked
Active
Viewed 582 times
0
-
This question has already been answered over here: http://stackoverflow.com/a/2457042/7675584 – Joseph Shenton Apr 15 '17 at 13:20
-
@JosephShenton Joe I wanna check if it is set on the app launch. Just like `if (isset($_COOKIE['name']))` in php – Jakeas hacks Apr 15 '17 at 14:11
1 Answers
0
First of all it is not clear what are you asking.
I'm assuming that you are what to check if cookie storage has some specific cookie. On iOS and OS X cookie storage is a singleton: NSHTTPCookieStorage. Now question how url should impact you check? For different urls you can have different cookie value with the same name.
Maybe you need something like this:
NSArray<NSHTTPCookie *> *cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage.cookies;
cookies = [cookies filteredArrayUsingPredicate: [NSPredicate
predicateWithFormat: @"name == %@", @"name"];
if (cookies.count > 0)
{
…
}

Marek R
- 32,568
- 6
- 55
- 140
-
-
-
note my code fetches all cookies for all urls (first line). Second line filters them by name. So in third line you have all cookies with name 'name' for all possible servers (urls). – Marek R Apr 15 '17 at 18:42