Skip to content

Instantly share code, notes, and snippets.

@jsooriah
Created February 14, 2014 09:52
Show Gist options
  • Save jsooriah/8998495 to your computer and use it in GitHub Desktop.
Save jsooriah/8998495 to your computer and use it in GitHub Desktop.
- (void)preparePushConfigurationParams {
NSMutableDictionary *finalParamsDict = [NSMutableDictionary dictionary];
NSArray *temp = [self.selectedTeamsForNews arrayByAddingObjectsFromArray:self.selectedTeamsForPush];
NSMutableDictionary *finalDict = [NSMutableDictionary dictionary];
for (FMTeam *aTeam in temp) {
NSMutableDictionary *aDict = [NSMutableDictionary dictionary];
BOOL configure_for_push = [self.selectedTeamsForPush containsObject:aTeam];
BOOL configure_for_news = [self.selectedTeamsForNews containsObject:aTeam];
[aDict setObject:[NSNumber numberWithInt:aTeam.fm_team_id] forKey:@"fm_team_id"];
[aDict setObject:[NSNumber numberWithBool:configure_for_news] forKey:@"receive_push_news"];
[aDict setObject:[NSNumber numberWithBool:configure_for_push] forKey:@"receive_push_live"];
[finalDict setObject:aDict forKey:[NSString stringWithFormat:@"%d", aTeam.fm_team_id]];
}
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *facebookUserId = [prefs stringForKey:@"fbUserId"];
if (!facebookUserId) {
/*UIAlertView *anAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"Veuillez vous connecter à Facebook avant de continuer." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[anAlertView show];
return;*/
} else {
[finalParamsDict setObject:facebookUserId forKey:@"facebook_id"];
}
NSString *device_id = [OpenUDID value];
NSString *push_id = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken_User"];
NSLog(@"push_id : %@", push_id);
if (!device_id || !push_id) {
// Failure
UIAlertView *anAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"Nous n'avons pas pu transmettre vos paramètres pour le moment. Veuillez re essayer ultérieurement." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[anAlertView show];
return;
}
[finalParamsDict setObject:push_id forKey:@"push_id"];
[finalParamsDict setObject:device_id forKey:@"device_id"];
[finalParamsDict setObject:@"ios" forKey:@"device_os"];
[finalParamsDict setObject:@"1" forKey:@"application_id"];
[finalParamsDict setObject:finalDict forKey:@"teams"];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:finalParamsDict options:0 error:&error];
if (!jsonData) {
// Failure
UIAlertView *anAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"Nous n'avons pas pu transmettre vos paramètres pour le moment. Veuillez re essayer ultérieurement." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[anAlertView show];
return;
} else {
NSString *JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
NSLog(@"JSONString : %@", JSONString);
}
NSURL *url = [NSURL URLWithString:@"http://footmercatov2.newdev.bewoopi.net"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/api_connect_user" parameters:nil];
[request setHTTPBody:jsonData];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Success
NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"responseString : %@", responseString);
[self skipModal];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Failure
UIAlertView *anAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"Nous n'avons pas pu transmettre vos paramètres pour le moment. Veuillez re essayer ultérieurement." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[anAlertView show];
[self skipModal];
return;
}];
if ((FBSession.activeSession.state == FBSessionStateClosed)) {/**/
UIAlertView *anAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"Please connect to facebook first" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[anAlertView show];
} else {
[operation start];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment