Fixes issues with registration in iOS simulator.

This commit is contained in:
Frederic Jacobs 2015-04-10 22:35:26 +02:00
parent dceb1c9976
commit abc63eca27
2 changed files with 232 additions and 177 deletions

View file

@ -22,7 +22,8 @@
@implementation PushManager
+ (instancetype)sharedManager {
+ (instancetype)sharedManager
{
static PushManager *sharedManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
@ -31,10 +32,12 @@
return sharedManager;
}
- (instancetype)init{
- (instancetype)init
{
self = [super init];
if (self) {
self.missingPermissionsAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ACTION_REQUIRED_TITLE", @"")
self.missingPermissionsAlertView =
[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ACTION_REQUIRED_TITLE", @"")
message:NSLocalizedString(@"PUSH_SETTINGS_MESSAGE", @"")
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"")
@ -45,14 +48,16 @@
#pragma mark Register device for Push Notification locally
-(TOCFuture*)registerPushNotificationFuture{
- (TOCFuture *)registerPushNotificationFuture
{
self.pushNotificationFutureSource = [TOCFutureSource new];
[UIApplication.sharedApplication registerForRemoteNotifications];
return self.pushNotificationFutureSource.future;
}
- (void)requestPushTokenWithSuccess:(void (^)(NSData* pushToken))success failure:(failedPushRegistrationBlock)failure{
- (void)requestPushTokenWithSuccess:(void (^)(NSData *pushToken))success failure:(failedPushRegistrationBlock)failure
{
TOCFuture *requestPushTokenFuture = [self registerPushNotificationFuture];
[requestPushTokenFuture catchDo:^(id failureObj) {
@ -78,14 +83,30 @@
}];
}
- (void)registrationAndRedPhoneTokenRequestWithSuccess:(void (^)(NSData* pushToken, NSString* signupToken))success failure:(failedPushRegistrationBlock)failure{
- (void)registrationAndRedPhoneTokenRequestWithSuccess:(void (^)(NSData *pushToken, NSString *signupToken))success
failure:(failedPushRegistrationBlock)failure
{
if (!self.wantRemoteNotifications) {
success([@"Fake PushToken" dataUsingEncoding:NSUTF8StringEncoding], @"");
[self registerTokenWithRedPhoneServer:[@"Fake PushToken" dataUsingEncoding:NSUTF8StringEncoding]
withSuccess:success
failure:failure];
return;
}
[self requestPushTokenWithSuccess:^(NSData *pushToken) {
[RPServerRequestsManager.sharedInstance performRequest:[RPAPICall requestTextSecureVerificationCode] success:^(NSURLSessionDataTask *task, id responseObject) {
[self registerTokenWithRedPhoneServer:pushToken withSuccess:success failure:failure];
} failure:^(NSError *error) {
[self.missingPermissionsAlertView show];
failure([NSError errorWithDomain:pushManagerDomain code:400 userInfo:@{}]);
}];
}
- (void)registerTokenWithRedPhoneServer:(NSData *)pushToken
withSuccess:(void (^)(NSData *pushToken, NSString *signupToken))success
failure:(failedPushRegistrationBlock)failure
{
[RPServerRequestsManager.sharedInstance performRequest:[RPAPICall requestTextSecureVerificationCode]
success:^(NSURLSessionDataTask *task, id responseObject) {
NSError *error;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&error];
@ -97,28 +118,29 @@
}
success(pushToken, tsToken);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
failure(error);
}];
} failure:^(NSError *error) {
[self.missingPermissionsAlertView show];
failure([NSError errorWithDomain:pushManagerDomain code:400 userInfo:@{}]);
}];
}
-(TOCFuture*)registerForUserNotificationsFuture{
- (TOCFuture *)registerForUserNotificationsFuture
{
self.userNotificationFutureSource = [TOCFutureSource new];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationType)[self allNotificationTypes]
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:(UIUserNotificationType)[self allNotificationTypes]
categories:nil];
[UIApplication.sharedApplication registerUserNotificationSettings:settings];
return self.userNotificationFutureSource.future;
}
-(BOOL) needToRegisterForRemoteNotifications {
- (BOOL)needToRegisterForRemoteNotifications
{
return self.wantRemoteNotifications && (!UIApplication.sharedApplication.isRegisteredForRemoteNotifications);
}
-(BOOL) wantRemoteNotifications {
- (BOOL)wantRemoteNotifications
{
BOOL isSimulator = [UIDevice.currentDevice.model.lowercaseString rangeOfString:@"simulator"].location != NSNotFound;
if (isSimulator) {
@ -129,11 +151,13 @@
return YES;
}
-(int)allNotificationTypes{
- (int)allNotificationTypes
{
return UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge;
}
- (void)validateUserNotificationSettings{
- (void)validateUserNotificationSettings
{
[[self registerForUserNotificationsFuture] thenDo:^(id value){
// Nothing to do, just making sure we are registered for User Notifications.
}];
@ -141,23 +165,27 @@
#pragma mark Register Push Notification Token with RedPhone server
-(TOCFuture*)registerForPushFutureWithToken:(NSData*)token{
- (TOCFuture *)registerForPushFutureWithToken:(NSData *)token
{
self.registerWithServerFutureSource = [TOCFutureSource new];
[RPServerRequestsManager.sharedInstance performRequest:[RPAPICall registerPushNotificationWithPushToken:token] success:^(NSURLSessionDataTask *task, id responseObject) {
[RPServerRequestsManager.sharedInstance performRequest:[RPAPICall registerPushNotificationWithPushToken:token]
success:^(NSURLSessionDataTask *task, id responseObject) {
if ([task.response isKindOfClass:NSHTTPURLResponse.class]) {
NSInteger statusCode = [(NSHTTPURLResponse *)task.response statusCode];
if (statusCode == 200) {
[self.registerWithServerFutureSource trySetResult:@YES];
} else {
DDLogError(@"The server returned %@ instead of a 200 status code", task.response);
[self.registerWithServerFutureSource trySetFailure:[NSError errorWithDomain:pushManagerDomain code:500 userInfo:nil]];
[self.registerWithServerFutureSource
trySetFailure:[NSError errorWithDomain:pushManagerDomain code:500 userInfo:nil]];
}
} else {
[self.registerWithServerFutureSource trySetFailure:task.response];
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
[self.registerWithServerFutureSource trySetFailure:error];
}];

View file

@ -22,16 +22,23 @@
@implementation CodeVerificationViewController
- (void)viewDidLoad {
- (void)viewDidLoad
{
[super viewDidLoad];
[self initializeKeyboardHandlers];
_headerLabel.text = NSLocalizedString(@"VERIFICATION_HEADER", @"");
_challengeTextField.placeholder = NSLocalizedString(@"VERIFICATION_CHALLENGE_DEFAULT_TEXT", @"");
[_challengeButton setTitle:NSLocalizedString(@"VERIFICATION_CHALLENGE_SUBMIT_CODE", @"") forState:UIControlStateNormal];
[_challengeButton setTitle:NSLocalizedString(@"VERIFICATION_CHALLENGE_SUBMIT_CODE", @"")
forState:UIControlStateNormal];
[_sendCodeViaSMSAgainButton setTitle:NSLocalizedString(@"VERIFICATION_CHALLENGE_SUBMIT_AGAIN", @"") forState:UIControlStateNormal];
[_sendCodeViaVoiceButton setTitle:[@" " stringByAppendingString:NSLocalizedString(@"VERIFICATION_CHALLENGE_SEND_VIAVOICE", @"")] forState:UIControlStateNormal];
[_changeNumberButton setTitle:[@" " stringByAppendingString:NSLocalizedString(@"VERIFICATION_CHALLENGE_CHANGE_NUMBER", @"")] forState:UIControlStateNormal];
[_sendCodeViaSMSAgainButton setTitle:NSLocalizedString(@"VERIFICATION_CHALLENGE_SUBMIT_AGAIN", @"")
forState:UIControlStateNormal];
[_sendCodeViaVoiceButton
setTitle:[@" " stringByAppendingString:NSLocalizedString(@"VERIFICATION_CHALLENGE_SEND_VIAVOICE", @"")]
forState:UIControlStateNormal];
[_changeNumberButton
setTitle:[@" " stringByAppendingString:NSLocalizedString(@"VERIFICATION_CHALLENGE_CHANGE_NUMBER", @"")]
forState:UIControlStateNormal];
}
- (void)viewWillAppear:(BOOL)animated
@ -42,12 +49,14 @@
[self adjustScreenSizes];
}
- (void)didReceiveMemoryWarning {
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)verifyChallengeAction:(id)sender {
- (IBAction)verifyChallengeAction:(id)sender
{
[self enableServerActions:NO];
[_challengeTextField resignFirstResponder];
@ -63,21 +72,28 @@
}
- (void)registerWithSuccess:(void(^)())success failure:(void(^)(NSError *))failure{
- (void)registerWithSuccess:(void (^)())success failure:(void (^)(NSError *))failure
{
[_submitCodeSpinner startAnimating];
[[RPServerRequestsManager sharedInstance] performRequest:[RPAPICall verifyVerificationCode:_challengeTextField.text] success:^(NSURLSessionDataTask *task, id responseObject) {
[[RPServerRequestsManager sharedInstance] performRequest:[RPAPICall verifyVerificationCode:_challengeTextField.text]
success:^(NSURLSessionDataTask *task, id responseObject) {
[PushManager.sharedManager registrationAndRedPhoneTokenRequestWithSuccess:^(NSData *pushToken, NSString *signupToken) {
[TSAccountManager registerWithRedPhoneToken:signupToken pushToken:pushToken success:^{
[PushManager.sharedManager registrationAndRedPhoneTokenRequestWithSuccess:^(NSData *pushToken,
NSString *signupToken) {
[TSAccountManager registerWithRedPhoneToken:signupToken
pushToken:pushToken
success:^{
success();
} failure:^(NSError *error) {
}
failure:^(NSError *error) {
failure(error);
}];
} failure:^(NSError *error) {
failure(error);
[_submitCodeSpinner stopAnimating];
}];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
NSHTTPURLResponse *badResponse = (NSHTTPURLResponse *)task.response;
NSError *responseError = [self errorForResponse:badResponse];
@ -89,8 +105,8 @@
// TODO: If useful, this could possibly go in a less-specific class
- (void)showAlertForError:(NSError *)error {
- (void)showAlertForError:(NSError *)error
{
if (error == nil) {
DDLogCError(@"%@: Error condition, but no NSError to display", self.class);
return;
@ -110,8 +126,8 @@
}
- (NSError *)errorForResponse:(NSHTTPURLResponse *)badResponse {
- (NSError *)errorForResponse:(NSHTTPURLResponse *)badResponse
{
NSString *description = NSLocalizedString(@"REGISTRATION_ERROR", @"");
NSString *failureReason = nil;
TSRegistrationFailure failureType;
@ -123,27 +139,31 @@
failureReason = NSLocalizedString(@"REGISTER_RATE_LIMITING_BODY", @"");
failureType = kTSRegistrationFailureRateLimit;
} else {
failureReason = [NSString stringWithFormat:@"%@ %lu", NSLocalizedString(@"SERVER_CODE", @""),(unsigned long)badResponse.statusCode];
failureReason = [NSString
stringWithFormat:@"%@ %lu", NSLocalizedString(@"SERVER_CODE", @""), (unsigned long)badResponse.statusCode];
failureType = kTSRegistrationFailureNetwork;
}
NSDictionary *userInfo = @{NSLocalizedDescriptionKey: description, NSLocalizedFailureReasonErrorKey: failureReason};
NSDictionary *userInfo =
@{NSLocalizedDescriptionKey : description, NSLocalizedFailureReasonErrorKey : failureReason};
NSError *error = [NSError errorWithDomain:TSRegistrationErrorDomain code:failureType userInfo:userInfo];
return error;
}
#pragma mark - Send codes again
- (IBAction)sendCodeSMSAction:(id)sender {
- (IBAction)sendCodeSMSAction:(id)sender
{
[self enableServerActions:NO];
[_requestCodeAgainSpinner startAnimating];
[[RPServerRequestsManager sharedInstance]performRequest:[RPAPICall requestVerificationCode] success:^(NSURLSessionDataTask *task, id responseObject) {
[[RPServerRequestsManager sharedInstance] performRequest:[RPAPICall requestVerificationCode]
success:^(NSURLSessionDataTask *task, id responseObject) {
[self enableServerActions:YES];
[_requestCodeAgainSpinner stopAnimating];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
DDLogError(@"Registration failed with information %@", error.description);
@ -160,17 +180,19 @@
}];
}
- (IBAction)sendCodeVoiceAction:(id)sender {
- (IBAction)sendCodeVoiceAction:(id)sender
{
[self enableServerActions:NO];
[_requestCallSpinner startAnimating];
[[RPServerRequestsManager sharedInstance]performRequest:[RPAPICall requestVerificationCodeWithVoice] success:^(NSURLSessionDataTask *task, id responseObject) {
[[RPServerRequestsManager sharedInstance] performRequest:[RPAPICall requestVerificationCodeWithVoice]
success:^(NSURLSessionDataTask *task, id responseObject) {
[self enableServerActions:YES];
[_requestCallSpinner stopAnimating];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}
failure:^(NSURLSessionDataTask *task, NSError *error) {
DDLogError(@"Registration failed with information %@", error.description);
@ -186,7 +208,8 @@
}];
}
-(void)enableServerActions:(BOOL)enabled {
- (void)enableServerActions:(BOOL)enabled
{
[_challengeButton setEnabled:enabled];
[_sendCodeViaSMSAgainButton setEnabled:enabled];
[_sendCodeViaVoiceButton setEnabled:enabled];
@ -195,16 +218,20 @@
#pragma mark - Keyboard notifications
- (void)initializeKeyboardHandlers{
UITapGestureRecognizer *outsideTabRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboardFromAppropriateSubView)];
- (void)initializeKeyboardHandlers
{
UITapGestureRecognizer *outsideTabRecognizer =
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboardFromAppropriateSubView)];
[self.view addGestureRecognizer:outsideTabRecognizer];
}
- (void)dismissKeyboardFromAppropriateSubView {
- (void)dismissKeyboardFromAppropriateSubView
{
[self.view endEditing:NO];
}
- (void)adjustScreenSizes {
- (void)adjustScreenSizes
{
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat blueHeaderHeight;