I have an app which has UITabBar with 5 views, each attached to a different UIWebView. Each of the WebViews responds to:
webViewDidStartLoad:(UIWebView *)webView
webViewDidFinishLoad:(UIWebView *)webView
Those two are responsible for displaying a loading screen (separate image for each tab, toggled visible or not) and activity indicator.
It all works fine when the page is loaded to WebView. User taps a link on the page, loading image is displayed along with the activity indicator. When the page is loaded they both disappear and new website is presented.
The problem is when user taps on one of the TabBar items. App intercepts the event and launches a method in appropriate view, which is refreshing the page.
The problem: after the tap view changes immediately to the other WebView, however loading screen takes a long time to appear (from what I gather it only shows when the page passed to homeView starts loading) and I can't figure out why. Displaying loading items is the first thing the app should do after method is called.
Here is the code that calls a method from TabBar controller:
[hv performSelector:@selector(goToPage) withObject:nil afterDelay:0.0];
and here is the goToPage method:
- (void) goToPage
{
homeView.delegate = self;
self.showLoading;
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat: @"%@/seed.php", appURL]];
NSString *body = [NSString stringWithFormat: @"uid=%@", uID];
NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: data];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
[request release];
NSString *seedString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSArray *seedArray = [seedString componentsSeparatedByString:@":::"];
NSString *eventNumber = [[seedArray objectAtIndex:0] retain];
NSString *passedSid = [[seedArray objectAtIndex:1] retain];
if (!seedString) {
// can't connect
NSLog(@"Can't connect.");
}else {
// connected
NSLog(@"Events: %@", eventNumber);
if(![evtNo isEqualToString:eventNumber]){
evtNo = eventNumber;
if(![evtNo isEqualToString:@"0"]){
AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
}
}
}
NSURL *urlView = [NSURL URLWithString: [NSString stringWithFormat: @"%@/index.php", appURL]];
NSString *bodyView = [NSString stringWithFormat: @"sid=%@", passedSid];
NSData *dataView = [bodyView dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *requestView = [[NSMutableURLRequest alloc]initWithURL:urlView];
[requestView setHTTPMethod: @"POST"];
[requestView setHTTPBody: dataView];
[homeView loadRequest: requestView];
if([evtNo isEqualToString:@"0"]){
// clearing badge
[[[[[self tabBarController] viewControllers] objectAtIndex: 0] tabBarItem] setBadgeValue: nil];
}else{
[[[[[self tabBarController] viewControllers] objectAtIndex: 0] tabBarItem] setBadgeValue: evtNo];
}
}
The goal I have is to display the loading image the moment user taps the TabBar item and remain visible until page stops loading.
The solution to this issue was to launch the method goToPage without the delay, display loading screen in goToPage and move the time consuming bits into another method inside that class, launched using persormSelector.