UIWebView webViewDidFinishLoad 没有被调用 iOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6031034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIWebView webViewDidFinishLoad not getting called iOS
提问by Ekra
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.
我正在使用 UIWebView 从文档目录加载一些文件。我已经设置的UIWebView的委托,我回应委托的2个方法是
webViewDidStartLoad
和webViewDidFinishLoad
我收到了webViewDidStartLoad
,但我不能接受webViewDidFinishLoad
的方法。
Below is the code:
下面是代码:
@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]);
}
Please let me know what is going wrong. I am not getting any error in didFailLoadWithError delegate method.
请让我知道出了什么问题。我在 didFailLoadWithError 委托方法中没有收到任何错误。
Note:- the file that I am loading are huge say 3 MB.
注意:- 我正在加载的文件很大,比如 3 MB。
Thanks
谢谢
=============EDITED==================
==============编辑==================
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
当我加载非常大的文件时,委托是在很长一段时间后出现的,我无法注意到,但对于小文件,一切正常
回答by SayeedHussain
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]
}
Call removeLoadingView method in the following UIWebViewDelegate methods
在以下 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]);
}
The showActivityIndicator method
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];
}
The removeLoadingView method
removeLoadingView 方法
-(void) removeLoadingView
{
[loadingView removeFromSuperView];
}
回答by CharlieMezak
Well, does your webview actually finish loading? You should implement the webView:didFailLoadWithError:
, too to catch failures.
那么,你的 webview 真的完成加载了吗?您也应该实现webView:didFailLoadWithError:
, 以捕获失败。
Since you're not getting either the failure or success message. There might be something wrong with the data that you're trying to load.
由于您没有收到失败或成功消息。您尝试加载的数据可能有问题。
Try telling the webView to load an HTML string ("Hello World!"), and see if that succeeds. If it does, then the problem is with your data resource or the path to it.
尝试告诉 webView 加载一个 HTML 字符串(“Hello World!”),看看是否成功。如果是这样,则问题出在您的数据资源或其路径上。
回答by Prithivi
WebView Delegates
WebView 委托
- Download MBProgessHUD
- Add to your Project
- When the page begins load loading (*) progess started and hide after page loaded successfully
- 下载 MBProgessHUD
- 添加到您的项目
- 当页面开始加载加载 (*) 过程开始并在页面加载成功后隐藏
.h file
.h 文件
@interface WebViewViewController : UIViewController <MBProgressHUDDelegate, UIWebViewDelegate>
{
MBProgressHUD *HUD;
}
.m File
.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];
}
回答by woakley5
Make sure to implement the delegate ( ) in the view's .h file and connect the delegate to the view. This fixed it for me.
确保在视图的 .h 文件中实现委托 ( ) 并将委托连接到视图。这为我修好了。
回答by Deepak Danduprolu
Probably it is experiencing an error. Implement –webView:didFailLoadWithError:
and check.
可能它遇到了错误。实施–webView:didFailLoadWithError:
和检查。
回答by Gami Nilesh
.h file
.h 文件
@property (retain, nonatomic) IBOutlet UIWebView *webview;
.m file
.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;
}