Objective-C UIWebView无法加载网站

use*_*331 0 objective-c uiwebview ios

我想让UIWebView暂时显示google.com,但它没有加载:

我只是得到一个空白的屏幕

.H

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

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

@end
Run Code Online (Sandbox Code Playgroud)

.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
Run Code Online (Sandbox Code Playgroud)

我做的是创建Web视图的以下内容:

  • 创建了一个新的UIViewController场景
  • 添加了一个UIWebView
  • 将webview连接到.h控制器
  • 让我的场景成为初始场景
  • 跑了应用程序,没有加载,没有错误,只是直接去UIViewController没有任何内容,请帮忙.

我尝试添加以下内容:

[self.view addSubview:self.webView];
Run Code Online (Sandbox Code Playgroud)

那什么都没做

sal*_*t5r 6

如果您在iOS> 9.0上运行Web View,可能您尚未将NSAppTransportSecurity密钥添加到项目*.plist文件中,因此应用程序无法请求任何URL.

如果要允许所有URL请求,则应将以下代码添加到项目plist文件中:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>
Run Code Online (Sandbox Code Playgroud)

否则,如果您只想允许特定的URL请求:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>allowed.domain.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>
Run Code Online (Sandbox Code Playgroud)