我正在使用 UIWebView 从文档目录加载一些文件。我已经设置了 UIWebView 的委托(delegate),我正在响应委托(delegate)的 2 种方法
webViewDidStartLoad 和 webViewDidFinishLoad 我收到了 webViewDidStartLoad 但我没有收到 webViewDidFinishLoad 方法。
代码如下:
@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 委托(delegate)方法中没有收到任何错误。
注意:- 我正在加载的文件很大,比如 3 MB。
谢谢
=============已编辑==================
当我加载非常大的文件时,代表在我无法注意到的很长一段时间后才出现,但对于小文件,一切正常
最佳答案
嘿,也许你应该这样做,
-(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 方法
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[self removeLoadingView];
NSLog(@"finish");
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[self removeLoadingView];
NSLog(@"Error for WEBVIEW: %@", [error description]);
}
showActivityIndicator 方法
-(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 方法
-(void) removeLoadingView
{
[loadingView removeFromSuperView];
}
关于iphone - UIWebView webViewDidFinishLoad 没有被调用 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6031034/