Skip to content

Commit

Permalink
iOS 4 compatibility updates:
Browse files Browse the repository at this point in the history
- Added JSONKit library as an alternative to NSJSON
- Swizzle iOS 5 method for NSURLConnection
  • Loading branch information
megastep committed Jul 20, 2012
1 parent 3247f3a commit f2c96ce
Show file tree
Hide file tree
Showing 7 changed files with 3,457 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -12,4 +12,5 @@ build/*
!default.xcworkspace
xcuserdata
profile
.DS_Store
*.moved-aside
34 changes: 34 additions & 0 deletions Beeblex-SDK.xcodeproj/project.pbxproj
Expand Up @@ -33,6 +33,10 @@
8F7A57D115B5F9A4008C48A1 /* libssl.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F7A57CD15B5F9A4008C48A1 /* libssl.a */; };
8F7A581615B5FAFC008C48A1 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F7A581515B5FAFC008C48A1 /* SystemConfiguration.framework */; };
8F7A582615B5FC6C008C48A1 /* SKPaymentTransaction.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F7A582515B5FC6C008C48A1 /* SKPaymentTransaction.m */; };
EAA4D35E15B95FE200DA0298 /* NSURLConnection+SendAsync.h in Headers */ = {isa = PBXBuildFile; fileRef = EAA4D35C15B95FE200DA0298 /* NSURLConnection+SendAsync.h */; };
EAA4D35F15B95FE200DA0298 /* NSURLConnection+SendAsync.m in Sources */ = {isa = PBXBuildFile; fileRef = EAA4D35D15B95FE200DA0298 /* NSURLConnection+SendAsync.m */; };
EADE7A5815B9457900A1306A /* _BBXJSONKit.h in Headers */ = {isa = PBXBuildFile; fileRef = EADE7A5615B9457900A1306A /* _BBXJSONKit.h */; };
EADE7A5915B9457900A1306A /* _BBXJSONKit.m in Sources */ = {isa = PBXBuildFile; fileRef = EADE7A5715B9457900A1306A /* _BBXJSONKit.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -70,6 +74,10 @@
8F7A581515B5FAFC008C48A1 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
8F7A582415B5FC6C008C48A1 /* SKPaymentTransaction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SKPaymentTransaction.h; sourceTree = "<group>"; };
8F7A582515B5FC6C008C48A1 /* SKPaymentTransaction.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SKPaymentTransaction.m; sourceTree = "<group>"; };
EAA4D35C15B95FE200DA0298 /* NSURLConnection+SendAsync.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSURLConnection+SendAsync.h"; sourceTree = "<group>"; };
EAA4D35D15B95FE200DA0298 /* NSURLConnection+SendAsync.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURLConnection+SendAsync.m"; sourceTree = "<group>"; };
EADE7A5615B9457900A1306A /* _BBXJSONKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _BBXJSONKit.h; sourceTree = "<group>"; };
EADE7A5715B9457900A1306A /* _BBXJSONKit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _BBXJSONKit.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -170,6 +178,8 @@
8F7A57AE15B5F899008C48A1 /* Private Classes */ = {
isa = PBXGroup;
children = (
EAA4D35615B95F4400DA0298 /* NSURLConnection+Async */,
EADE7A5515B9457900A1306A /* JSONKit */,
8F7A57B515B5F8AE008C48A1 /* Crypto */,
8F7A57BD15B5F8C5008C48A1 /* Reachability */,
);
Expand Down Expand Up @@ -241,6 +251,24 @@
name = Mocks;
sourceTree = "<group>";
};
EAA4D35615B95F4400DA0298 /* NSURLConnection+Async */ = {
isa = PBXGroup;
children = (
EAA4D35C15B95FE200DA0298 /* NSURLConnection+SendAsync.h */,
EAA4D35D15B95FE200DA0298 /* NSURLConnection+SendAsync.m */,
);
name = "NSURLConnection+Async";
sourceTree = "<group>";
};
EADE7A5515B9457900A1306A /* JSONKit */ = {
isa = PBXGroup;
children = (
EADE7A5615B9457900A1306A /* _BBXJSONKit.h */,
EADE7A5715B9457900A1306A /* _BBXJSONKit.m */,
);
path = JSONKit;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand All @@ -253,6 +281,8 @@
8F7A57C115B5F8EC008C48A1 /* BBXBeeblex.h in Headers */,
8F7A57C415B5F8EC008C48A1 /* BBXBeeblex_Private.h in Headers */,
8F7A57C815B5F90A008C48A1 /* BBXIAPTransaction.h in Headers */,
EADE7A5815B9457900A1306A /* _BBXJSONKit.h in Headers */,
EAA4D35E15B95FE200DA0298 /* NSURLConnection+SendAsync.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -387,6 +417,8 @@
8F7A57BB15B5F8C2008C48A1 /* _BBXReachability.m in Sources */,
8F7A57C215B5F8EC008C48A1 /* BBXBeeblex.m in Sources */,
8F7A57C915B5F90A008C48A1 /* BBXIAPTransaction.m in Sources */,
EADE7A5915B9457900A1306A /* _BBXJSONKit.m in Sources */,
EAA4D35F15B95FE200DA0298 /* NSURLConnection+SendAsync.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -482,6 +514,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Beeblex-SDK/Beeblex-SDK-Prefix.pch";
GCC_THUMB_SUPPORT = NO;
IPHONEOS_DEPLOYMENT_TARGET = 4.0;
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"\"$(SRCROOT)\"",
Expand All @@ -503,6 +536,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Beeblex-SDK/Beeblex-SDK-Prefix.pch";
GCC_THUMB_SUPPORT = NO;
IPHONEOS_DEPLOYMENT_TARGET = 4.0;
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"\"$(SRCROOT)\"",
Expand Down
31 changes: 16 additions & 15 deletions Beeblex-SDK/BBXIAPTransaction.m
Expand Up @@ -13,7 +13,9 @@

#import "_BBXCrypto.h"
#import "_BBXReachability.h"
#import "_BBXJSONKit.h"

#import "NSURLConnection+SendAsync.h"

const struct BBXIAPTransactionErrorCodes BBXIAPTransactionErrorCodes = {
.domain = @"BBXIAPTransactionDomain",
Expand Down Expand Up @@ -117,10 +119,9 @@ - (void) validateWithCompletionBlock:(BBXAPITransactionCompletionBlock)completio
completionBlock(error);
return;
}

[crypto setClearTextWithData:[payload JSONDataWithOptions:JKSerializeOptionNone error:nil]];

[crypto setClearTextWithData:[NSJSONSerialization dataWithJSONObject:payload
options:0
error:Nil]];
NSData *cypherText = [crypto encrypt:@"blowfish"];

if (!cypherText) {
Expand Down Expand Up @@ -168,15 +169,15 @@ - (void) validateWithCompletionBlock:(BBXAPITransactionCompletionBlock)completio
@"payload" : [_BBXCrypto encodeBase64:cypherText WithNewlines:NO]
};

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:finalPayload
options:0
error:Nil];

NSData *jsonData = [finalPayload JSONDataWithOptions:JKSerializeOptionNone error:nil];

NSAssert(jsonData, @"Unable to create JSON payload");

#ifdef DEBUG
if (!self.useSecureConnection) {
NSLog(@"Warning: Beeblex is not using HTTPS to connect to the server, most likely due to export compliance reasons. This is not necessarily a problem, but we thought you should know.");
}
#endif

NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/app/%@/verify",
self.useSecureConnection ? [BBXBeeblex _baseSecureURL] : [BBXBeeblex _baseURL],
Expand All @@ -186,6 +187,7 @@ - (void) validateWithCompletionBlock:(BBXAPITransactionCompletionBlock)completio
request.HTTPMethod = @"POST";
request.HTTPBody = jsonData;


[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
Expand All @@ -194,15 +196,15 @@ - (void) validateWithCompletionBlock:(BBXAPITransactionCompletionBlock)completio
code:BBXIAPTransactionErrorCodes.cannotContactBBXValidationServer
userInfo:@{
NSLocalizedDescriptionKey : NSLocalizedString(@"Unable to contact the Beeblex validation server.", Nil)}];

self.hasServerError = YES;
self.running = NO;
completionBlock(error);
return;
}

NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;

if (httpResponse.statusCode > 399 && httpResponse.statusCode < 500) {
error = [NSError errorWithDomain:BBXIAPTransactionErrorCodes.domain
code:BBXIAPTransactionErrorCodes.clientError
Expand Down Expand Up @@ -253,7 +255,7 @@ - (void) validateWithCompletionBlock:(BBXAPITransactionCompletionBlock)completio
completionBlock(error);
return;
}

[crypto setCipherText:data];

NSData *result = [crypto decrypt:@"blowfish"];
Expand All @@ -269,10 +271,9 @@ - (void) validateWithCompletionBlock:(BBXAPITransactionCompletionBlock)completio
completionBlock(error);
return;
}

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:result
options:0
error:&error];

NSDictionary *dictionary = [[BBXJSONDecoder decoder] objectWithData:result
error:&error];

if (!dictionary || ![dictionary isKindOfClass:[NSDictionary class]]) {
error = [NSError errorWithDomain:BBXIAPTransactionErrorCodes.domain
Expand Down Expand Up @@ -301,7 +302,7 @@ - (void) validateWithCompletionBlock:(BBXAPITransactionCompletionBlock)completio
self.validatedTransactionData = iapData;
self.transactionIsDuplicate = [[dictionary objectForKey:@"duplicate"] boolValue];
}

completionBlock(Nil);
}];
}
Expand Down

0 comments on commit f2c96ce

Please sign in to comment.