Skip to content

Instantly share code, notes, and snippets.

@pita5
Created May 24, 2012 10:36
Show Gist options
  • Save pita5/2780766 to your computer and use it in GitHub Desktop.
Save pita5/2780766 to your computer and use it in GitHub Desktop.
- (void)performLogin
{
/* Perform validation on the form input */
if ([self validateLogin])
{
[SVProgressHUD showInView:self.view
status:nil
networkIndicator:YES
posY:-1
maskType:SVProgressHUDMaskTypeBlack];
/* Login the account */
[[EntitlementsManager sharedEntitlementsManager] requestAccountWithEmail:self.emailAddressTextField.text Password:self.passwordTextField.text Completion:^(EntitlementsResponse response, NSDictionary *jsonResult) {
/* Login successful, or login success but not entitled */
if (response == Success ||
response == NotEntitled)
{
/* We need to get the users attributes (User Name, Email etc) */
[[EntitlementsManager sharedEntitlementsManager] getAccountDetailsWithEmail:self.emailAddressTextField.text Password:self.passwordTextField.text Completion:^(EntitlementsResponse attributesResponse, NSDictionary *data) {
/* This is the users details */
if (data)
{
/* Make sense of the crappy response from phoenix */
NSArray *attrs = [data objectForKey:@"Attributes"];
NSDictionary *firstNameDict = [attrs objectAtIndex:0];
NSDictionary *lastNameDict = [attrs objectAtIndex:1];
NSString *firstNameStr = [firstNameDict objectForKey:@"Value"];
NSString *lastNameStr = [lastNameDict objectForKey:@"Value"];
/* Store the users details for later use */
[[HRTZUserDefaults sharedUserDefaults] setUserName:[NSString stringWithFormat:@"%@ %@", firstNameStr, lastNameStr]];
[[HRTZUserDefaults sharedUserDefaults] setGuid:[jsonResult objectForKey:@"Guid"]];
[[HRTZUserDefaults sharedUserDefaults] setEmail:self.emailAddressTextField.text];
[[HRTZUserDefaults sharedUserDefaults] setPassword:self.passwordTextField.text];
}
else
{
/* We can't login without knowing the user name/email */
[AppDelegate dismissProgressIndicator];
[self showNetworkError];
return;
}
/* Retrieve our current receipt */
NSData* receipt = [[HRTZUserDefaults sharedUserDefaults] subscriptionReceipt];
/* If I have a receipt, validate it with Phoenix */
if (receipt)
{
[[EntitlementsManager sharedEntitlementsManager] validateReceipt:receipt Completion:^(EntitlementsResponse entitlementResponse) {
switch (entitlementResponse)
{
case Success:
{
/* If success, then the receipt is valid and we have subscription access */
[[HRTZUserDefaults sharedUserDefaults] setHasSubscriberAccess:YES];
/* We should update the Haaretz account entitlement with the receipt also */
[[EntitlementsManager sharedEntitlementsManager] updateAccountDetailsWithEmail:[[HRTZUserDefaults sharedUserDefaults] email]
Password:[[HRTZUserDefaults sharedUserDefaults] password]
Receipt:receipt
Completion:^(EntitlementsResponse response) {
NSLog(@"Updating Account");
}];
}
break;
case AppStoreNotReachable:
{
/* Fail silently */
/* Account has not been linked */
}
break;
case SubscriptionExpired:
/* Subscription has been deemed expired, no access */
[[HRTZUserDefaults sharedUserDefaults] setHasSubscriberAccess:NO];
break;
default:
break;
}
}];
}
else
{
/* When we don't have a receipt on device, assume phoenix knows more about the subscription status */
[[HRTZUserDefaults sharedUserDefaults] setHasSubscriberAccess:(response == Success)];
}
/* Dismiss and update analytics */
[[HRTZAnalytics sharedInstance] paywallFormWasCompleted:@"login"];
/* Show the welcome screen */
[self dismissViewControllerAnimated:YES completion:^{
[AppDelegate dismissProgressIndicator];
[[NSNotificationCenter defaultCenter] postNotificationName:LoginSuccessfulNotification object:nil];
WelcomeController* viewController = [[WelcomeController alloc] initWithNibName:nil bundle:nil];
[viewController setReturning:YES];
[viewController setSubscriber:!(response == NotEntitled)];
CGRect viewBounds = viewController.view.bounds;
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:viewController animated:YES completion:nil];
viewController.view.superview.bounds = viewBounds;
}];
}];
}
else
{
/* Validation on form has failed */
[AppDelegate dismissProgressIndicator];
[[HRTZUserDefaults sharedUserDefaults] setGuid:nil];
[[HRTZUserDefaults sharedUserDefaults] setEmail:nil];
[[HRTZUserDefaults sharedUserDefaults] setHasSubscriberAccess:NO];
[[NSNotificationCenter defaultCenter] postNotificationName:LoginFailedNotification object:nil];
switch (response)
{
case Success:
break;
case NetworkError:
self.errorLabel1.text = NSLocalizedString(@"Error: The system is currently not responding. Please try again later", nil);
break;
case LoginFailed:
self.errorLabel1.text = NSLocalizedString(@"Error: Incorrect password, please try again", nil);
break;
case NotEntitled:
break;
default:
break;
}
}
}];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment