-->

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

2020-08-22 05:01发布

问题:

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!

回答1:

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)



回答2:

If by frameworks you mean the .h files imported in the code on the page you linked to, here's the link to directly download them. For further help you'll need to elaborate more.

EDIT: Additionally, please see: NSURLConnection NSURLRequest proxy for asynchronous web service calls

The linked answer explains how to NSURLRequest to request a connection and get different callbacks for different states of the request, such as - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data