Ekr*_*kra 25 iphone cocoa-touch uiwebview ios
我正在使用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]);
}
Run Code Online (Sandbox Code Playgroud)
请告诉我出了什么问题.我没有在didFailLoadWithError委托方法中收到任何错误.
注意: - 我加载的文件很大,比如3 MB.
谢谢
============= EDITED ==================
当我加载非常庞大的文件时,代表是在很长一段时间后才出现的,我无法注意到但是对于小文件,一切都工作正常
Say*_*ain 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]
}
Run Code Online (Sandbox Code Playgroud)
在以下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]);
}
Run Code Online (Sandbox Code Playgroud)
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];
}
Run Code Online (Sandbox Code Playgroud)
removeLoadingView方法
-(void) removeLoadingView
{
[loadingView removeFromSuperView];
}
Run Code Online (Sandbox Code Playgroud)
那你的webview真的完成了加载吗?你也应该实现webView:didFailLoadWithError:
,以捕捉失败.
既然你没有得到失败或成功的消息.您尝试加载的数据可能有问题.
尝试告诉webView加载HTML字符串("Hello World!"),看看是否成功.如果是,那么问题在于您的数据资源或其路径.
归档时间: |
|
查看次数: |
55011 次 |
最近记录: |