From 25620fe8391183adf7ed15ebdd8c15e8802e95c6 Mon Sep 17 00:00:00 2001 From: Zachary Waldowski Date: Sat, 28 Apr 2012 17:57:41 -0400 Subject: [PATCH] Restore iOS 4.0 compatibility. Signed-off-by: Zachary Waldowski --- AFNetworking/AFHTTPRequestOperation.m | 22 +++++++++++-------- CHANGES | 2 -- .../project.pbxproj | 2 ++ README.md | 2 +- .../project.pbxproj | 2 ++ 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/AFNetworking/AFHTTPRequestOperation.m b/AFNetworking/AFHTTPRequestOperation.m index 67b34bf..3c3b75e 100644 --- a/AFNetworking/AFHTTPRequestOperation.m +++ b/AFNetworking/AFHTTPRequestOperation.m @@ -54,10 +54,10 @@ NSSet * AFContentTypesFromHTTPHeader(NSString *string) { return [NSSet setWithSet:mutableContentTypes]; } -static void AFSwizzleClassMethodWithClassAndSelectorUsingBlock(Class klass, SEL selector, void *block) { +static void AFSwizzleClassMethodWithImplementation(Class klass, SEL selector, IMP implementation) { Method originalMethod = class_getClassMethod(klass, selector); - IMP implementation = imp_implementationWithBlock(block); - class_replaceMethod(objc_getMetaClass([NSStringFromClass(klass) UTF8String]), selector, implementation, method_getTypeEncoding(originalMethod)); + if (method_getImplementation(originalMethod) != implementation) + class_replaceMethod(objc_getMetaClass([NSStringFromClass(klass) UTF8String]), selector, implementation, method_getTypeEncoding(originalMethod)); } static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) { @@ -261,6 +261,10 @@ NSString * AFCreateIncompleteDownloadDirectoryPath(void) { #pragma mark - AFHTTPRequestOperation +static id AFStaticClassValueImplementation(id self, SEL _cmd) { + return objc_getAssociatedObject([self class], _cmd); +} + + (NSIndexSet *)acceptableStatusCodes { return [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)]; } @@ -268,9 +272,9 @@ NSString * AFCreateIncompleteDownloadDirectoryPath(void) { + (void)addAcceptableStatusCodes:(NSIndexSet *)statusCodes { NSMutableIndexSet *mutableStatusCodes = [[[NSMutableIndexSet alloc] initWithIndexSet:[self acceptableStatusCodes]] autorelease]; [mutableStatusCodes addIndexes:statusCodes]; - AFSwizzleClassMethodWithClassAndSelectorUsingBlock([self class], @selector(acceptableStatusCodes), ^(id _self) { - return mutableStatusCodes; - }); + SEL selector = @selector(acceptableStatusCodes); + AFSwizzleClassMethodWithImplementation([self class], selector, (IMP)AFStaticClassValueImplementation); + objc_setAssociatedObject([self class], selector, mutableStatusCodes, OBJC_ASSOCIATION_COPY_NONATOMIC); } + (NSSet *)acceptableContentTypes { @@ -280,9 +284,9 @@ NSString * AFCreateIncompleteDownloadDirectoryPath(void) { + (void)addAcceptableContentTypes:(NSSet *)contentTypes { NSMutableSet *mutableContentTypes = [[[NSMutableSet alloc] initWithSet:[self acceptableContentTypes] copyItems:YES] autorelease]; [mutableContentTypes unionSet:contentTypes]; - AFSwizzleClassMethodWithClassAndSelectorUsingBlock([self class], @selector(acceptableContentTypes), ^(id _self) { - return mutableContentTypes; - }); + SEL selector = @selector(acceptableContentTypes); + AFSwizzleClassMethodWithImplementation([self class], selector, (IMP)AFStaticClassValueImplementation); + objc_setAssociatedObject([self class], selector, mutableContentTypes, OBJC_ASSOCIATION_COPY_NONATOMIC); } + (BOOL)canProcessRequest:(NSURLRequest *)request { diff --git a/CHANGES b/CHANGES index 957b9a3..2ed3c18 100644 --- a/CHANGES +++ b/CHANGES @@ -1,7 +1,5 @@ = 1.0RC1 / 2012-04-25 - * Note: 1.0RC1 Deprecates iOS < 4.3 compatibility because of the addition of `AFHTTPRequestOperation +addAcceptableStatusCodes / +addAcceptableContentTypes` - * Add `AFHTTPRequestOperation +addAcceptableStatusCodes / +addAcceptableContentTypes` to dynamically add acceptable status codes and content types on the class level (Mattt Thompson) diff --git a/Mac Example/AFNetworking Mac Example.xcodeproj/project.pbxproj b/Mac Example/AFNetworking Mac Example.xcodeproj/project.pbxproj index 5d8a7dd..19c6d09 100644 --- a/Mac Example/AFNetworking Mac Example.xcodeproj/project.pbxproj +++ b/Mac Example/AFNetworking Mac Example.xcodeproj/project.pbxproj @@ -381,6 +381,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = Prefix.pch; INFOPLIST_FILE = Info.plist; + MACOSX_DEPLOYMENT_TARGET = 10.6; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; @@ -392,6 +393,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = Prefix.pch; INFOPLIST_FILE = Info.plist; + MACOSX_DEPLOYMENT_TARGET = 10.6; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; diff --git a/README.md b/README.md index 55b3a66..f413006 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,7 @@ operation.outputStream = [NSOutputStream outputStreamToMemory]; ## Requirements -AFNetworking requires either [iOS 4.3](http://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iPhoneOS4.html%23//apple_ref/doc/uid/TP40009559-SW1) and above, or [Mac OS 10.6](http://developer.apple.com/library/mac/#releasenotes/MacOSX/WhatsNewInOSX/Articles/MacOSX10_6.html#//apple_ref/doc/uid/TP40008898-SW7) and above. +AFNetworking requires either [iOS 4.0](http://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iPhoneOS4.html) and above, or [Mac OS 10.6](http://developer.apple.com/library/mac/#releasenotes/MacOSX/WhatsNewInOSX/Articles/MacOSX10_6.html#//apple_ref/doc/uid/TP40008898-SW7) and above. AFNetworking uses [`NSJSONSerialization`](http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html) if it is available. If your app targets a platform where this class is not available you can include one of the following JSON libraries to your project for AFNetworking to automatically detect and use. diff --git a/iOS Example/AFNetworking iOS Example.xcodeproj/project.pbxproj b/iOS Example/AFNetworking iOS Example.xcodeproj/project.pbxproj index 02b18ea..9356315 100644 --- a/iOS Example/AFNetworking iOS Example.xcodeproj/project.pbxproj +++ b/iOS Example/AFNetworking iOS Example.xcodeproj/project.pbxproj @@ -392,6 +392,7 @@ GCC_WARN_SIGN_COMPARE = YES; GCC_WARN_UNUSED_PARAMETER = NO; INFOPLIST_FILE = Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 4.0; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; @@ -410,6 +411,7 @@ GCC_WARN_SIGN_COMPARE = YES; GCC_WARN_UNUSED_PARAMETER = NO; INFOPLIST_FILE = Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 4.0; PRODUCT_NAME = "$(TARGET_NAME)"; VALIDATE_PRODUCT = YES; WRAPPER_EXTENSION = app;