我正在使用UIWebView从文档目录加载一些文件。我已经设置了UIWebView的委托,我正在响应委托的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委托方法中没有收到任何错误。
注意: - 我正在加载的文件很大,比如3 MB。
谢谢
============= EDITED ==================
当我加载非常庞大的文件时,代表在很长一段时间后才出现,我无法注意到但是对于小文件,一切都工作正常
答案 0 :(得分:26)
嘿,你可能应该这样做,
-(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];
}
答案 1 :(得分:8)
嗯,您的网页视图是否真的完成加载?您也应该实现webView:didFailLoadWithError:
以捕获失败。
因为您没有收到失败或成功消息。您尝试加载的数据可能有问题。
尝试告诉webView加载HTML字符串(“Hello World!”),看看是否成功。如果是,那么问题在于您的数据资源或其路径。
答案 2 :(得分:3)
WebView代理
.h文件
@interface WebViewViewController : UIViewController <MBProgressHUDDelegate, UIWebViewDelegate>
{
MBProgressHUD *HUD;
}
.m文件
- (void)webViewDidStartLoad:(UIWebView *)webView
{
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Loading";
[HUD show:YES];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[HUD hide:YES];
}
答案 3 :(得分:2)
可能是出现了错误。实施–webView:didFailLoadWithError:
并检查。
答案 4 :(得分:2)
确保在视图的.h文件中实现delegate()并将委托连接到视图。这为我修好了。
答案 5 :(得分:0)
.h文件
@property (retain, nonatomic) IBOutlet UIWebView *webview;
.m文件
-(void)viewDidLoad {
NSString *urlAddress = @"YOUR_URL";
_webview.scalesPageToFit = YES;
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[_webview loadRequest:requestObj];
}
- (void)webViewDidStartLoad:(UIWebView *)webView;
{
[self.indicater_web startAnimating];
}
//a web view starts loading
- (void)webViewDidFinishLoad:(UIWebView *)webView;
{
[self.indicater_web stopAnimating];
}
//web view finishes loading
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
{
[self.indicater_web stopAnimating];
}
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)rq
{
[self.indicater_web startAnimating];
return YES;
}