简体   繁体   English

iOS UIWebView loadRequest花费太长时间

[英]iOS UIWebView loadRequest takes too long

I have one UIWebView in my app, 我的应用程序中有一个UIWebView

-(void)viewDidLaod{
    NSURL *url = [NSURL URLWithString:@"some url string"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:30.0];        
    myWbView.delegate = self;
        [myWebView loadRequest:request];

        if ([timer isValid]) {
            [timer invalidate];
        }else{
            timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(addSec:) userInfo:nil repeats:YES];
             //counting seconds
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    if (webView.isloading){
    NSLog(@"LOADING");//it takes 5-8 seconds in average keep logging this
    }else{
    NSLog(@"FINISH");//since the request started,8-15 seconds later this is logged out
    }
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"LOAD FAIL: %@",error);
}

here's the webpage I am trying to load, I put the webview controller in a pageViewController that every time I swipe to the next page a new load request starts. 这是我要加载的网页 ,我将webview控制器放在pageViewController ,每次我滑动到下一页时,都会启动新的加载请求。

(I do stop any unfinished loading when the swiped out views dealloc, so it won't be blocking the web tread) (当滑出的视图取消分配时,我会停止所有未完成的加载,因此不会阻塞Web界面)

sometimes i got some error messages like: 有时我会收到一些错误消息,例如:

didFialLoadWithError中的错误

As a newbie to app development, I totally have no idea what's wrong with my code, is it some thread got stuck or the webpage using JavaScript/HTML5 that matters the speed? 作为应用程序开发的新手,我完全不知道我的代码有什么问题,是某个线程卡住了还是使用JavaScript / HTML5的网页对速度至关重要?

I had looked into the Flipboard app (iOS7 version) which uses webview to load content page,it responses immediately after user taps any post on it. 我调查了Flipboard应用程序(iOS7版本),该应用程序使用webview加载内容页面,在用户点击任何内容后立即响应。 Even if the loading speed of UIWebView is normally slow, a faster performance is possible. 即使UIWebView的加载速度通常很慢,也可以实现更快的性能。

Any suggested guide link / background knowledge I should have known would be appreciated. 我应该知道的任何建议的指南链接/背景知识将不胜感激。

PS: Since another better stuff WKWebView is only supported in iOS8 and later (in my app WKWebView loads in the same speed as UIWebView , but scores higher at HTML5 test site ), I still need UIWebView for iOS7. PS:由于只有在iOS8及更高版本中才支持WKWebView, WKWebView是另一个更好的东西(在我的应用中, WKWebView加载速度与UIWebView相同,但在HTML5测试站点上得分更高),因此我仍然需要iOS7的UIWebView。

Well, the NSLocalizedDescription of the error you printed says what's going on: "Too many HTTP redirects." 好吧,您打印的错误的NSLocalizedDescription指出了正在发生的事情:“ HTTP重定向太多。”

When you try to access a web page, rather than giving you the page you're trying to reach, the server can reply with a redirect—an address for a different page you should go to instead. 当您尝试访问网页时,服务器可以给您重定向,而不是为您提供要访问的页面,而是重定向到另一个页面,而您应该转到该地址。 For instance, accessing a shortened link will typically redirect you to the real page; 例如,访问缩短的链接通常会将您重定向到真实页面; a password-protected website might redirect you to a sign-in page; 受密码保护的网站可能会将您重定向到登录页面; or a site that's been redesigned might redirect old addresses to their new locations. 否则经过重新设计的网站可能会将旧地址重定向到其新位置。

However, if not implemented carefully, this feature could end up locking up a browser by redirecting it infinitely: for instance, imagine if www.example.com/a redirected you to www.example.com/b , and www.example.com/b redirected you back to www.example.com/a . 但是,如果不仔细实施,此功能最终可能会通过无限重定向来锁定浏览器:例如,假设www.example.com/a将您重定向到www.example.com/bwww.example.com/b将您重定向回www.example.com/a So browsers impose a limit on the number of redirects they'll follow—usually about ten or fifteen—to avoid getting stuck in these kinds of loops. 因此,浏览器对其遵循的重定向数量施加了限制(通常为10或15个),以避免陷入此类循环。

The error you're getting means that, for some reason, accessing that page is causing your browser to be redirected many times in a row—so many that it just assumed something must be wrong and gave up. 您收到的错误表示,由于某种原因,访问该页面导致浏览器连续多次重定向-如此之多,以至于它只是假定一定有问题并放弃了。

Sitting here without a copy of your app to examine, I can't tell you what those redirects are, let alone why they're being issued, but you can at least answer the first question using the Safari Web Inspector. 坐在这里,没有您的应用程序副本可供检查,我无法告诉您这些重定向是什么,更不用说为什么要发布这些重定向了,但是您至少可以使用Safari Web Inspector回答第一个问题。 You'll find it in Safari's "Develop" menu, which can be enabled in Safari preferences; 您可以在Safari的“开发”菜单中找到它,可以在Safari偏好设置中启用该菜单; there are options in that menu to inspect web pages in iOS devices or simulators. 该菜单中有一些选项可以检查iOS设备或模拟器中的网页。 You may have to turn the Web Inspector option on in the device's or simulator's Safari settings as well. 您可能还必须在设备或模拟器的Safari设置中打开“ Web检查器”选项。

The Web Inspector's Timeline tool can tell you what redirects you're getting sent through. Web检查器的时间轴工具可以告诉您通过什么重定向发送。 Once you know that, you may be able to figure out why it's doing what it's doing, and maybe even tweak your app to work around the issue. 一旦知道了这一点,您就可以弄清它为什么这样做了,甚至可以调整您的应用程序来解决该问题。 (For example, the user agent string your UIWebView is using might be confusing the servers involved. This is a complete guess, though, so don't assume that that's what's going on here.) (例如,您的UIWebView使用的用户代理字符串可能会使所涉及的服务器感到困惑。不过,这是一个完整的猜测,因此,请不要以为这就是问题所在。)

Move the request with performselector inbackground, Else it blocks your UI thread. 使用performselector在后台移动请求,否则它会阻塞您的UI线程。 Another point, the UIWebView object is heavy, 还有一点,UIWebView对象很重,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM