From 2345989ce454342d8ac45077e2cd650d18744066 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 18 Oct 2017 01:48:34 +0300 Subject: device registration --- Juick/APIClient.m | 5 +---- Juick/AppDelegate.h | 2 ++ Juick/AppDelegate.m | 21 ++++++++++++++------- Juick/Juick.entitlements | 2 +- Juick/Model/DeviceRegistration.h | 17 +++++++++++++++++ Juick/Model/TokensList.h | 14 -------------- Juick/Model/TokensList.m | 13 ------------- Juick/Supporting Files/Juick-Info.plist | 2 +- Juick/ViewControllers/LoginViewController.m | 5 +++++ 9 files changed, 41 insertions(+), 40 deletions(-) create mode 100644 Juick/Model/DeviceRegistration.h delete mode 100644 Juick/Model/TokensList.h delete mode 100644 Juick/Model/TokensList.m (limited to 'Juick') diff --git a/Juick/APIClient.m b/Juick/APIClient.m index d2356b5..beace7c 100644 --- a/Juick/APIClient.m +++ b/Juick/APIClient.m @@ -23,16 +23,13 @@ -(id)init { if (self = [super init]) { self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.juick.com"]]; + self.manager.requestSerializer = [AFJSONRequestSerializer new]; } return self; } -(void) pullNextFromPath:(NSString *)path params:(NSDictionary *) params callback:(void(^)(NSArray *, NSError *))callback { AFHTTPSessionManager *manager = [APIClient sharedClient].manager; - if ([[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] != nil) { - manager.requestSerializer = [AFJSONRequestSerializer serializer]; - [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]]; - } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [manager GET:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSMutableArray *messages = [NSMutableArray new]; diff --git a/Juick/AppDelegate.h b/Juick/AppDelegate.h index b5954e1..64cc01a 100644 --- a/Juick/AppDelegate.h +++ b/Juick/AppDelegate.h @@ -13,4 +13,6 @@ @property (strong, nonatomic) UIWindow *window; +-(void) registerForRemoteNotifications; + @end diff --git a/Juick/AppDelegate.m b/Juick/AppDelegate.m index 7be4424..d4f0b8c 100644 --- a/Juick/AppDelegate.m +++ b/Juick/AppDelegate.m @@ -12,7 +12,7 @@ #import "ColorScheme.h" #import "Message.h" -#import "TokensList.h" +#import "DeviceRegistration.h" #import "NewPostViewController.h" @implementation AppDelegate @@ -25,7 +25,8 @@ [[UIToolbar appearance] setTintColor:[ColorScheme linkColor]]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; - + + [self registerForRemoteNotifications]; return YES; } @@ -34,7 +35,9 @@ center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){ if(!error){ - [[UIApplication sharedApplication] registerForRemoteNotifications]; + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + [[UIApplication sharedApplication] registerForRemoteNotifications]; + }]; } }]; } @@ -42,11 +45,15 @@ -(void) application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]]; token = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; - TokensList *tokensList = [TokensList new]; - tokensList.type = @"apns"; - tokensList.tokens = [NSArray arrayWithObjects:token, nil]; + DeviceRegistration *registration = [DeviceRegistration new]; + registration.type = @"apns"; + registration.token = token; AFHTTPSessionManager *manager = [APIClient sharedClient].manager; - [manager PUT:@"/notifications" parameters:[tokensList yy_modelToJSONObject] success:nil failure:nil]; + [manager PUT:@"/notifications" parameters:[@[registration] yy_modelToJSONObject] success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + NSLog(@"success"); + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + NSLog(@"fail %@", [error localizedDescription]); + } ]; } -(void) application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { diff --git a/Juick/Juick.entitlements b/Juick/Juick.entitlements index 28c29bf..903def2 100644 --- a/Juick/Juick.entitlements +++ b/Juick/Juick.entitlements @@ -3,6 +3,6 @@ aps-environment - production + development diff --git a/Juick/Model/DeviceRegistration.h b/Juick/Model/DeviceRegistration.h new file mode 100644 index 0000000..e629637 --- /dev/null +++ b/Juick/Model/DeviceRegistration.h @@ -0,0 +1,17 @@ +// +// TokensList.h +// Juick +// +// Created by Vitaly Takmazov on 27/03/2017. +// Copyright © 2017 com.juick. All rights reserved. +// + +#import + +@interface DeviceRegistration : NSObject +@property NSString *type; +@property NSString *token; +@end + +@implementation DeviceRegistration +@end diff --git a/Juick/Model/TokensList.h b/Juick/Model/TokensList.h deleted file mode 100644 index 9ed5ab0..0000000 --- a/Juick/Model/TokensList.h +++ /dev/null @@ -1,14 +0,0 @@ -// -// TokensList.h -// Juick -// -// Created by Vitaly Takmazov on 27/03/2017. -// Copyright © 2017 com.juick. All rights reserved. -// - -#import - -@interface TokensList : NSObject -@property NSString *type; -@property NSArray *tokens; -@end diff --git a/Juick/Model/TokensList.m b/Juick/Model/TokensList.m deleted file mode 100644 index 5cfe6a6..0000000 --- a/Juick/Model/TokensList.m +++ /dev/null @@ -1,13 +0,0 @@ -// -// TokensList.m -// Juick -// -// Created by Vitaly Takmazov on 27/03/2017. -// Copyright © 2017 com.juick. All rights reserved. -// - -#import "TokensList.h" - -@implementation TokensList - -@end diff --git a/Juick/Supporting Files/Juick-Info.plist b/Juick/Supporting Files/Juick-Info.plist index 74fbe61..e3ddfc5 100644 --- a/Juick/Supporting Files/Juick-Info.plist +++ b/Juick/Supporting Files/Juick-Info.plist @@ -21,7 +21,7 @@ CFBundleSignature ???? CFBundleVersion - 1.0.19 + 1.0.20 ITSAppUsesNonExemptEncryption LSApplicationCategoryType diff --git a/Juick/ViewControllers/LoginViewController.m b/Juick/ViewControllers/LoginViewController.m index 1718366..d3d1edc 100644 --- a/Juick/ViewControllers/LoginViewController.m +++ b/Juick/ViewControllers/LoginViewController.m @@ -9,6 +9,7 @@ #import "LoginViewController.h" #import "User.h" #import "ColorScheme.h" +#import "AppDelegate.h" @implementation LoginViewController @@ -57,6 +58,10 @@ - (void) doneSignIn { [User checkIsValid:^(BOOL success) { if (success) { + [[APIClient sharedClient].manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]]; + + AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; + [app registerForRemoteNotifications]; CATransition* transition = [CATransition animation]; transition.duration = 0.3; transition.type = kCATransitionFade; -- cgit v1.2.3