Skip to content

Commit

Permalink
Moved postAPNSToken function to BakerAPI singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Simbul committed Mar 31, 2013
1 parent 9b6ef82 commit 3c0811f
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 24 deletions.
6 changes: 6 additions & 0 deletions Baker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
ABD7D45F1616FC5B008B13B5 /* BakerIssue.m in Sources */ = {isa = PBXBuildFile; fileRef = ABD7D45E1616FC5B008B13B5 /* BakerIssue.m */; };
ABDC05AF16A4C42A00F6B63D /* JSONStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = ABDC05AE16A4C42A00F6B63D /* JSONStatus.m */; };
ABDC05B216A4CB3900F6B63D /* ShelfStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = ABDC05B116A4CB3900F6B63D /* ShelfStatus.m */; };
ABF23B1D1708418E003AA3B0 /* BakerAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = ABF23B1C1708418E003AA3B0 /* BakerAPI.m */; };
BF3CCCA215D080560087B9CA /* ios-icon-ipad.png in Resources */ = {isa = PBXBuildFile; fileRef = BF3CCC9E15D080560087B9CA /* ios-icon-ipad.png */; };
BF3CCCA315D080560087B9CA /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = BF3CCC9F15D080560087B9CA /* [email protected] */; };
BF3CCCA415D080560087B9CA /* ios-icon-iphone.png in Resources */ = {isa = PBXBuildFile; fileRef = BF3CCCA015D080560087B9CA /* ios-icon-iphone.png */; };
Expand Down Expand Up @@ -210,6 +211,8 @@
ABDC05AE16A4C42A00F6B63D /* JSONStatus.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JSONStatus.m; sourceTree = "<group>"; };
ABDC05B016A4CB3900F6B63D /* ShelfStatus.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ShelfStatus.h; sourceTree = "<group>"; };
ABDC05B116A4CB3900F6B63D /* ShelfStatus.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ShelfStatus.m; sourceTree = "<group>"; };
ABF23B1B1708418E003AA3B0 /* BakerAPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BakerAPI.h; sourceTree = "<group>"; };
ABF23B1C1708418E003AA3B0 /* BakerAPI.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BakerAPI.m; sourceTree = "<group>"; };
BF3CCC9E15D080560087B9CA /* ios-icon-ipad.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "ios-icon-ipad.png"; sourceTree = "<group>"; };
BF3CCC9F15D080560087B9CA /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
BF3CCCA015D080560087B9CA /* ios-icon-iphone.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "ios-icon-iphone.png"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -416,6 +419,8 @@
9F58D84E15D66E870027902C /* UICustomNavigationBar.m */,
9F58D84F15D66E870027902C /* UICustomNavigationController.h */,
9F58D85015D66E870027902C /* UICustomNavigationController.m */,
ABF23B1B1708418E003AA3B0 /* BakerAPI.h */,
ABF23B1C1708418E003AA3B0 /* BakerAPI.m */,
);
path = BakerShelf;
sourceTree = "<group>";
Expand Down Expand Up @@ -617,6 +622,7 @@
ABBFD8F216C6614300BA64A4 /* NSString+UUID.m in Sources */,
ABADF3F916C7BAD900F52F3C /* NSURL+Extensions.m in Sources */,
AB67F5BA16EE99B2005A04F6 /* Reachability.m in Sources */,
ABF23B1D1708418E003AA3B0 /* BakerAPI.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
6 changes: 3 additions & 3 deletions Baker/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#import "UICustomNavigationController.h"
#import "UICustomNavigationBar.h"
#import "IssuesManager.h"
#import "PurchasesManager.h"
#import "BakerAPI.h"

#import "BakerViewController.h"

Expand Down Expand Up @@ -133,8 +133,8 @@ - (void)application:(UIApplication*)application didRegisterForRemoteNotification
[[NSUserDefaults standardUserDefaults] setObject:apnsToken forKey:@"apns_token"];
[[NSUserDefaults standardUserDefaults] synchronize];

PurchasesManager *purchaseManager = [PurchasesManager sharedInstance];
[purchaseManager postAPNSToken:apnsToken];
BakerAPI *api = [BakerAPI sharedInstance];
[api postAPNSToken:apnsToken];
}
#endif

Expand Down
44 changes: 44 additions & 0 deletions BakerShelf/BakerAPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// BakerAPI.h
// Baker
//
// ==========================================================================================
//
// Copyright (c) 2010-2012, Davide Casali, Marco Colombo, Alessandro Morandi
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// Neither the name of the Baker Framework nor the names of its contributors may be used to
// endorse or promote products derived from this software without specific prior written
// permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#import <Foundation/Foundation.h>

@interface BakerAPI : NSObject

#pragma mark - Singleton

+ (BakerAPI *)sharedInstance;

#pragma mark - APNS

- (BOOL)postAPNSToken:(NSString *)apnsToken;

@end
89 changes: 89 additions & 0 deletions BakerShelf/BakerAPI.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// BakerAPI.m
// Baker
//
// ==========================================================================================
//
// Copyright (c) 2010-2012, Davide Casali, Marco Colombo, Alessandro Morandi
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// Neither the name of the Baker Framework nor the names of its contributors may be used to
// endorse or promote products derived from this software without specific prior written
// permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#import "BakerAPI.h"
#import "Constants.h"
#import "NSMutableURLRequest+WebServiceClient.h"

#import "Utils.h"
#ifdef BAKER_NEWSSTAND
#import "PurchasesManager.h"
#endif

@implementation BakerAPI

#pragma mark - Singleton

+ (BakerAPI *)sharedInstance {
static dispatch_once_t once;
static BakerAPI *sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}

#pragma mark - APNS

- (BOOL)postAPNSToken:(NSString *)apnsToken {
if ([POST_APNS_TOKEN_URL length] > 0) {
NSDictionary *params = [NSDictionary dictionaryWithObject:apnsToken forKey:@"apns_token"];
NSError *error = nil;

[self postParams:params toURL:[NSURL URLWithString:POST_APNS_TOKEN_URL] error:&error];
if (error) {
NSLog(@"Error sending APNS device token %@", error);
return NO;
}
return YES;
}
return NO;
}

#pragma mark - Helpers

- (NSData *)postParams:(NSDictionary *)params toURL:(NSURL *)url error:(NSError **)error {
NSMutableDictionary *postParams = [NSMutableDictionary dictionaryWithDictionary:params];
[postParams setObject:[Utils appID] forKey:@"app_id"];

#ifdef BAKER_NEWSSTAND
[postParams setObject:[PurchasesManager UUID] forKey:@"user_id"];
#endif

NSURLResponse *response = nil;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:REQUEST_TIMEOUT];
[request setHTTPMethod:@"POST"];
[request setFormPostParameters:postParams];

return [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];
}

@end
4 changes: 0 additions & 4 deletions BakerShelf/PurchasesManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@

- (SKProduct *)productFor:(NSString *)productID;

#pragma mark - APNS

- (BOOL)postAPNSToken:(NSString *)apnsToken;

#pragma mark - Subscriptions

- (BOOL)hasSubscriptions;
Expand Down
17 changes: 0 additions & 17 deletions BakerShelf/PurchasesManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -356,23 +356,6 @@ - (SKProduct *)productFor:(NSString *)productID {
return [self.products objectForKey:productID];
}

#pragma mark - APNS

- (BOOL)postAPNSToken:(NSString *)apnsToken {
if ([POST_APNS_TOKEN_URL length] > 0) {
NSDictionary *params = [NSDictionary dictionaryWithObject:apnsToken forKey:@"apns_token"];
NSError *error = nil;

[self postParams:params toURL:[NSURL URLWithString:POST_APNS_TOKEN_URL] error:&error];
if (error) {
NSLog(@"Error sending APNS device token %@", error);
return NO;
}
return YES;
}
return NO;
}

#pragma mark - Subscriptions

- (BOOL)hasSubscriptions {
Expand Down

0 comments on commit 3c0811f

Please sign in to comment.