简体   繁体   中英

login working in Safari but not in UIWebView

I have a dead-simple iOS app that simply displays a UIWebView displaying a certain website that requires a login. If I run my app and try to login the site just redisplays the login page (with no error message). If I use Safari, I can login to that same site fine. What could I be doing wrong in my UIWebView ? In case it matters, the site uses SSL / https.

It turned out that the problem was this: Asp.Net Forms Authentication when using iPhone UIWebView

(The UIWebView sends an odd User Agent header which ASP.NET doesn't recognize so it thinks it can't handle cookies. This has apparently been fixed in ASP.NET 4.5 and even in earlier versions you can fix it on the server if you have access to the ASP.NET site)

and the answer was here: Change User Agent in UIWebView (iPhone SDK)

which linked to: http://www.mphweb.com/en/blog/easily-set-user-agent-uiwebview

with the critical part being to put this code in your app startup code:

- (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSDictionary *dictionary = [NSDictionary 
        dictionaryWithObjectsAndKeys:
        @"Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5", 
        @"UserAgent", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}

This changes the User Agent. There are a surprising amounts of other ways to do this, some immensely more complicated (one of which is called "swizzling") and most of which don't work.

In such cases, UIWebView require implementing the right delegate protocol methods that allows you to communicate with back-end architectures. Please have a look at this , this and this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM