UIAlertView+AFNetworking.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // UIAlertView+AFNetworking.m
  2. // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #import "UIAlertView+AFNetworking.h"
  22. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
  23. #import "AFURLConnectionOperation.h"
  24. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  25. #import "AFURLSessionManager.h"
  26. #endif
  27. static void AFGetAlertViewTitleAndMessageFromError(NSError *error, NSString * __autoreleasing *title, NSString * __autoreleasing *message) {
  28. if (error.localizedDescription && (error.localizedRecoverySuggestion || error.localizedFailureReason)) {
  29. *title = error.localizedDescription;
  30. if (error.localizedRecoverySuggestion) {
  31. *message = error.localizedRecoverySuggestion;
  32. } else {
  33. *message = error.localizedFailureReason;
  34. }
  35. } else if (error.localizedDescription) {
  36. *title = NSLocalizedStringFromTable(@"Error", @"AFNetworking", @"Fallback Error Description");
  37. *message = error.localizedDescription;
  38. } else {
  39. *title = NSLocalizedStringFromTable(@"Error", @"AFNetworking", @"Fallback Error Description");
  40. *message = [NSString stringWithFormat:NSLocalizedStringFromTable(@"%@ Error: %ld", @"AFNetworking", @"Fallback Error Failure Reason Format"), error.domain, (long)error.code];
  41. }
  42. }
  43. @implementation UIAlertView (AFNetworking)
  44. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
  45. + (void)showAlertViewForTaskWithErrorOnCompletion:(NSURLSessionTask *)task
  46. delegate:(id)delegate
  47. {
  48. [self showAlertViewForTaskWithErrorOnCompletion:task delegate:delegate cancelButtonTitle:NSLocalizedStringFromTable(@"Dismiss", @"AFNetworking", @"UIAlertView Cancel Button Title") otherButtonTitles:nil, nil];
  49. }
  50. + (void)showAlertViewForTaskWithErrorOnCompletion:(NSURLSessionTask *)task
  51. delegate:(id)delegate
  52. cancelButtonTitle:(NSString *)cancelButtonTitle
  53. otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION
  54. {
  55. NSMutableArray *mutableOtherTitles = [NSMutableArray array];
  56. va_list otherButtonTitleList;
  57. va_start(otherButtonTitleList, otherButtonTitles);
  58. {
  59. for (NSString *otherButtonTitle = otherButtonTitles; otherButtonTitle != nil; otherButtonTitle = va_arg(otherButtonTitleList, NSString *)) {
  60. [mutableOtherTitles addObject:otherButtonTitle];
  61. }
  62. }
  63. va_end(otherButtonTitleList);
  64. __block __weak id<NSObject> observer = [[NSNotificationCenter defaultCenter] addObserverForName:AFNetworkingTaskDidCompleteNotification object:task queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
  65. NSError *error = notification.userInfo[AFNetworkingTaskDidCompleteErrorKey];
  66. if (error) {
  67. NSString *title, *message;
  68. AFGetAlertViewTitleAndMessageFromError(error, &title, &message);
  69. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil, nil];
  70. for (NSString *otherButtonTitle in mutableOtherTitles) {
  71. [alertView addButtonWithTitle:otherButtonTitle];
  72. }
  73. [alertView setTitle:title];
  74. [alertView setMessage:message];
  75. [alertView show];
  76. }
  77. [[NSNotificationCenter defaultCenter] removeObserver:observer];
  78. }];
  79. }
  80. #endif
  81. #pragma mark -
  82. + (void)showAlertViewForRequestOperationWithErrorOnCompletion:(AFURLConnectionOperation *)operation
  83. delegate:(id)delegate
  84. {
  85. [self showAlertViewForRequestOperationWithErrorOnCompletion:operation delegate:delegate cancelButtonTitle:NSLocalizedStringFromTable(@"Dismiss", @"AFNetworking", @"UIAlertView Cancel Button Title") otherButtonTitles:nil, nil];
  86. }
  87. + (void)showAlertViewForRequestOperationWithErrorOnCompletion:(AFURLConnectionOperation *)operation
  88. delegate:(id)delegate
  89. cancelButtonTitle:(NSString *)cancelButtonTitle
  90. otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION
  91. {
  92. NSMutableArray *mutableOtherTitles = [NSMutableArray array];
  93. va_list otherButtonTitleList;
  94. va_start(otherButtonTitleList, otherButtonTitles);
  95. {
  96. for (NSString *otherButtonTitle = otherButtonTitles; otherButtonTitle != nil; otherButtonTitle = va_arg(otherButtonTitleList, NSString *)) {
  97. [mutableOtherTitles addObject:otherButtonTitle];
  98. }
  99. }
  100. va_end(otherButtonTitleList);
  101. __block __weak id<NSObject> observer = [[NSNotificationCenter defaultCenter] addObserverForName:AFNetworkingOperationDidFinishNotification object:operation queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
  102. if (notification.object && [notification.object isKindOfClass:[AFURLConnectionOperation class]]) {
  103. NSError *error = [(AFURLConnectionOperation *)notification.object error];
  104. if (error) {
  105. NSString *title, *message;
  106. AFGetAlertViewTitleAndMessageFromError(error, &title, &message);
  107. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil, nil];
  108. for (NSString *otherButtonTitle in mutableOtherTitles) {
  109. [alertView addButtonWithTitle:otherButtonTitle];
  110. }
  111. [alertView setTitle:title];
  112. [alertView setMessage:message];
  113. [alertView show];
  114. }
  115. }
  116. [[NSNotificationCenter defaultCenter] removeObserver:observer];
  117. }];
  118. }
  119. @end
  120. #endif