webview having trouble with redirects

梦想与她 提交于 2019-12-02 21:12:20

问题


NSURL *urlString = [NSURL URLWithString:urlAddress];

//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlString];

//Load the request in the UIWebView.
[self.webview loadRequest:requestObj];

The UIWebView is having trouble with redirects. How can I make sure the uiwebview handles redirects properly?


回答1:


In order to do a redirect from one site to another, just load your website, but add a BOOL var for checking if the right page is loaded.

 BOOL page1Loaded;

then, if you need to change to another site, just create a global NSString var like so

 // for site name
 NSString *urlAddress;

and just update upon needing to

 if(page1Loaded) {

    // as seen in @Lithu T.V.'s comment, DO NOT FORGET the http:// or https://
    // name of address 
    urlAddress = @"http://..." or @"https://..."

    NSString *urlString = [NSURL URLWithString:urlAddress];

    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlString];

    //Load the request in the UIWebView.
    [self.webview loadRequest:requestObj];
 }
 else {

    // as seen in @Lithu T.V.'s comment, DO NOT FORGET the http:// or https://
    // name of redirect 
    urlAddress = @"http://..." or @"https://..." 

    NSString *urlString = [NSURL URLWithString:urlAddress];

    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlString];

    //Load the request in the UIWebView.
    [self.webview loadRequest:requestObj];
 }

hope this helps!



来源:https://stackoverflow.com/questions/17847242/webview-having-trouble-with-redirects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!