gpt4 book ai didi

ios - Objective-C UIWebView 未加载网站

转载 作者:行者123 更新时间:2023-11-28 18:02:53 25 4
gpt4 key购买 nike

我正在尝试让 UIWebView 暂时显示 google.com,但它没有加载:

我只是得到一个空白屏幕

.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

我所做的是创建一个 WebView :

  • 创建了一个新的 UIViewController 场景
  • 为其添加了一个 UIWebView
  • 将 webview 连接到 .h Controller
  • 将我的场景设为初始场景
  • 运行应用程序,没有加载任何内容,没有错误,直接进入 UIViewController,其中没有任何内容,请帮忙。

我尝试添加以下内容:

[self.view addSubview:self.webView];

什么都没做

最佳答案

如果您在 iOS > 9.0 上运行 Web View,可能您还没有将 NSAppTransportSecurity key 添加到项目 *.plist 文件中,因此该应用无法请求任何 URL。

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

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

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

<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>allowed.domain.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>

关于ios - Objective-C UIWebView 未加载网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32465577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com