Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find last load of UIWebView(Ready State 4)

I have a problem with the didFinishLoad method in UIWebView that is keeps firing. I want to try the estimatedProgress solution, however I dont know where to find those frameworks unfortunately. I am running Mountain Lion and using the latest version of XCode. Also, for the UIWebView firing multiple times, is this the best way to go or are there new methods. There seems to be a javascript answer on SO, however that did not work for me. That was dated back in 2009, and I hear that Apple rejects any app that uses a private API. Please help me out here!

Thanks Everybody!

like image 526
chaitanya.varanasi Avatar asked Aug 10 '12 20:08

chaitanya.varanasi


1 Answers

What about keeping track of the number of outstanding requests? I whipped up some test code that seems to work:

@interface WebViewDelegate : NSObject<UIWebViewDelegate>
@property ( nonatomic ) NSUInteger numberOfRunningRequests ;
@end

@implementation WebViewDelegate

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    self.numberOfRunningRequests = self.numberOfRunningRequests + 1 ;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.numberOfRunningRequests = self.numberOfRunningRequests - 1 ;
    if ( self.numberOfRunningRequests == 0 )
    {
        NSLog(@"done!\n") ;
    }
}

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    UIWebView * webView = [[ UIWebView alloc ] initWithFrame:self.window.bounds ] ;
    static WebViewDelegate * delegate = nil ;
    delegate = [[ WebViewDelegate alloc ] init ] ;
    webView.delegate = delegate ;

    [self.window addSubview:webView ] ;
    NSURLRequest * request = [ NSURLRequest requestWithURL:[ NSURL URLWithString:@"http://stackoverflow.com"] ];
    [ webView loadRequest:request ] ;
    return YES;
}

@end

(Create an Xcode sample project and replace your AppDelegate.m with this)

like image 134
nielsbot Avatar answered Oct 19 '22 23:10

nielsbot