iOS 7 UIWebView doesn't load webpage
Solution 1
I moved the loadRequest method to the completion handler of a presentViewController and it works in iOS 5, 6 and 7:
[self presentViewController:gwvc animated:YES completion:^(void){
[gwvc.wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.walkjogrun.net/about/eukanuba.html"]]];
}];
Solution 2
I found the root cause.
maybe I incorrectly used UIWebView
, but it works in iOS5 and iOS6.
I don't know why it works in earlier iOS versions...
Moreover, it works in iOS7 when I build code with SDK 6.1.
Here's my old code.
RechargeWebPageViewController *webPageViewController;
webPageViewController = [[ RechargeWebPageViewController alloc] initWithNibName:@"WebPage" bundle:nil];
if (webPageViewController != nil) {
webPageViewController.hidesBottomBarWhenPushed = YES;
webPageViewController.delegate=self;
[self.navigationController pushViewController:webPageViewController animated:YES];
NSURL *url = [NSURL URLWithString:@"https://xxx.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[webPageViewController loadRequest:request];
[request release];
}
I moved the loadRequest
from the viewDidLoad
method to the ViewWillAppear
method, then it worked.
I think maybe UIWebView
is not initialized correctly in my old code for iOS7.
Related videos on Youtube
Bruce Tsai
Updated on September 15, 2022Comments
-
Bruce Tsai over 1 year
My app uses UIWebview, and it works well in iOS 5 and iOS 6. However, it doesn't load the webpage in iOS 7 when I build in Xcode 5 and run the same code.
- (void)webViewDidFinishLoad:(UIWebView *)webView {} - (void)webViewDidStartLoad:(UIWebView *)webView {} - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {}
All delegate function is not called. But I do set delegate in xib file and code
self.theWebView.delegate = self;
I didn't find any information via google. Thank you for your help.
-
NLemay over 10 yearsI still call loadRequest in viewDidLoad and it doesn't seem to be a problem.
-
Bruce Tsai over 10 yearsThe problem of my code is I call pushViewcontroller first, then I call loadRequest. It seems we need to call loadRequest in ViewwillAppear or ViewdidLoad. In this way, we can make sure the UIWebview is initilized and ready to receive events of loadRequest. This is just my guess.
-
Willster over 10 yearsSimilarly, I needed to move the call to loadRequest: into viewDidAppear.
-
Willster over 10 yearsIf pushing the web view controller onto a navigation controller with animation, then I found that loadRequest needs to be done in viewDidAppear.
-
Henrik Erlandsson over 10 yearsThis didn't help me, but the cause was that my web view in IB got autosynthesized to _webV instead of webV. Just check if the webview in your loadrequest is not nil.