2011-05-17 13 views
24

Estoy cargando algunos archivos del directorio de documentos con UIWebView. Configuré el delegado de UIWebView y respondo a 2 métodos de delegado que son webViewDidStartLoad y webViewDidFinishLoad Recibo el webViewDidStartLoad. Pero no recibo el método webViewDidFinishLoad.UIWebView webViewDidFinishLoad no se llama iOS

A continuación se muestra el código:

@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]); 
} 

Por favor, que me haga saber lo que está mal. No recibo ningún error en el método de delegado didFailLoadWithError.

Nota: - el archivo que estoy cargando es enorme, digamos 3 MB.

Gracias

============= ================== EDITADO

Como estaba cargando un archivo muy grande El delegado venía después de una larga duración que no pude ver, pero para archivos pequeños todo funciona bien

+0

También puede reducir 'NSString * path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @ "% @", pathString]]; 'to' NSString * path = [documentsDirectory stringByAppendingPathComponent: pathString]; ' –

+0

http://stackoverflow.com/questions/8986963/how-to-set-my-web-view-loaded-with-already- iniciar sesión -user-iphone –

Respuesta

22

Hey, probablemente, usted debe hacer esto, el método removeLoadingView

-(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] 
} 

de llamadas en los siguientes métodos UIWebViewDelegate

-(void)webViewDidFinishLoad:(UIWebView *)webView { 

    [self removeLoadingView]; 
    NSLog(@"finish"); 
} 


-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { 

    [self removeLoadingView]; 
    NSLog(@"Error for WEBVIEW: %@", [error description]); 
} 

El método 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]; 
} 

El método removeLoadingView

-(void) removeLoadingView 
{ 
    [loadingView removeFromSuperView]; 
} 
+0

Esa es una forma correcta de mostrar esa vista web cargando su solicitud ... –

2

Probablemente está experimentando un error. Implementar –webView:didFailLoadWithError: y verificar.

+0

He editado mi pregunta. Por favor, eche un vistazo. – Ekra

8

Bueno, ¿su vista web realmente termina de cargarse? Debe implementar el webView:didFailLoadWithError:, también para detectar fallas.

Dado que no recibe el mensaje de error o éxito. Puede haber algún problema con los datos que intenta cargar.

Intente decirle a webView que cargue una cadena HTML ("¡Hola mundo!") Y vea si eso tiene éxito. Si lo hace, entonces el problema está en su recurso de datos o en la ruta hacia él.

+0

Implementé el error pero no aparece en ese método. Voy a editar mi pregunta – Ekra

+1

Bueno, ¿qué sucede en realidad? ¿La vista web carga con éxito el contenido? – CharlieMezak

+0

WebView carga con éxito el contenido. Pero como estoy mostrando un archivo muy grande, digamos 3MB, lleva mucho tiempo. Así que quería mostrar un poco de progreso al usuario, pero como no puedo obtener el método de delegación correcto, no puedo hacerlo. – Ekra

3

WebView delegados

  1. Descargar MBProgessHUD
  2. añadir al proyecto
  3. Cuando la página comienza la carga de carga (*) progess comenzó y se esconden tras página se ha cargado correctamente

. h archivo

@interface WebViewViewController : UIViewController <MBProgressHUDDelegate, UIWebViewDelegate> 
    { 
    MBProgressHUD *HUD; 

    } 

.Archivo

- (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]; 
} 
2

m Asegúrese de poner en práctica el delegado() en el archivo .h de la vista y conecte el delegado a la vista. Esto me lo arregló.

0

archivo .h

@property (retain, nonatomic) IBOutlet UIWebView *webview; 

archivo .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; 
} 
Cuestiones relacionadas