4

I have created a NSObject class and included , in the init i create a uiwebview set the delegate to self and send the load request.

For some reason webViewDidFinishLoad or didFailLoadWithError never get fired. I can't figure why.

//
//  RXBTest.h
#import <Foundation/Foundation.h>
@interface RXBTest : NSObject <UIWebViewDelegate>
@end

//  RXBTest.m
//  pageTest
#import "RXBTest.h"
@implementation RXBTest
- (id) init
{
     if((self=[super init])){
         UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
         [webView setDelegate:self];

         [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]]];
     }
     return self;
}   
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
     NSLog(@"ERROR LOADING WEBPAGE: %@", error);
}
- (void) webViewDidFinishLoad:(UIWebView*)webView
{
     NSLog(@"finished");
}
@end

anybody has any ideas?

thanks rudi

rudirudi
  • 73
  • 1
  • 5
  • are you using `ARC` or `MRC`? – holex Jan 30 '13 at 20:49
  • I'm using ARC and ios6 min. – rudirudi Jan 30 '13 at 21:08
  • that is the problem then. the local `UIWebView` will be deallocated immediately when you run out of the scope because nothing keeps the object alive... but it was also mentioned with the obvious solution by someone else. – holex Jan 30 '13 at 22:19

2 Answers2

5

If you are using ARC, then the problem is that your webView variable is local to the init method and therefore is getting deallocated before the web view finishes loading. Try adding the web view as an instance variable:

@interface RXBTest : NSObject <UIWebViewDelegate>
{
    UIWebView* webView;
}
@end

@implementation RXBTest
- (id) init
{
    if((self=[super init])){
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
        [webView setDelegate:self];

        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/"]]];
    }
    return self;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"ERROR LOADING WEBPAGE: %@", error);
}
- (void) webViewDidFinishLoad:(UIWebView*)webView
{
    NSLog(@"finished");
}
@end

If you are not using ARC, you will need to remember to release your webView object in the dealloc method as well.

Mathew
  • 1,788
  • 15
  • 27
2

you forgot to add this in your header file (.h):

#import <UIKit/UIWebView.h>
  • Use the method: `- (void)webViewDidStartLoad:(UIWebView *)webView` and put an `NSLog()` in there and see if it gets logged, if so, than it means the webView *does* load. –  Jan 30 '13 at 21:11