【问题标题】:Xcode 7 UIWebView doesn't load urlXcode 7 UIWebView 不加载 url
【发布时间】:2025-12-18 12:10:01
【问题描述】:

我在我的应用程序中使用 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.
}

【问题讨论】:

    标签: ios xcode uiwebview


    【解决方案1】:

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

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

    如果加载失败,则会告诉您问题所在。

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

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

    这里总结了这些变化:https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/index.html

    【讨论】:

      【解决方案2】:

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

      这是在 AppTransportSecurity 中增强的安全点。

      试试这个:

      • 转到您的 info.plist
      • 添加一个名为 NSAppTransportSecurity 的字典
      • 为此添加一个布尔属性,称为 NSAllowsArbitraryLoads
      • 将其传递给 TRUE

      重新加载您的应用。

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

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

      来自法国的欢呼

      【讨论】:

      • 更多细节,我建议你阅读这篇完整的文章:WORKING WITH APPLE’S APP TRANSPORT SECURITY
      • 因为人们可能还在看这个答案:Apple 正式宣布他们将停止自动接受带有 ATS 豁免的 App Store 提交(info.plist 中的 NSAppTransportSecurity 键)。这是在 WWDC 2016 会议“安全新特性”期间。演讲者提到,有合理理由的应用提交仍可能使用 ATS 豁免。
      • @DylanS 你是对的!但目前没有任何具体内容,除了本次会议主题文件中的一句话:/!对于所有人:正如我在写答案时所说,您可能不会分流此 ATS,而是检查您的证书(例如自签名:用于生产的 booo)