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.xcodeproj/project.pbxproj | 17 +++++++++++------ 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 +++++ 10 files changed, 52 insertions(+), 46 deletions(-) create mode 100644 Juick/Model/DeviceRegistration.h delete mode 100644 Juick/Model/TokensList.h delete mode 100644 Juick/Model/TokensList.m diff --git a/Juick.xcodeproj/project.pbxproj b/Juick.xcodeproj/project.pbxproj index d2961f0..c6fa084 100644 --- a/Juick.xcodeproj/project.pbxproj +++ b/Juick.xcodeproj/project.pbxproj @@ -8,7 +8,6 @@ /* Begin PBXBuildFile section */ 274CA3F5A1521329BEA647FB /* Pods_Juick.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EE63D145DADD14AE17A58456 /* Pods_Juick.framework */; }; - 770A42911E8881CB000B206A /* TokensList.m in Sources */ = {isa = PBXBuildFile; fileRef = 770A42901E8881CB000B206A /* TokensList.m */; }; 77317BAC181BBE8500D60005 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 77317BAB181BBE8500D60005 /* Foundation.framework */; }; 77317BAE181BBE8500D60005 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 77317BAD181BBE8500D60005 /* CoreGraphics.framework */; }; 77317BB0181BBE8500D60005 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 77317BAF181BBE8500D60005 /* UIKit.framework */; }; @@ -61,8 +60,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ - 770A428F1E8881CB000B206A /* TokensList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TokensList.h; sourceTree = ""; }; - 770A42901E8881CB000B206A /* TokensList.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TokensList.m; sourceTree = ""; }; + 770A428F1E8881CB000B206A /* DeviceRegistration.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DeviceRegistration.h; sourceTree = ""; }; 77317BA8181BBE8500D60005 /* Juick.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Juick.app; sourceTree = BUILT_PRODUCTS_DIR; }; 77317BAB181BBE8500D60005 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 77317BAD181BBE8500D60005 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; }; @@ -96,6 +94,7 @@ 774528C41F93AC0F004D110B /* CoreDataStack.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CoreDataStack.m; sourceTree = ""; }; 774528C91F93EE83004D110B /* AssetsLibrary.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AssetsLibrary.framework; path = System/Library/Frameworks/AssetsLibrary.framework; sourceTree = SDKROOT; }; 774528CB1F93EE9F004D110B /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; }; + 774528CD1F96B39C004D110B /* Juick.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Juick.entitlements; sourceTree = ""; }; 77975A1B182B6E9A00410C2B /* NewPostViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NewPostViewController.h; sourceTree = ""; }; 77975A1C182B6E9A00410C2B /* NewPostViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NewPostViewController.m; sourceTree = ""; }; 77975A1E182BDCE900410C2B /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; }; @@ -200,6 +199,7 @@ 77317BB1181BBE8500D60005 /* Juick */ = { isa = PBXGroup; children = ( + 774528CD1F96B39C004D110B /* Juick.entitlements */, 773F23341F76ED5D00B5B0DF /* Splash.png */, 773F23351F76ED5D00B5B0DF /* Splash@2x.png */, 773F23361F76ED5E00B5B0DF /* Splash@3x.png */, @@ -261,8 +261,7 @@ 77A09549181F1F25002BDECD /* Message.m */, 77B099A4189D57F000A84F59 /* User.h */, 77B099A5189D57F000A84F59 /* User.m */, - 770A428F1E8881CB000B206A /* TokensList.h */, - 770A42901E8881CB000B206A /* TokensList.m */, + 770A428F1E8881CB000B206A /* DeviceRegistration.h */, 774528C01F930C06004D110B /* Attachment.h */, 774528C11F930C06004D110B /* Attachment.m */, ); @@ -367,6 +366,11 @@ TargetAttributes = { 77317BA7181BBE8500D60005 = { DevelopmentTeam = KH4MX79ZK7; + SystemCapabilities = { + com.apple.Push = { + enabled = 1; + }; + }; }; 77317BCB181BBE8500D60005 = { DevelopmentTeam = KH4MX79ZK7; @@ -521,7 +525,6 @@ 774528C51F93AC0F004D110B /* CoreDataStack.m in Sources */, 77B09994189D0B9900A84F59 /* UIImage+Utils.m in Sources */, 77317BC2181BBE8500D60005 /* MessagesViewController.m in Sources */, - 770A42911E8881CB000B206A /* TokensList.m in Sources */, 77B099A6189D57F000A84F59 /* User.m in Sources */, 77C6ADE41F7717BC000AEA8C /* ThreadViewController.m in Sources */, 77975A1D182B6E9A00410C2B /* NewPostViewController.m in Sources */, @@ -677,6 +680,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; + CODE_SIGN_ENTITLEMENTS = Juick/Juick.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; DEVELOPMENT_TEAM = KH4MX79ZK7; @@ -698,6 +702,7 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; + CODE_SIGN_ENTITLEMENTS = Juick/Juick.entitlements; CODE_SIGN_IDENTITY = "iPhone Developer"; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "Juick/Supporting Files/Juick-Prefix.pch"; 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