Return to Snippet

Revision: 22643
at January 18, 2010 05:12 by christophercjensen


Initial Code
- (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 *searchType = [ConstantsConverter searchTypeTagToKey:self.selectedSearchType];
	
	NSString *searchString = [searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	
	NSString *searchURLString = [[NSString alloc] initWithFormat:@"%@?type=%@&search=%@", 
								 self.baseURL, searchType, searchString];
	
	NSLog(@"connecting to: %@", searchURLString);
	//retrieve the json data
	self.responseData = [[NSMutableData data] retain];
	NSURL *searchURL = [[NSURL alloc] initWithString:searchURLString];
	NSURLRequest *request = [NSURLRequest requestWithURL:searchURL];
	
	[self setSearchingModeEnabled:YES];
	[[NSURLConnection alloc] initWithRequest:request delegate:self];
	
	[searchURLString release];
	[searchURL release];
}


#pragma mark -
#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
	[self.responseData setLength:0];
	self.searchResultFileSize = [NSNumber numberWithLongLong:[response expectedContentLength]];
	if ([self.searchResultFileSize intValue] != NSURLResponseUnknownLength) {
		HUD.mode = MBProgressHUDModeDeterminate;
		HUD.labelText = @"Getting Results";
	}
	
    NSLog(@"content-length: %@ bytes", self.searchResultFileSize);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
	[self.responseData appendData:data];
	
	NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.responseData length]];
    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]);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{
	[self setSearchingModeEnabled:NO];
	
	NSString *detailMessage = [[NSString alloc]
							   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];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
	[self setSearchingModeEnabled:NO];
	
	[connection release];
	
	NSString *jsonResponse = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
	[self.responseData release];
	
	NSString *searchType = [ConstantsConverter searchTypeTagToKey:self.selectedSearchType];
	
	//parse the result and remove outermost wrapper
	SBJSON *parser = [[SBJSON alloc] init];
	NSArray *tempResults = [[parser objectWithString:jsonResponse error:nil] objectForKey:searchType];
	NSMutableDictionary *searchResultsKeepers = [[NSMutableDictionary alloc] init];
	
	//evaluate the results, and keep only unique ones
	for (NSDictionary *entry in tempResults) {
		if (![[searchResultsKeepers allKeys] containsObject:[entry objectForKey:@"label"]]) {
			//insert the search type
			NSMutableDictionary *modifiedEntry = [[NSMutableDictionary alloc] initWithDictionary:entry];
			[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];
}

Initial URL


Initial Description


Initial Title
MBProgressHUD with an asynchronous NSURLConnection call

Initial Tags
c

Initial Language
Objective C