Enable cookies in UIWebView (iPhone)

How do I enable cookies in my iPhone application using the UIWebView window so that my login system works?

+6
source share
2 answers

Start with

[NSHTTPCookieStorage sharedHTTPCookieStorage].cookieAcceptPolicy = NSHTTPCookieAcceptPolicyAlways; 

But, as @JoelFan has already been mentioned, the problem may be your User Agent string, causing ASP.NET to try and fail when logging into cookieless. Instead of an answer that includes

Set-Cookie: .ASPXAUTH = really-long-hex-number

it returns a redirect to something like

Location: / (F (long-sorta-base64ish-look-string)) /

The default UIWebView user agent string looks like

User-Agent: Mozilla / 5.0 (iPad, CPU OS 7_0_2, like Mac OS X) AppleWebKit / 537.51.1 (KHTML, like Gecko) Mobile / 11A501

but ASP.NET doesn't like it. Safari is sending something like this:

User-Agent: Mozilla / 5.0 (iPad, CPU OS 7_0_2, like Mac OS X) AppleWebKit / 537.51.1 (KHTML, for example, Gecko) Version /7.0 Mobile / 11A501 Safari / 9537.53

Do the following early on, perhaps in your AppDelegate.m

 // DON'T try to reuse a UIWebView for this. UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectZero]; // This webview has already decided to use the default user agent string. // let use javascript to get the existing user agent string NSString *userAgent = [wv stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"]; // let tack on some stuff to make ASP.NET happy userAgent = [userAgent stringByAppendingString:@" Version/7.0 Safari/9537.53"]; [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": userAgent}]; // New UIWebViews inited after here will use the user agent string you made. 
+8
source

If the site you are accessing is an ASP.NET site, the problem may be that the UIWebView is sending an unrecognized User Agent. See Change User Agent in UIWebView (iPhone SDK)

+1
source

Source: https://habr.com/ru/post/911242/


All Articles