Do requests in UIWebView use sharedHTTPCookieStorage? - iphone

From googling a bit, the answer to my question is "yes". But how do I test that?
I tried to inpsect the HTTP request headers (i.e. [request allHTTPHeaderFields]) in webView:shouldStartLoadWithRequest:navigationType:, but none of the request has the "Cookie" entry, even though cookies are successfully stored in [NSHTTPCookieStorage sharedHTTPCookieStorage].

try this to loop through all the cookies in the UIWebView to verify they are there
NSHTTPCookie *aCookie;
for (aCookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
NSLog(#"%#", aCookie);
}

Related

Cookie from ios webview

I wanted to ask some things about a piece of code i'm trying to make without having any previous contact with ios or objective-c.
This piece of code will:
Open a WebView with a specific url where the user will login
(done)
After the user logs in it will take the cookie created
from that login.
It will use that cookie in a next request to
load another site that requires authentication.
I'm stuck a bit at part 2 because it has to a) wait in another thread till the user does the login (how?) and b) because i can't seem to get the specific cookie for the site easily. I have only found and tried this poc but how do I filter out only the cookie for the site I want?
NSHTTPCookie *cookie;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for(cookie in [cookieJar cookies]) {
NSLog(#"%#", cookie);
}
Any ideas on how to make the 2a/b parts? Objective-c syntax seems a bit confusing.
I found a solution by following this https://www.inkling.com/read/learning-ios-programming-alasdair-allan-2nd/chapter-7/embedding-a-web-browser-in-your .
Used the webViewDidFinishLoad delegate and got the cookies from there with the cookiesForURL method.
NSArray* availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:#"MYURL"]];
Cookies are created as follows,
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
url, NSHTTPCookieOriginURL,
#"testCookies", NSHTTPCookieName,
#"1", NSHTTPCookieValue,
nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
You can use the cookie object to get the origin URL attribute and filter.
NSDictionary *cookieProperties = [cookie properties];
NSURL *originURL = [cookieProperties objectForKey:NSHTTPCookieOriginURL];
After filtering the cookie you need you can get the cookie value using,
cookie.value

How do I clear the browser cache programmatically on the iPhone?

I am using the new Basecamp API for my iOS basecamp client app. I want the user to be able to logout and switch accounts. But I can't as the account credentials stored in the browser cache are used every time I request authorization.
I figured out I would need to flush browser cache to do this. How do I clear the browser cache?
[[NSURLCache sharedURLCache] removeAllCachedResponses];
After that, you can deleting any associated cookies with in the UIWebView:
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
if([[cookie domain] isEqualToString:someNSStringUrlDomain]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}

deleted NSHTTPCookie returns if app is terminated

After using some code to delete all of the cookies:
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies]) {
[storage deleteCookie:cookie];
}
if you continue to use the app for a period of time, the cookies stay deleted. however, if you terminate the app immediately afterwards, the cookies will come back. sounds like some kind of cookie sync mechanism isn't kicking in fast enough, but the's no mention of it in the HTTPCookieStore docs.
How do you get a cookie to (reliably) stay deleted?
I do not think that there is a way to do that. Cookies are cached before they are sent to the NSHTTPCookie class and the same thing happens when you delete cookies. The class is told to delete the Cookies on quit, but won't if the app crashes as it doesn't catch the appropriate event.

iphone nsurlconnection read cookies

I am using async NSURLConnection to connect to a web site from iPhone. Handle didReceiveResponse is activated on response and I am trying to get all cookies, by using allHeaderFields from NSHTTPURLResponse
I see many hreader, but no Set-Cookie - it looks like iphone simulator just ignores them...
And I am sure cookies are present in response - network monitor shows they present
I do not use any http storage - all that I am trying to do is to print to log all header - and do not see cookies info
Does anybody know about this issue?
UPDATE
I have made some research: if my website returns custom header, like "Custom-Header: value" - then this header is visible in java client, but is not in iphone...
thanks
Try to look for it in the shared HTTP cookies storage:
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies])
{
NSLog(#"name: '%#'\n", [cookie name]);
NSLog(#"value: '%#'\n", [cookie value]);
NSLog(#"domain: '%#'\n", [cookie domain]);
NSLog(#"path: '%#'\n", [cookie path]);
}
or if working in Swift:
for cookie in HTTPCookieStorage.shared.cookies!
{
NSLog("name: \(cookie.name)")
NSLog("value: \(cookie.value)")
NSLog("domain: \(cookie.name)")
NSLog("path: \(cookie.path)")
}
Try this: in your NSMutableURLRequest, you should tell it to handle cookies:
[request setHTTPShouldHandleCookies:YES];
I don't know if it matters in apps, but what is your Accept Cookies setting for Safari in the Settings app. See if changing to Always matters.
According to some sites I've seen, a complete reboot of the iPhone is required for this setting to have any effect.

iPhone NSData/NSUrl with cookie

I'm trying to play/stream a mp3 hosted on a website. The site requires a cookie header to be set, but I'm having trouble setting that or getting the container to do that for me.
NSURL *sampleUrl = [NSURL URLWithString:#"http://domain/files/sample.mp3"];
NSData *sampleAudio = [NSData dataWithContentsOfURL:sampleUrl];
Up until this point, I've been using jQuery to do/manage XMLHTTPRequests, but I've had to call into native code to stream the audio. It doesn't look like the cookie is getting picked up by the native HTTP request.
Is there anyway to inject the cookie into the above request, or otherwise ensure that a cookie gets added against a given domain?
Thanks
Ok, so the way to do this is not injecting headers but setting a cookie manager to always accept cookies. This will then pass on the cookie to subsequent requests.
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
Robbie