Adding pre-processor directives to conditionally compile Mac equivalent of UIKit-dependent APIs
This commit is contained in:
parent
72090fd4e2
commit
53d61e7eb2
10 changed files with 191 additions and 20 deletions
|
|
@ -291,7 +291,6 @@ typedef enum {
|
|||
|
||||
@protocol AFHTTPClientOperation <NSObject>
|
||||
+ (BOOL)canProcessRequest:(NSURLRequest *)request;
|
||||
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request;
|
||||
+ (id)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
success:(void (^)(id object))success
|
||||
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -58,6 +64,17 @@ static inline NSString * AFImageCacheKeyFromURLAndCacheName(NSURL *url, NSString
|
|||
|
||||
[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
|
||||
|
||||
@end
|
||||
|
|
|
|||
|
|
@ -23,8 +23,13 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
#import "AFHTTPRequestOperation.h"
|
||||
|
||||
#import <Availability.h>
|
||||
|
||||
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
||||
#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.
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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,7 +56,20 @@ 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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -24,9 +24,18 @@
|
|||
|
||||
#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 ()
|
||||
@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];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,11 +20,13 @@
|
|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "AFImageRequestOperation.h"
|
||||
|
||||
#import <Availability.h>
|
||||
|
||||
#if __IPHONE_OS_VERSION_MIN_REQUIRED
|
||||
#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.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue