Adding pre-processor directives to conditionally compile Mac equivalent of UIKit-dependent APIs

This commit is contained in:
Mattt Thompson 2011-10-11 10:39:41 -05:00
parent 72090fd4e2
commit 53d61e7eb2
10 changed files with 191 additions and 20 deletions

View file

@ -291,7 +291,6 @@ typedef enum {
@protocol AFHTTPClientOperation <NSObject> @protocol AFHTTPClientOperation <NSObject>
+ (BOOL)canProcessRequest:(NSURLRequest *)request; + (BOOL)canProcessRequest:(NSURLRequest *)request;
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request;
+ (id)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest + (id)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(id object))success success:(void (^)(id object))success
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure; failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;

View file

@ -87,10 +87,6 @@
return NO; return NO;
} }
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
return request;
}
+ (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest + (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(id object))success success:(void (^)(id object))success
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure

View file

@ -51,6 +51,9 @@
#if __IPHONE_OS_VERSION_MIN_REQUIRED #if __IPHONE_OS_VERSION_MIN_REQUIRED
- (UIImage *)cachedImageForURL:(NSURL *)url - (UIImage *)cachedImageForURL:(NSURL *)url
cacheName:(NSString *)cacheName; cacheName:(NSString *)cacheName;
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
- (NSImage *)cachedImageForURL:(NSURL *)url
cacheName:(NSString *)cacheName;
#endif #endif
/** /**
@ -65,6 +68,10 @@
- (void)cacheImage:(UIImage *)image - (void)cacheImage:(UIImage *)image
forURL:(NSURL *)url forURL:(NSURL *)url
cacheName:(NSString *)cacheName; cacheName:(NSString *)cacheName;
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
- (void)cacheImage:(NSImage *)image
forURL:(NSURL *)url
cacheName:(NSString *)cacheName;
#endif #endif
@end @end

View file

@ -45,6 +45,12 @@ static inline NSString * AFImageCacheKeyFromURLAndCacheName(NSURL *url, NSString
{ {
return [self objectForKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)]; 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 #endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED #if __IPHONE_OS_VERSION_MIN_REQUIRED
@ -58,6 +64,17 @@ static inline NSString * AFImageCacheKeyFromURLAndCacheName(NSURL *url, NSString
[self setObject:image forKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)]; [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 #endif
@end @end

View file

@ -23,8 +23,13 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "AFHTTPRequestOperation.h" #import "AFHTTPRequestOperation.h"
#import <Availability.h>
#if __IPHONE_OS_VERSION_MIN_REQUIRED #if __IPHONE_OS_VERSION_MIN_REQUIRED
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
#import <Cocoa/Cocoa.h>
#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. `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 { @interface AFImageRequestOperation : AFHTTPRequestOperation {
@private @private
#if __IPHONE_OS_VERSION_MIN_REQUIRED
UIImage *_responseImage; UIImage *_responseImage;
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
NSImage *_responseImage;
#endif
} }
#if __IPHONE_OS_VERSION_MIN_REQUIRED
@property (readonly, nonatomic, retain) UIImage *responseImage; @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. Creates and returns an `AFImageRequestOperation` object and sets the specified success callback.
@ -47,8 +60,13 @@
@return A new image request operation @return A new image request operation
*/ */
#if __IPHONE_OS_VERSION_MIN_REQUIRED
+ (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(UIImage *image))success; 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. Creates and returns an `AFImageRequestOperation` object and sets the specified success callback.
@ -61,11 +79,18 @@
@return A new image request operation @return A new image request operation
*/ */
#if __IPHONE_OS_VERSION_MIN_REQUIRED
+ (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest
imageProcessingBlock:(UIImage *(^)(UIImage *))imageProcessingBlock imageProcessingBlock:(UIImage *(^)(UIImage *))imageProcessingBlock
cacheName:(NSString *)cacheNameOrNil cacheName:(NSString *)cacheNameOrNil
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; 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 @end
#endif

View file

@ -32,9 +32,12 @@ static dispatch_queue_t image_request_operation_processing_queue() {
return af_image_request_operation_processing_queue; return af_image_request_operation_processing_queue;
} }
#if __IPHONE_OS_VERSION_MIN_REQUIRED
@interface AFImageRequestOperation () @interface AFImageRequestOperation ()
#if __IPHONE_OS_VERSION_MIN_REQUIRED
@property (readwrite, nonatomic, retain) UIImage *responseImage; @property (readwrite, nonatomic, retain) UIImage *responseImage;
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
@property (readwrite, nonatomic, retain) NSImage *responseImage;
#endif
+ (NSSet *)defaultAcceptableContentTypes; + (NSSet *)defaultAcceptableContentTypes;
+ (NSSet *)defaultAcceptablePathExtensions; + (NSSet *)defaultAcceptablePathExtensions;
@ -43,6 +46,7 @@ static dispatch_queue_t image_request_operation_processing_queue() {
@implementation AFImageRequestOperation @implementation AFImageRequestOperation
@synthesize responseImage = _responseImage; @synthesize responseImage = _responseImage;
#if __IPHONE_OS_VERSION_MIN_REQUIRED
+ (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest + (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(UIImage *image))success success:(void (^)(UIImage *image))success
{ {
@ -52,7 +56,20 @@ static dispatch_queue_t image_request_operation_processing_queue() {
} }
} failure:nil]; } 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 + (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest
imageProcessingBlock:(UIImage *(^)(UIImage *))imageProcessingBlock imageProcessingBlock:(UIImage *(^)(UIImage *))imageProcessingBlock
cacheName:(NSString *)cacheNameOrNil cacheName:(NSString *)cacheNameOrNil
@ -95,6 +112,50 @@ static dispatch_queue_t image_request_operation_processing_queue() {
return operation; 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 { + (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]; 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]; [super dealloc];
} }
#if __IPHONE_OS_VERSION_MIN_REQUIRED
- (UIImage *)responseImage { - (UIImage *)responseImage {
if (!_responseImage && [self isFinished]) { if (!_responseImage && [self isFinished]) {
if ([[UIScreen mainScreen] scale] == 2.0) { if ([[UIScreen mainScreen] scale] == 2.0) {
@ -132,6 +194,15 @@ static dispatch_queue_t image_request_operation_processing_queue() {
return _responseImage; 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 #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]]; return [[self defaultAcceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]] || [[self defaultAcceptablePathExtensions] containsObject:[[request URL] pathExtension]];
} }
#if __IPHONE_OS_VERSION_MIN_REQUIRED
+ (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest + (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(id object))success success:(void (^)(id object))success
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure
@ -149,6 +221,17 @@ static dispatch_queue_t image_request_operation_processing_queue() {
failure(response, error); 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 @end
#endif

View file

@ -115,7 +115,7 @@ static dispatch_queue_t json_request_operation_processing_queue() {
if (!_responseJSON && [self isFinished]) { if (!_responseJSON && [self isFinished]) {
NSError *error = nil; 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]) { if ([NSJSONSerialization class]) {
self.responseJSON = [NSJSONSerialization JSONObjectWithData:self.responseData options:0 error:&error]; self.responseJSON = [NSJSONSerialization JSONObjectWithData:self.responseData options:0 error:&error];
} else { } else {

View file

@ -28,7 +28,7 @@
@interface AFXMLRequestOperation : AFHTTPRequestOperation { @interface AFXMLRequestOperation : AFHTTPRequestOperation {
@private @private
NSXMLParser *_responseXMLParser; NSXMLParser *_responseXMLParser;
#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED #if __MAC_OS_X_VERSION_MIN_REQUIRED
NSXMLDocument *_responseXMLDocument; NSXMLDocument *_responseXMLDocument;
#endif #endif
NSError *_XMLError; NSError *_XMLError;
@ -36,7 +36,7 @@
@property (readonly, nonatomic, retain) NSXMLParser *responseXMLParser; @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; @property (readonly, nonatomic, retain) NSXMLDocument *responseXMLDocument;
#endif #endif
@ -45,7 +45,7 @@
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; 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 + (AFXMLRequestOperation *)XMLDocumentRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLDocument *document))success success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLDocument *document))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;

View file

@ -24,9 +24,18 @@
#include <Availability.h> #include <Availability.h>
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 () @interface AFXMLRequestOperation ()
@property (readwrite, nonatomic, retain) NSXMLParser *responseXMLParser; @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; @property (readwrite, nonatomic, retain) NSXMLDocument *responseXMLDocument;
#endif #endif
@property (readwrite, nonatomic, retain) NSError *error; @property (readwrite, nonatomic, retain) NSError *error;
@ -37,7 +46,7 @@
@implementation AFXMLRequestOperation @implementation AFXMLRequestOperation
@synthesize responseXMLParser = _responseXMLParser; @synthesize responseXMLParser = _responseXMLParser;
#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED #if __MAC_OS_X_VERSION_MIN_REQUIRED
@synthesize responseXMLDocument = _responseXMLDocument; @synthesize responseXMLDocument = _responseXMLDocument;
#endif #endif
@synthesize error = _XMLError; @synthesize error = _XMLError;
@ -69,6 +78,39 @@
return operation; 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 { + (NSSet *)defaultAcceptableContentTypes {
return [NSSet setWithObjects:@"application/xml", @"text/xml", nil]; return [NSSet setWithObjects:@"application/xml", @"text/xml", nil];
} }

View file

@ -20,11 +20,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
#import <Foundation/Foundation.h>
#import "AFImageRequestOperation.h"
#import <Availability.h> #import <Availability.h>
#if __IPHONE_OS_VERSION_MIN_REQUIRED #if __IPHONE_OS_VERSION_MIN_REQUIRED
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#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. 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.