0

I have a UIViewController class named WebScreenViewController, which is having an UIWebView. Here is the class implementation file:

@implementation WebScreenViewController

@synthesize urlString;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.webView.delegate = self;
    // Do any additional setup after loading the view.
    [self gotoUrl:self.urlString];
    //[self gotoUrl:@"http://www.google.com"];

}


#pragma mark - UIWebView Delegates
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

    NSURL* url = [request URL];
    NSString* urlStr = url.absoluteString;
    NSLog(@"shouldStartLoadWithRequest. URLStr = %@", urlStr);

    return YES;
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"webViewDidStartLoad");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString* html = [webView stringByEvaluatingJavaScriptFromString:@"document.body"];
    NSLog(@"webViewDidFinishLoad: HTML string = %@", html);

}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"didFailLoadWithError. ERROR: %@", error);
}

#pragma mark - Member Methods

-(void)gotoUrl:(NSString*)urlStr
{
    NSLog(@"Calling website %@ in UIWebView...", urlStr);
    NSURL* url = [NSURL URLWithString:urlStr];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:urlRequest];
}

@end

The above code work well on one of my testing device but did not work on another. It worked on my iPod Touch with the latest iOS 8.1.2. But it didn't work on my iPad3 with iOS version 7.0. When I run it on my iPad3, it will call the gotoUrl: method and the log "Calling website %@ in UIWebView..." will be displayed. And that's it. I receive no more logs from the delegates. To check the internet connections, I tried using Safari on the same device and it was working.

Aleš Roubíček
  • 5,198
  • 27
  • 29
  • 2
    did you check if self.webview is null in [gotoUrl:] ? – Shai Jan 06 '15 at 08:56
  • before setting the delegate first check whether it is initialised or not. If it is null then first initialise it then set its delegate. – Vaibhav Gautam Jan 06 '15 at 09:14
  • Well, like I said, the above code works fine when I use it in my iPod. Meaning, all the NSLogs print out correctly. The problem is if I run it in my iPad, it won't work. So I don't know what's wrong with it that it couldn't work in a different device with a different OS. – Bawenang Rukmoko Pardian Putra Jan 08 '15 at 03:07

2 Answers2

0

Check whether you've connected the web view's outlet correctly in iPad storyboard if you're using different storyboards for iPhone & iPad.

Also check that the web view is not nil.

Hope this helps..

Henit Nathwani
  • 442
  • 3
  • 10
0
NSString * strURL = [NSString stringWithFormat:@"http://www.google.com/"];
NSURL *loadURL=[NSURL URLWithString:strURL];
NSURLRequest *loadRequest = [NSURLRequest requestWithURL:loadURL];
[webView loadRequest:loadRequest];
webView.delegate=self;

Try this way in your view did load or if you have URL you can replace first line like this

NSString * strURL = [NSString stringWithFormat:self.urlString];
NewStackUser
  • 657
  • 5
  • 17