From 53d61e7eb28966b05210915140815f21f5c0b389 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Tue, 11 Oct 2011 10:39:41 -0500 Subject: [PATCH] Adding pre-processor directives to conditionally compile Mac equivalent of UIKit-dependent APIs --- AFNetworking/AFHTTPClient.h | 1 - AFNetworking/AFHTTPRequestOperation.m | 4 -- AFNetworking/AFImageCache.h | 7 ++ AFNetworking/AFImageCache.m | 19 ++++- AFNetworking/AFImageRequestOperation.h | 27 ++++++- AFNetworking/AFImageRequestOperation.m | 95 +++++++++++++++++++++++-- AFNetworking/AFJSONRequestOperation.m | 2 +- AFNetworking/AFXMLRequestOperation.h | 6 +- AFNetworking/AFXMLRequestOperation.m | 46 +++++++++++- AFNetworking/UIImageView+AFNetworking.h | 4 +- 10 files changed, 191 insertions(+), 20 deletions(-) diff --git a/AFNetworking/AFHTTPClient.h b/AFNetworking/AFHTTPClient.h index b0d3c14..1847fc5 100644 --- a/AFNetworking/AFHTTPClient.h +++ b/AFNetworking/AFHTTPClient.h @@ -291,7 +291,6 @@ typedef enum { @protocol AFHTTPClientOperation + (BOOL)canProcessRequest:(NSURLRequest *)request; -+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request; + (id)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest success:(void (^)(id object))success failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure; diff --git a/AFNetworking/AFHTTPRequestOperation.m b/AFNetworking/AFHTTPRequestOperation.m index 6f052d5..69bc370 100644 --- a/AFNetworking/AFHTTPRequestOperation.m +++ b/AFNetworking/AFHTTPRequestOperation.m @@ -87,10 +87,6 @@ return NO; } -+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { - return request; -} - + (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest success:(void (^)(id object))success failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure diff --git a/AFNetworking/AFImageCache.h b/AFNetworking/AFImageCache.h index 66c0044..8c16508 100644 --- a/AFNetworking/AFImageCache.h +++ b/AFNetworking/AFImageCache.h @@ -51,6 +51,9 @@ #if __IPHONE_OS_VERSION_MIN_REQUIRED - (UIImage *)cachedImageForURL:(NSURL *)url cacheName:(NSString *)cacheName; +#elif __MAC_OS_X_VERSION_MIN_REQUIRED +- (NSImage *)cachedImageForURL:(NSURL *)url + cacheName:(NSString *)cacheName; #endif /** @@ -65,6 +68,10 @@ - (void)cacheImage:(UIImage *)image forURL:(NSURL *)url cacheName:(NSString *)cacheName; +#elif __MAC_OS_X_VERSION_MIN_REQUIRED +- (void)cacheImage:(NSImage *)image + forURL:(NSURL *)url + cacheName:(NSString *)cacheName; #endif @end diff --git a/AFNetworking/AFImageCache.m b/AFNetworking/AFImageCache.m index b62e5bc..ad7a2b1 100644 --- a/AFNetworking/AFImageCache.m +++ b/AFNetworking/AFImageCache.m @@ -45,6 +45,12 @@ static inline NSString * AFImageCacheKeyFromURLAndCacheName(NSURL *url, NSString { return [self objectForKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)]; } +#elif __MAC_OS_X_VERSION_MIN_REQUIRED +- (NSImage *)cachedImageForURL:(NSURL *)url + cacheName:(NSString *)cacheName +{ + return [self objectForKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)]; +} #endif #if __IPHONE_OS_VERSION_MIN_REQUIRED @@ -55,7 +61,18 @@ static inline NSString * AFImageCacheKeyFromURLAndCacheName(NSURL *url, NSString if (!image) { return; } - + + [self setObject:image forKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)]; +} +#elif __MAC_OS_X_VERSION_MIN_REQUIRED +- (void)cacheImage:(NSImage *)image + forURL:(NSURL *)url + cacheName:(NSString *)cacheName +{ + if (!image) { + return; + } + [self setObject:image forKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)]; } #endif diff --git a/AFNetworking/AFImageRequestOperation.h b/AFNetworking/AFImageRequestOperation.h index 42e5859..06e130d 100644 --- a/AFNetworking/AFImageRequestOperation.h +++ b/AFNetworking/AFImageRequestOperation.h @@ -23,8 +23,13 @@ #import #import "AFHTTPRequestOperation.h" +#import + #if __IPHONE_OS_VERSION_MIN_REQUIRED #import +#elif __MAC_OS_X_VERSION_MIN_REQUIRED +#import +#endif /** `AFImageRequestOperation` is an `NSOperation` that wraps the callback from `AFHTTPRequestOperation` to create an image from the response body, and optionally cache the image to memory. @@ -34,10 +39,18 @@ */ @interface AFImageRequestOperation : AFHTTPRequestOperation { @private +#if __IPHONE_OS_VERSION_MIN_REQUIRED UIImage *_responseImage; +#elif __MAC_OS_X_VERSION_MIN_REQUIRED + NSImage *_responseImage; +#endif } +#if __IPHONE_OS_VERSION_MIN_REQUIRED @property (readonly, nonatomic, retain) UIImage *responseImage; +#elif __MAC_OS_X_VERSION_MIN_REQUIRED +@property (readonly, nonatomic, retain) NSImage *responseImage; +#endif /** Creates and returns an `AFImageRequestOperation` object and sets the specified success callback. @@ -47,8 +60,13 @@ @return A new image request operation */ +#if __IPHONE_OS_VERSION_MIN_REQUIRED + (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest success:(void (^)(UIImage *image))success; +#elif __MAC_OS_X_VERSION_MIN_REQUIRED ++ (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(NSImage *image))success; +#endif /** Creates and returns an `AFImageRequestOperation` object and sets the specified success callback. @@ -61,11 +79,18 @@ @return A new image request operation */ +#if __IPHONE_OS_VERSION_MIN_REQUIRED + (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest imageProcessingBlock:(UIImage *(^)(UIImage *))imageProcessingBlock cacheName:(NSString *)cacheNameOrNil success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; +#elif __MAC_OS_X_VERSION_MIN_REQUIRED ++ (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + imageProcessingBlock:(NSImage *(^)(NSImage *))imageProcessingBlock + cacheName:(NSString *)cacheNameOrNil + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSImage *image))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; +#endif @end -#endif diff --git a/AFNetworking/AFImageRequestOperation.m b/AFNetworking/AFImageRequestOperation.m index 44e9309..c31cd3d 100644 --- a/AFNetworking/AFImageRequestOperation.m +++ b/AFNetworking/AFImageRequestOperation.m @@ -32,9 +32,12 @@ static dispatch_queue_t image_request_operation_processing_queue() { return af_image_request_operation_processing_queue; } -#if __IPHONE_OS_VERSION_MIN_REQUIRED @interface AFImageRequestOperation () +#if __IPHONE_OS_VERSION_MIN_REQUIRED @property (readwrite, nonatomic, retain) UIImage *responseImage; +#elif __MAC_OS_X_VERSION_MIN_REQUIRED +@property (readwrite, nonatomic, retain) NSImage *responseImage; +#endif + (NSSet *)defaultAcceptableContentTypes; + (NSSet *)defaultAcceptablePathExtensions; @@ -43,6 +46,7 @@ static dispatch_queue_t image_request_operation_processing_queue() { @implementation AFImageRequestOperation @synthesize responseImage = _responseImage; +#if __IPHONE_OS_VERSION_MIN_REQUIRED + (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest success:(void (^)(UIImage *image))success { @@ -52,12 +56,25 @@ static dispatch_queue_t image_request_operation_processing_queue() { } } failure:nil]; } +#elif __MAC_OS_X_VERSION_MIN_REQUIRED ++ (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(NSImage *image))success +{ + return [self imageRequestOperationWithRequest:urlRequest imageProcessingBlock:nil cacheName:nil success:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, NSImage *image) { + if (success) { + success(image); + } + } failure:nil]; +} +#endif + +#if __IPHONE_OS_VERSION_MIN_REQUIRED + (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest - imageProcessingBlock:(UIImage *(^)(UIImage *))imageProcessingBlock - cacheName:(NSString *)cacheNameOrNil - success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success - failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure + imageProcessingBlock:(UIImage *(^)(UIImage *))imageProcessingBlock + cacheName:(NSString *)cacheNameOrNil + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure { AFImageRequestOperation *operation = [[[AFImageRequestOperation alloc] initWithRequest:urlRequest] autorelease]; @@ -95,6 +112,50 @@ static dispatch_queue_t image_request_operation_processing_queue() { return operation; } +#elif __MAC_OS_X_VERSION_MIN_REQUIRED ++ (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + imageProcessingBlock:(NSImage *(^)(NSImage *))imageProcessingBlock + cacheName:(NSString *)cacheNameOrNil + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSImage *image))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure +{ + AFImageRequestOperation *operation = [[[AFImageRequestOperation alloc] initWithRequest:urlRequest] autorelease]; + + operation.completionBlock = ^ { + if ([operation isCancelled]) { + return; + } + + dispatch_async(image_request_operation_processing_queue(), ^(void) { + if (operation.error) { + if (failure) { + dispatch_async(dispatch_get_main_queue(), ^(void) { + failure(operation.request, operation.response, operation.error); + }); + } + } else { + NSImage *image = operation.responseImage; + + if (imageProcessingBlock) { + image = imageProcessingBlock(image); + } + + if (success) { + dispatch_async(dispatch_get_main_queue(), ^(void) { + success(operation.request, operation.response, image); + }); + } + + if ([operation.request cachePolicy] != NSURLCacheStorageNotAllowed) { + [[AFImageCache sharedImageCache] cacheImage:image forURL:[operation.request URL] cacheName:cacheNameOrNil]; + } + } + }); + }; + + return operation; +} +#endif + (NSSet *)defaultAcceptableContentTypes { return [NSSet setWithObjects:@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon" @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap", nil]; @@ -120,6 +181,7 @@ static dispatch_queue_t image_request_operation_processing_queue() { [super dealloc]; } +#if __IPHONE_OS_VERSION_MIN_REQUIRED - (UIImage *)responseImage { if (!_responseImage && [self isFinished]) { if ([[UIScreen mainScreen] scale] == 2.0) { @@ -132,6 +194,15 @@ static dispatch_queue_t image_request_operation_processing_queue() { return _responseImage; } +#elif __MAC_OS_X_VERSION_MIN_REQUIRED +- (NSImage *)responseImage { + if (!_responseImage && [self isFinished]) { + self.responseImage = [[[NSImage alloc] initWithData:self.responseData] autorelease]; + } + + return _responseImage; +} +#endif #pragma mark - AFHTTPClientOperation @@ -139,6 +210,7 @@ static dispatch_queue_t image_request_operation_processing_queue() { return [[self defaultAcceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[self defaultAcceptablePathExtensions] containsObject:[[request URL] pathExtension]]; } +#if __IPHONE_OS_VERSION_MIN_REQUIRED + (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest success:(void (^)(id object))success failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure @@ -149,6 +221,17 @@ static dispatch_queue_t image_request_operation_processing_queue() { failure(response, error); }]; } +#elif __MAC_OS_X_VERSION_MIN_REQUIRED ++ (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(id object))success + failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure +{ + return [self imageRequestOperationWithRequest:urlRequest imageProcessingBlock:nil cacheName:nil success:^(NSURLRequest __unused *request, NSHTTPURLResponse __unused *response, NSImage *image) { + success(image); + } failure:^(NSURLRequest __unused *request, NSHTTPURLResponse *response, NSError *error) { + failure(response, error); + }]; +} +#endif @end -#endif diff --git a/AFNetworking/AFJSONRequestOperation.m b/AFNetworking/AFJSONRequestOperation.m index a8f548c..a268cfa 100644 --- a/AFNetworking/AFJSONRequestOperation.m +++ b/AFNetworking/AFJSONRequestOperation.m @@ -115,7 +115,7 @@ static dispatch_queue_t json_request_operation_processing_queue() { if (!_responseJSON && [self isFinished]) { NSError *error = nil; -#if __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_6 || __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_4_3 +#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_4_3 || __MAC_OS_X_VERSION_MIN_REQUIRED > __MAC_10_6 if ([NSJSONSerialization class]) { self.responseJSON = [NSJSONSerialization JSONObjectWithData:self.responseData options:0 error:&error]; } else { diff --git a/AFNetworking/AFXMLRequestOperation.h b/AFNetworking/AFXMLRequestOperation.h index 450c601..2bbde74 100644 --- a/AFNetworking/AFXMLRequestOperation.h +++ b/AFNetworking/AFXMLRequestOperation.h @@ -28,7 +28,7 @@ @interface AFXMLRequestOperation : AFHTTPRequestOperation { @private NSXMLParser *_responseXMLParser; -#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED +#if __MAC_OS_X_VERSION_MIN_REQUIRED NSXMLDocument *_responseXMLDocument; #endif NSError *_XMLError; @@ -36,7 +36,7 @@ @property (readonly, nonatomic, retain) NSXMLParser *responseXMLParser; -#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED +#if __MAC_OS_X_VERSION_MIN_REQUIRED @property (readonly, nonatomic, retain) NSXMLDocument *responseXMLDocument; #endif @@ -45,7 +45,7 @@ failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; -#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED +#if __MAC_OS_X_VERSION_MIN_REQUIRED + (AFXMLRequestOperation *)XMLDocumentRequestOperationWithRequest:(NSURLRequest *)urlRequest success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLDocument *document))success failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; diff --git a/AFNetworking/AFXMLRequestOperation.m b/AFNetworking/AFXMLRequestOperation.m index 579d9bb..d57374a 100644 --- a/AFNetworking/AFXMLRequestOperation.m +++ b/AFNetworking/AFXMLRequestOperation.m @@ -24,9 +24,18 @@ #include +static dispatch_queue_t af_xml_request_operation_processing_queue; +static dispatch_queue_t xml_request_operation_processing_queue() { + if (af_xml_request_operation_processing_queue == NULL) { + af_xml_request_operation_processing_queue = dispatch_queue_create("com.alamofire.networking.xml-request.processing", 0); + } + + return af_xml_request_operation_processing_queue; +} + @interface AFXMLRequestOperation () @property (readwrite, nonatomic, retain) NSXMLParser *responseXMLParser; -#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED +#if __MAC_OS_X_VERSION_MIN_REQUIRED @property (readwrite, nonatomic, retain) NSXMLDocument *responseXMLDocument; #endif @property (readwrite, nonatomic, retain) NSError *error; @@ -37,7 +46,7 @@ @implementation AFXMLRequestOperation @synthesize responseXMLParser = _responseXMLParser; -#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED +#if __MAC_OS_X_VERSION_MIN_REQUIRED @synthesize responseXMLDocument = _responseXMLDocument; #endif @synthesize error = _XMLError; @@ -69,6 +78,39 @@ return operation; } +#if __MAC_OS_X_VERSION_MIN_REQUIRED ++ (AFXMLRequestOperation *)XMLDocumentRequestOperationWithRequest:(NSURLRequest *)urlRequest + success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLDocument *document))success + failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure +{ + AFXMLRequestOperation *operation = [[[self alloc] initWithRequest:urlRequest] autorelease]; + operation.completionBlock = ^ { + if ([operation isCancelled]) { + return; + } + + if (operation.error) { + if (failure) { + dispatch_async(dispatch_get_main_queue(), ^(void) { + failure(operation.request, operation.response, operation.error); + }); + } + } else { + dispatch_async(xml_request_operation_processing_queue(), ^(void) { + NSXMLDocument *XMLDocument = operation.responseXMLDocument; + if (success) { + dispatch_async(dispatch_get_main_queue(), ^(void) { + success(operation.request, operation.response, XMLDocument); + }); + } + }); + } + }; + + return operation; +} +#endif + + (NSSet *)defaultAcceptableContentTypes { return [NSSet setWithObjects:@"application/xml", @"text/xml", nil]; } diff --git a/AFNetworking/UIImageView+AFNetworking.h b/AFNetworking/UIImageView+AFNetworking.h index d28514b..71ecdc0 100644 --- a/AFNetworking/UIImageView+AFNetworking.h +++ b/AFNetworking/UIImageView+AFNetworking.h @@ -20,11 +20,13 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. +#import +#import "AFImageRequestOperation.h" + #import #if __IPHONE_OS_VERSION_MIN_REQUIRED #import -#import "AFImageRequestOperation.h" /** This category adds methods to the UIKit framework's `UIImageView` class. The methods in this category provide support for loading remote images asynchronously from a URL.