带有异步NSURLConnection调用的MBProgressHUD

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带有异步NSURLConnection调用的MBProgressHUD相关的知识,希望对你有一定的参考价值。

  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. }

以上是关于带有异步NSURLConnection调用的MBProgressHUD的主要内容,如果未能解决你的问题,请参考以下文章

https 上的 NSURLConnection 同步请求

使用来自异步 NSURLConnection 的数据填充 NSImage

带有异步回调的 NSThread 与 GCD

带有 POST 的 NSURLConnection 不起作用

带有 NSURLConnection 的网络管理器

Swift3.0:NSURLConnection的使用