【问题标题】:Objective-C UIWebView not loading websiteObjective-C UIWebView 不加载网站
【发布时间】:2015-12-04 14:20:51
【问题描述】:

我现在正在尝试让 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

我所做的是创建一个 web 视图:

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

我尝试添加以下内容:

[self.view addSubview:self.webView];

什么都没做

【问题讨论】:

  • 你试过了吗[webView loadRequest:request]; , 你还必须在你的实现中使用 webview 属性。
  • 当插座已经存在时,尝试将其添加为子视图时没有意义

标签: ios objective-c uiwebview


【解决方案1】:

添加断点,检查NSURLRequest *request是否为nil, 如果为零,则执行

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

尝试对 url 进行编码...希望它能起作用

【讨论】:

    【解决方案2】:

    在 Info.plist 中允许 AppTransport 权限为 yes

    【讨论】:

      【解决方案3】:

      也许问题在于您调用了 seter (_webView)。试试看:

      [self.webView loadRequest:request];
      

      【讨论】:

        【解决方案4】:

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

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

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

        else 如果您只想允许特定的 URL 请求:

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

        【讨论】:

          【解决方案5】:

          您是否会在 Xcode 7 中构建并针对 iOS 9?

          如果是这样,您可能会遇到通过 https 访问网站的新默认安全要求,详情如下:

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

          【讨论】:

            【解决方案6】:

            如果你真的在使用 webview 的插座,那么无论如何这应该可以工作。

               [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
            

            【讨论】:

            • 确保插座已连接到故事板
            【解决方案7】:

            尝试添加:

            - (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.
            }
            

            并在你的类中添加这个新方法(也在 te .h 中实现 UIWebViewDelegate)

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

            【讨论】:

            • 这是我控制台中的内容:WebView : &lt;UIWebView: 0x17e8e5c0; frame = (0 0; 240 128); autoresize = RM+BM; layer = &lt;CALayer: 0x17e8e7c0&gt;&gt;
            • 并且从不打印 didFailLoadWithError 日志??
            • 谢谢!! didFailLoadWithError 帮助我找到了我的问题。
            猜你喜欢
            • 2013-02-08
            • 1970-01-01
            • 1970-01-01
            • 2012-04-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多