Xcode 7 UIWebView 不加载 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32719032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:54:26  来源:igfitidea点击:

Xcode 7 UIWebView doesn't load url

iosxcodeuiwebview

提问by wang hai peng

I use UIWebView in my app and it all worked fine in Xcode4,5,6 simulator. but not for Xcode 7 simulator, I don't know why, there is no warning or error in simulator, and the screen is just showing blank page. Please help me. Thanks.

我在我的应用程序中使用 UIWebView,并且在 Xcode4、5、6 模拟器中一切正常。但不适用于 Xcode 7 模拟器,我不知道为什么,模拟器中没有警告或错误,屏幕只是显示空白页。请帮我。谢谢。

#import "IndexViewController.h"

@interface IndexViewController ()

@end

@implementation IndexViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *urlString = nil;
    NSString *languageCode = [[NSLocale preferredLanguages] objectAtIndex:0];
    if ([languageCode isEqualToString:@"zh-Hans"]) {
        urlString = @"http://www.originoftime.net/index-cn";
    }else if ([languageCode isEqualToString:@"zh-Hant"]) {
        urlString = @"http://www.originoftime.net/index-cn";
    }else{
        urlString = @"http://www.originoftime.net/index-en";
    }
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

    NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];

    [_Index loadRequest:urlrequest];
}

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

回答by jlngdt

Xcode 7 with iOS9 now force you to not use HTTP call, but HTTPS ones.

带有 iOS9 的 Xcode 7 现在强制您不要使用 HTTP 调用,而是使用 HTTPS 调用。

this is a security point enhanced in AppTransportSecurity.

这是 AppTransportSecurity 中增强的安全点。

Try this:

尝试这个:

  • Go to your info.plist
  • Add a dictionary called NSAppTransportSecurity
  • Add a boolean attribute to this one, called NSAllowsArbitraryLoads
  • Pass it to TRUE
  • 转到您的 info.plist
  • 添加一个名为 NSAppTransportSecurity 的字典
  • 向这个添加一个布尔属性,称为 NSAllowsArbitraryLoads
  • 将其传递给 TRUE

Reload your app.

重新加载您的应用程序。

I advice you that if Apple want to block HTTP (unsecured) calls is for a good reason. http://www.originoftime.net/index-cnhas a HTTPS one but the server certificate seems to be self-signed.

我建议你,如果 Apple 想要阻止 HTTP(不安全)调用是有充分理由的。http://www.originoftime.net/index-cn有一个HTTPS,但服务器证书似乎是自签名的。

Let me know if this workaround work for you

让我知道此解决方法是否适合您

Cheers from france

来自法国的欢呼

回答by Rory McKinnel

Do you implement the web view delegate methods? In particular:

您是否实现了 Web 视图委托方法?特别是:

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

If a load fails, that will tell you what the issue is.

如果加载失败,它会告诉您问题是什么。

It could well be the error related to the new security model which is being enforced for network access. You can override this new behavior by adding the following into your Info.plist file. Just edit the XML and paste this in:

这很可能是与为网络访问强制执行的新安全模型相关的错误。您可以通过将以下内容添加到您的 Info.plist 文件中来覆盖此新行为。只需编辑 XML 并将其粘贴到:

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

The changes are summarized here: https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html

更改总结如下:https: //developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html