Come posso abilitare i cookie nella mia applicazione iPhone che utilizza una finestra di UIWebView, in modo che il mio sistema di login funzioni?Abilitare i cookie in UIWebView (iPhone)
risposta
Se il sito in cui si effettua l'accesso è un sito ASP.NET, il problema potrebbe essere dovuto a UIWebView che invia un agente utente non riconosciuto. Vedi Change User Agent in UIWebView (iPhone SDK)
per l'avvio sicuro che con
[NSHTTPCookieStorage sharedHTTPCookieStorage].cookieAcceptPolicy =
NSHTTPCookieAcceptPolicyAlways;
Ma, come detto dal @JoelFan, il problema potrebbe essere il vostro utente stringa agente che causa ASP.NET per tentare e fallire a un account di accesso senza cookie. Invece di una risposta che include
Set-Cookie: ASPXAUTH = davvero a lungo-hex-numero
restituisce un reindirizzamento a qualcosa di simile
Località:/(F (long-sorta-base64ish-looking-string))/
La stringa dell'agent user di UIWebView predefinita è qualcosa come
User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0_2 come Mac OS X) AppleWebKit/537.51.1 (KHTML, come Gecko) Mobile/11A501
ma ASP.NET non piace questo. Safari invia qualcosa di simile:
User-Agent: Mozilla/5.0 (iPad, CPU OS 7_0_2 come Mac OS X) AppleWebKit/537.51.1 (KHTML, come Gecko) Version/7.0 Mobile/11A501 Safari/9537,53
seguire le seguenti procedure nella fase iniziale, forse nel vostro 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's use javascript to get the existing user agent string
NSString *userAgent = [wv stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
// let's 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.