1

I am trying to get a UIWebView to display google.com for now, but its not loading:

I just get a blank screen

.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIWebView *webView;

@end

.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    NSURL *targetURL = [NSURL URLWithString:@"http://www.google.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
    [_webView loadRequest:request];

    // Do any additional setup after loading the view, typically from a nib.
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

What I did is the following to create a web view:

  • Created a new UIViewController scene
  • Added a UIWebView to it
  • connected the webview to .h controller
  • Made my scene the inital scene
  • Ran the app, nothing was loaded, no errors, just go straight to UIViewController with nothing in it, please help.

I tried adding the following:

[self.view addSubview:self.webView];

That did nothing

user979331
  • 11,039
  • 73
  • 223
  • 418

7 Answers7

6

If You're running Web View on iOS > 9.0, probably You haven't added NSAppTransportSecurity key to project *.plist file, so the app can't request any URL.

If You want to allow all of URL request, then following code should be added to project plist file:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

else if You want to allow only specific URL request:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>allowed.domain.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>
saltwat5r
  • 1,107
  • 13
  • 21
1

Try adding :

- (void)viewDidLoad {

    [super viewDidLoad];

    //Check if the web view is linked correctly from the XIB
    NSLog(@"WebView : %@",_webView);
    _webView.delegate = self;


    NSURL *targetURL = [NSURL URLWithString:@"http://www.google.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
    [_webView loadRequest:request];

    // Do any additional setup after loading the view, typically from a nib.
}

and add this new method in your class (also implement UIWebViewDelegate in te .h)

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
   //Check if the web view loadRequest is sending an error message
   NSLog(@"Error : %@",error);
}
piyuj
  • 163
  • 7
1

If you are really using an outlet for webview, then this should work no matter what.

   [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
1

add Break point , check if NSURLRequest *request is nil , if nil, then do

NSString *urlString = @"http://www.yourWebAddress.com";
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];

try to encode the url...hope it will works

0

Would you happen to be building in Xcode 7 and targeting iOS 9?

If so, you're likely running into the new default security requirement for accessing sites via https, as detailed here:

Disabling ATS for an in-app browser in iOS 9?

Community
  • 1
  • 1
0

Perhaps the problem is that you call seter (_webView). Try through:

[self.webView loadRequest:request];
0

Allow AppTransport permission to yes in Info.plist

Harjeet Singh
  • 91
  • 1
  • 2