UIWebView webViewDidFinishLoad not getting called iOS(UIWebView webViewDidFinishLoad 没有被调用 iOS)
问题描述
我正在使用 UIWebView 从文档目录加载一些文件.我已经设置了 UIWebView 的委托,我正在响应 2 种委托方法,即webViewDidStartLoad
和 webViewDidFinishLoad
我收到 webViewDidStartLoad
但我没有收到 webViewDidFinishLoad
方法.
I am loading some file from document directory using UIWebView. I have set the delegate of UIWebView and I am responding to 2 methods of delegate that is
webViewDidStartLoad
and webViewDidFinishLoad
I am receiving the webViewDidStartLoad
But I am not receiving webViewDidFinishLoad
method.
下面是代码:
@interface MyView: UIViewController <UIWebViewDelegate> {
UIWebView *webView;
}
@property (nonatomic, retain) UIWebView *webView;
========================= Class ===========================
-(void)viewDidLoad {
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
mWebView = [[UIWebView alloc] initWithFrame:webFrame];
mWebView.delegate = self;
mWebView.scalesPageToFit = YES;
[self.view addSubview:mWebView];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", pathString]];
[mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]] ];
}
// Delegate methods
-(void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"start");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"finish");
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(@"Error for WEBVIEW: %@", [error description]);
}
请让我知道出了什么问题.我在 didFailLoadWithError 委托方法中没有收到任何错误.
Please let me know what is going wrong. I am not getting any error in didFailLoadWithError delegate method.
注意:- 我正在加载的文件很大,比如 3 MB.
Note:- the file that I am loading are huge say 3 MB.
谢谢
=============已编辑==================
当我加载非常大的文件时,代理在很长一段时间后才出现,我没有注意到,但对于小文件,一切正常
As I was loading very huge File the delegate was coming after very long duration that I was not able to notice but for small files everything is working fine
推荐答案
嘿,也许你应该这样做,
Hey probably you should do this,
-(void)viewDidLoad {
//webView alloc and add to view
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
mWebView = [[UIWebView alloc] initWithFrame:webFrame];
mWebView.delegate = self;
mWebView.scalesPageToFit = YES;
[self.view addSubview:mWebView];
//path of local html file present in documentsDirectory
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", pathString]];
//load file into webView
[mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]] ];
//show activity indicator
[self showActivityIndicator]
}
在以下 UIWebViewDelegate 方法中调用 removeLoadingView 方法
Call removeLoadingView method in the following UIWebViewDelegate methods
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[self removeLoadingView];
NSLog(@"finish");
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[self removeLoadingView];
NSLog(@"Error for WEBVIEW: %@", [error description]);
}
showActivityIndicator 方法
The showActivityIndicator method
-(void) showActivityIndicator
{
//Add a UIView in your .h and give it the same property as you have given to your webView.
//Also ensure that you synthesize these properties on top of your implementation file
loadingView = [UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]
loadingView.alpha = 0.5;
//Create and add a spinner to loadingView in the center and animate it. Then add this loadingView to self.View using
[self.view addSubView:loadingView];
}
removeLoadingView 方法
The removeLoadingView method
-(void) removeLoadingView
{
[loadingView removeFromSuperView];
}
这篇关于UIWebView webViewDidFinishLoad 没有被调用 iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIWebView webViewDidFinishLoad 没有被调用 iOS
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01