Permalink
Browse files

iOS 6.0 login support w/samples

Summary:
Adds login support for iOS 6.0, including but not limited to --
 * renaming of permissions to denote read requirement where applicable
 * addition of arguments, overloads and enums to support audience selection
 * update of samples that post, to defer publish permissions until action is taken
 * core iOS 6.0 authorization support in FBSession

Test Plan:
Mainly manual testing of impacted samples, including full matrix of login and
permission upgrade, with and without iOS integrated auth, as well as adding
and removing iOS integrated auth part-way through the test process.

Reviewers: mmarucheck, clang, caabernathy

Reviewed By: mmarucheck

CC: msdkexp@, ekoneil, leil

Differential Revision: https://phabricator.fb.com/D577229
  • Loading branch information...
Jason Clark authored and onebit committed Aug 21, 2012
1 parent 0f3ac30 commit 4dfb45bd39850f21ac4c7c99e15f13134c997e6a
@@ -62,7 +62,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
viewControllerSettings = [[FBUserSettingsViewController alloc] init];
viewControllerSettings.title = @"Facebook Settings";
viewControllerSettings.tabBarItem.image = [UIImage imageNamed:@"second"];
- viewControllerSettings.permissions = [NSArray arrayWithObjects:@"publish_actions", nil];
+ viewControllerSettings.permissions = nil;
self.tabBarController = [[UITabBarController alloc] init];
@@ -135,7 +135,7 @@ - (void)pickerView:(UIPickerView *)pickerView
// FBSample logic
// This is the workhorse method of this view. It sets up the content for a custom OG action, and then posts it
-// using FBRequest/FBRequestConnection. This method also uses the custom protocols defined in
+// using FBRequest/FBRequestConnection. This method also uses the custom protocols defined in
// OGProtocols.h (defined in this sample application), in order to create and consume actions in a typed fashion
- (void)postAction:(NSString *)actionPath
leftOperand:(BOOL)left
@@ -145,50 +145,68 @@ - (void)postAction:(NSString *)actionPath
// if we have a valid session, then we post the action to the users wall, else noop
if (FBSession.activeSession.isOpen) {
- // create an object to hold our action information, the FBGraphObject class has a lightweight
- // static method API that supports creating and comparing of objects that implement the
- // FBGraphObject protocol
- id<BOGGraphBooleanAction> action = (id<BOGGraphBooleanAction>)[FBGraphObject graphObject];
-
- // set the action's results and two truth-value objects, using inferred accessor methods
- // in our custom open graph action protocol
- action.result = result ? @"1" : @"0";
- action.truthvalue = [BOGFirstViewController ogObjectForTruthValue:left];
- action.anothertruthvalue = [BOGFirstViewController ogObjectForTruthValue:right];
-
-
- // post the action using one of the lightweight static start* methods on FBRequest
- [FBRequestConnection startForPostWithGraphPath:actionPath
- graphObject:action
- completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
- if (!error) {
- // successful post, in the sample we do nothing with the id, however
- // a more complex applicaiton may want to store or perform addtional actions
- // with the id that represents the just-posted action
- } else {
- // get the basic error message
- NSString *message = error.localizedDescription;
-
- // see if we can improve on it with an error message from the server
- id json = [error.userInfo objectForKey:FBErrorParsedJSONResponseKey];
- if ([json isKindOfClass:[NSDictionary class]] &&
- (json = [json objectForKey:@"body"]) &&
- [json isKindOfClass:[NSDictionary class]] &&
- (json = [json objectForKey:@"error"]) &&
- [json isKindOfClass:[NSDictionary class]] &&
- (json = [json objectForKey:@"message"])) {
- message = [json description];
+ // if we don't have permission to post, let's first address that
+ if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
+
+ [FBSession.activeSession reauthorizeWithPermissions:[NSArray arrayWithObject:@"publish_actions"]
+ behavior:FBSessionLoginBehaviorWithFallbackToWebView
+ defaultAudience:FBSessionDefaultAudienceFriends
+ completionHandler:^(FBSession *session, NSError *error) {
+ if (!error) {
+ // re-call assuming we now have the permission
+ [self postAction:actionPath
+ leftOperand:left
+ rightOperand:right
+ result:result];
+ }
+ }];
+ } else {
+
+ // create an object to hold our action information, the FBGraphObject class has a lightweight
+ // static method API that supports creating and comparing of objects that implement the
+ // FBGraphObject protocol
+ id<BOGGraphBooleanAction> action = (id<BOGGraphBooleanAction>)[FBGraphObject graphObject];
+
+ // set the action's results and two truth-value objects, using inferred accessor methods
+ // in our custom open graph action protocol
+ action.result = result ? @"1" : @"0";
+ action.truthvalue = [BOGFirstViewController ogObjectForTruthValue:left];
+ action.anothertruthvalue = [BOGFirstViewController ogObjectForTruthValue:right];
+
+
+ // post the action using one of the lightweight static start* methods on FBRequest
+ [FBRequestConnection startForPostWithGraphPath:actionPath
+ graphObject:action
+ completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
+ if (!error) {
+ // successful post, in the sample we do nothing with the id, however
+ // a more complex applicaiton may want to store or perform addtional actions
+ // with the id that represents the just-posted action
+ } else {
+ // get the basic error message
+ NSString *message = error.localizedDescription;
+
+ // see if we can improve on it with an error message from the server
+ id json = [error.userInfo objectForKey:FBErrorParsedJSONResponseKey];
+ if ([json isKindOfClass:[NSDictionary class]] &&
+ (json = [json objectForKey:@"body"]) &&
+ [json isKindOfClass:[NSDictionary class]] &&
+ (json = [json objectForKey:@"error"]) &&
+ [json isKindOfClass:[NSDictionary class]] &&
+ (json = [json objectForKey:@"message"])) {
+ message = [json description];
+ }
+
+ // display the message that we have
+ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OG Post Failed"
+ message:message
+ delegate:nil
+ cancelButtonTitle:@"OK"
+ otherButtonTitles:nil];
+ [alert show];
}
-
- // display the message that we have
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OG Post Failed"
- message:message
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alert show];
- }
- }];
+ }];
+ }
}
}
@@ -56,8 +56,7 @@ - (void)viewDidLoad {
[super viewDidLoad];
// Create Login View so that the app will be granted "status_update" permission.
- FBLoginView *loginview =
- [[FBLoginView alloc] initWithPermissions:[NSArray arrayWithObject:@"publish_actions"]];
+ FBLoginView *loginview = [[FBLoginView alloc] init];
loginview.frame = CGRectOffset(loginview.frame, 5, 5);
loginview.delegate = self;
@@ -119,37 +118,63 @@ - (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView {
// Post Status Update button handler
- (IBAction)postStatusUpdateClick:(UIButton *)sender {
-
- // Post a status update to the user's feed via the Graph API, and display an alert view
- // with the results or an error.
-
- NSString *message = [NSString stringWithFormat:@"Updating %@'s status at %@",
- self.loggedInUser.first_name, [NSDate date]];
-
- [FBRequestConnection startForPostStatusUpdate:message
- completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
-
- [self showAlert:message result:result error:error];
- self.buttonPostStatus.enabled = YES;
- }];
+ // we defer request for permission to post to the moment of post, then we check for the permission
+ if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
+ // if we don't already have the permission, then we request it now
+ [FBSession.activeSession reauthorizeWithPermissions:[NSArray arrayWithObject:@"publish_actions"]
+ behavior:FBSessionLoginBehaviorWithFallbackToWebView
+ defaultAudience:FBSessionDefaultAudienceFriends
+ completionHandler:^(FBSession *session, NSError *error) {
+ if (!error) {
+ // re-call ourselves assuming we now have the necessary permission
+ [self postStatusUpdateClick:sender];
+ }
+ }];
+ } else {
+ // Post a status update to the user's feed via the Graph API, and display an alert view
+ // with the results or an error.
- self.buttonPostStatus.enabled = NO;
+ NSString *message = [NSString stringWithFormat:@"Updating %@'s status at %@",
+ self.loggedInUser.first_name, [NSDate date]];
+
+ [FBRequestConnection startForPostStatusUpdate:message
+ completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
+
+ [self showAlert:message result:result error:error];
+ self.buttonPostStatus.enabled = YES;
+ }];
+
+ self.buttonPostStatus.enabled = NO;
+ }
}
// Post Photo button handler
- (IBAction)postPhotoClick:(UIButton *)sender {
-
- // Just use the icon image from the application itself. A real app would have a more
- // useful way to get an image.
- UIImage *img = [UIImage imageNamed:@"Icon-72@2x.png"];
-
- [FBRequestConnection startForUploadPhoto:img
- completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
- [self showAlert:@"Photo Post" result:result error:error];
- self.buttonPostPhoto.enabled = YES;
- }];
-
- self.buttonPostPhoto.enabled = NO;
+ // we defer request for permission to post to the moment of post, then we check for the permission
+ if ([FBSession.activeSession.permissions indexOfObject:@"publish_actions"] == NSNotFound) {
+ // if we don't already have the permission, then we request it now
+ [FBSession.activeSession reauthorizeWithPermissions:[NSArray arrayWithObject:@"publish_actions"]
+ behavior:FBSessionLoginBehaviorWithFallbackToWebView
+ defaultAudience:FBSessionDefaultAudienceFriends
+ completionHandler:^(FBSession *session, NSError *error) {
+ if (!error) {
+ // re-call ourselves assuming we now have the necessary permission
+ [self postPhotoClick:sender];
+ }
+ }];
+ } else {
+ // Just use the icon image from the application itself. A real app would have a more
+ // useful way to get an image.
+ UIImage *img = [UIImage imageNamed:@"Icon-72@2x.png"];
+
+ [FBRequestConnection startForUploadPhoto:img
+ completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
+ [self showAlert:@"Photo Post" result:result error:error];
+ self.buttonPostPhoto.enabled = YES;
+ }];
+
+ self.buttonPostPhoto.enabled = NO;
+ }
}
// Pick Friends button handler
@@ -60,28 +60,28 @@ - (void)buttonRequestClickHandler:(id)sender {
// login is integrated with the send button -- so if open, we send
[self sendRequests];
} else {
- [FBSession openActiveSessionWithPermissions:nil
- allowLoginUI:YES
- completionHandler:^(FBSession *session,
- FBSessionState status,
- NSError *error) {
- // if login fails for any reason, we alert
- if (error) {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
- message:error.localizedDescription
- delegate:nil
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [alert show];
- // if otherwise we check to see if the session is open, an alternative to
- // to the FB_ISSESSIONOPENWITHSTATE helper-macro would be to check the isOpen
- // property of the session object; the macros are useful, however, for more
- // detailed state checking for FBSession objects
- } else if (FB_ISSESSIONOPENWITHSTATE(status)) {
- // send our requests if we successfully logged in
- [self sendRequests];
- }
- }];
+ [FBSession openActiveSessionWithReadPermissions:nil
+ allowLoginUI:YES
+ completionHandler:^(FBSession *session,
+ FBSessionState status,
+ NSError *error) {
+ // if login fails for any reason, we alert
+ if (error) {
+ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
+ message:error.localizedDescription
+ delegate:nil
+ cancelButtonTitle:@"OK"
+ otherButtonTitles:nil];
+ [alert show];
+ // if otherwise we check to see if the session is open, an alternative to
+ // to the FB_ISSESSIONOPENWITHSTATE helper-macro would be to check the isOpen
+ // property of the session object; the macros are useful, however, for more
+ // detailed state checking for FBSession objects
+ } else if (FB_ISSESSIONOPENWITHSTATE(status)) {
+ // send our requests if we successfully logged in
+ [self sendRequests];
+ }
+ }];
}
}
Oops, something went wrong.

0 comments on commit 4dfb45b

Please sign in to comment.