【问题标题】:How to load a website URL in UIWebView without getting frozen?如何在 UIWebView 中加载网站 URL 而不会被冻结?
【发布时间】:2015-03-06 20:49:59
【问题描述】:

我在下面的这段代码中打开了一个带有网站 URL 的 UIwebview,这是一个非常繁重的电子商务网站。一旦我开始浏览该站点,我就会遇到一个页面开始加载但没有完成的块。 就像在我的 UIWebViewDelegate 中一样,调用了 webViewDidStartLoad 但没有调用 webViewDidFinishLoad...

我的 viewcontroller.h 文件:

            #import <UIKit/UIKit.h>

            @interface WVTesterViewController : UIViewController <UIWebViewDelegate>

            @end

我的 .m 文件:

            #import "WVTesterViewController.h"

            @interface WVTesterViewController ()

            @end

            @implementation WVTesterViewController

            - (void)viewDidLoad
            {
                [super viewDidLoad];
                // Do any additional setup after loading the view, typically from a nib.

                UIWebView *view = self.view.subviews[0];
                view.delegate = self;
                NSURL *url = [NSURL URLWithString:@"<<< MY WEBSITE URL >>>"];
                [view loadRequest:[NSURLRequest requestWithURL:url]];
            }

            - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
            {
                NSLog(@"Failed to load with error :%@",[error debugDescription]);
            }

            - (void)webViewDidStartLoad:(UIWebView *)webView
            {
                NSLog(@"webViewDidStartLoad :");
            }

            - (void)webViewDidFinishLoad:(UIWebView *)webView
            {
                NSLog(@"webViewDidFinishLoad :");
            }

            - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
            {
                NSLog(@"shouldStartLoadWithRequest :");
                return true;
            }

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

            @end

知道为什么会发生这种情况,下面是我的网络线程的屏幕帽,它挂起,我的应用程序已经死在水中了。

https://www.dropbox.com/s/ufmbncjbz0x0vbi/Screenshot%202015-03-06%2015.41.08.png?dl=0

其他信息

  • WebThread 的 CPU 使用率为 100%,表明正在发生无限循环。
  • 网络线程调用的方法的名称表明循环是在初始页面加载期间发生的。
  • 当主线程需要在WebThread 上运行代码时会发生死锁,导致它无限期地阻塞等待无限循环结束
  • 在生产代码中,UIWebViewUIViewControllerIBOutlet 属性,但在我们最小的可重现示例中,我们试图尽可能避免 IB 魔法。

【问题讨论】:

    标签: ios objective-c uiwebview


    【解决方案1】:

    您可能希望将 webview 对象作为属性添加到您的 WVTesterViewController 类:

    @property (weak, nonatomic) IBOutlet UIWebView *yourWebView;
    

    并在您的实现文件(.m 文件)中设置您的 Web 视图属性的委托,如下所示:

    [self.yourWebView setDelegate:self];
    [self.yourWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://yourWebsiteURL.com"]]];
    

    尝试使用 NSURLProtocol,您将更好地控制加载和加载进度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-08
      • 2015-12-04
      • 1970-01-01
      • 2021-10-13
      • 2011-07-12
      • 1970-01-01
      相关资源
      最近更新 更多