/ Published in: Objective C
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
- (void)setSearchingModeEnabled:(BOOL)isSearching { //when network action, toggle network indicator and activity indicator if (isSearching) { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; UIWindow *window = [UIApplication sharedApplication].keyWindow; HUD = [[MBProgressHUD alloc] initWithWindow:window]; [window addSubview:HUD]; HUD.labelText = @"Connecting"; [HUD show:YES]; } else { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; [HUD hide:YES]; [HUD removeFromSuperview]; [HUD release]; } } - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [searchBar resignFirstResponder]; NSString *searchString = [searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; self.baseURL, searchType, searchString]; NSLog(@"connecting to: %@", searchURLString); //retrieve the json data [self setSearchingModeEnabled:YES]; [searchURLString release]; [searchURL release]; } #pragma mark - #pragma mark NSURLConnection Delegate Methods { [self.responseData setLength:0]; if ([self.searchResultFileSize intValue] != NSURLResponseUnknownLength) { HUD.mode = MBProgressHUDModeDeterminate; HUD.labelText = @"Getting Results"; } NSLog(@"content-length: %@ bytes", self.searchResultFileSize); } { [self.responseData appendData:data]; NSLog(@"resourceData length: %d", [resourceLength intValue]); NSLog(@"filesize: %d", self.searchResultFileSize); NSLog(@"float filesize: %f", [self.searchResultFileSize floatValue]); HUD.progress = [resourceLength floatValue] / [self.searchResultFileSize floatValue]; NSLog(@"progress: %f", [resourceLength floatValue] / [self.searchResultFileSize floatValue]); } { [self setSearchingModeEnabled:NO]; initWithFormat:@"Connection failed: %@", [error description]]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" message:detailMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; [detailMessage release]; } { [self setSearchingModeEnabled:NO]; [connection release]; [self.responseData release]; //parse the result and remove outermost wrapper SBJSON *parser = [[SBJSON alloc] init]; //evaluate the results, and keep only unique ones if (![[searchResultsKeepers allKeys] containsObject:[entry objectForKey:@"label"]]) { //insert the search type [modifiedEntry setObject:searchType forKey:@"type"]; [searchResultsKeepers setObject:modifiedEntry forKey:[modifiedEntry objectForKey:@"label"]]; [modifiedEntry release]; } } self.searchResults = searchResultsKeepers; [searchResultsKeepers release]; [parser release]; [jsonResponse release]; [resultsView reloadData]; }