MBProgressHUD with an asynchronous NSURLConnection call


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. - (void)setSearchingModeEnabled:(BOOL)isSearching
  2. {
  3. //when network action, toggle network indicator and activity indicator
  4. if (isSearching) {
  5. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  6.  
  7. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  8. HUD = [[MBProgressHUD alloc] initWithWindow:window];
  9. [window addSubview:HUD];
  10. HUD.labelText = @"Connecting";
  11. [HUD show:YES];
  12. } else {
  13. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
  14.  
  15. [HUD hide:YES];
  16. [HUD removeFromSuperview];
  17. [HUD release];
  18. }
  19.  
  20. }
  21.  
  22.  
  23. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  24. {
  25. [searchBar resignFirstResponder];
  26.  
  27. NSString *searchType = [ConstantsConverter searchTypeTagToKey:self.selectedSearchType];
  28.  
  29. NSString *searchString = [searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  30.  
  31. NSString *searchURLString = [[NSString alloc] initWithFormat:@"%@?type=%@&search=%@",
  32. self.baseURL, searchType, searchString];
  33.  
  34. NSLog(@"connecting to: %@", searchURLString);
  35. //retrieve the json data
  36. self.responseData = [[NSMutableData data] retain];
  37. NSURL *searchURL = [[NSURL alloc] initWithString:searchURLString];
  38. NSURLRequest *request = [NSURLRequest requestWithURL:searchURL];
  39.  
  40. [self setSearchingModeEnabled:YES];
  41. [[NSURLConnection alloc] initWithRequest:request delegate:self];
  42.  
  43. [searchURLString release];
  44. [searchURL release];
  45. }
  46.  
  47.  
  48. #pragma mark -
  49. #pragma mark NSURLConnection Delegate Methods
  50. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  51. {
  52. [self.responseData setLength:0];
  53. self.searchResultFileSize = [NSNumber numberWithLongLong:[response expectedContentLength]];
  54. if ([self.searchResultFileSize intValue] != NSURLResponseUnknownLength) {
  55. HUD.mode = MBProgressHUDModeDeterminate;
  56. HUD.labelText = @"Getting Results";
  57. }
  58.  
  59. NSLog(@"content-length: %@ bytes", self.searchResultFileSize);
  60. }
  61.  
  62. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  63. {
  64. [self.responseData appendData:data];
  65.  
  66. NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.responseData length]];
  67. NSLog(@"resourceData length: %d", [resourceLength intValue]);
  68. NSLog(@"filesize: %d", self.searchResultFileSize);
  69. NSLog(@"float filesize: %f", [self.searchResultFileSize floatValue]);
  70. HUD.progress = [resourceLength floatValue] / [self.searchResultFileSize floatValue];
  71. NSLog(@"progress: %f", [resourceLength floatValue] / [self.searchResultFileSize floatValue]);
  72. }
  73.  
  74. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  75. {
  76. [self setSearchingModeEnabled:NO];
  77.  
  78. NSString *detailMessage = [[NSString alloc]
  79. initWithFormat:@"Connection failed: %@",
  80. [error description]];
  81.  
  82. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure" message:detailMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  83.  
  84. [alert show];
  85. [alert release];
  86. [detailMessage release];
  87. }
  88.  
  89. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  90. {
  91. [self setSearchingModeEnabled:NO];
  92.  
  93. [connection release];
  94.  
  95. NSString *jsonResponse = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding];
  96. [self.responseData release];
  97.  
  98. NSString *searchType = [ConstantsConverter searchTypeTagToKey:self.selectedSearchType];
  99.  
  100. //parse the result and remove outermost wrapper
  101. SBJSON *parser = [[SBJSON alloc] init];
  102. NSArray *tempResults = [[parser objectWithString:jsonResponse error:nil] objectForKey:searchType];
  103. NSMutableDictionary *searchResultsKeepers = [[NSMutableDictionary alloc] init];
  104.  
  105. //evaluate the results, and keep only unique ones
  106. for (NSDictionary *entry in tempResults) {
  107. if (![[searchResultsKeepers allKeys] containsObject:[entry objectForKey:@"label"]]) {
  108. //insert the search type
  109. NSMutableDictionary *modifiedEntry = [[NSMutableDictionary alloc] initWithDictionary:entry];
  110. [modifiedEntry setObject:searchType forKey:@"type"];
  111. [searchResultsKeepers setObject:modifiedEntry forKey:[modifiedEntry objectForKey:@"label"]];
  112. [modifiedEntry release];
  113. }
  114. }
  115. self.searchResults = searchResultsKeepers;
  116.  
  117. [searchResultsKeepers release];
  118. [parser release];
  119. [jsonResponse release];
  120. [resultsView reloadData];
  121. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.