#import "LoginViewController.h"
#import <FacebookSDK/FacebookSDK.h>
#import "WelcomeViewController.h"
@interface LoginViewController ()
@end
@implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
FBLoginView *loginView = [[FBLoginView alloc] initWithReadPermissions: @[@"public_profile", @"email", @"user_friends"]];
[self.view addSubview:loginView];
loginView.delegate = self;
}
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user {
self.profilePictureView.profileID = user.objectID;
self.nameLabel.text = user.name;
//
// UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//
// WelcomeViewController * viewController = [storyboard instantiateViewControllerWithIdentifier:@"WelcomeViewController"];
// viewController.username.text = user.name;
// [self presentViewController:viewController animated:YES completion:nil];
}
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView {
self.statusLabel.text = @"You're logged in as";
}
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView {
self.profilePictureView.profileID = nil;
self.nameLabel.text = @"";
self.statusLabel.text= @"You're not logged in!";
}
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {
NSString *alertMessage, *alertTitle;
// If the user performs an action outside of you app to recover,
// the SDK provides a message, you just need to surface it.
// This handles cases like Facebook password change or unverified Facebook accounts.
if ([FBErrorUtility shouldNotifyUserForError:error]) {
alertTitle = @"Facebook error";
alertMessage = [FBErrorUtility userMessageForError:error];
// This code will handle session closures that happen outside of the app
// You can take a look at our error handling guide to know more about it
// https://developers.facebook.com/docs/ios/errors
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {
alertTitle = @"Session Error";
alertMessage = @"Your current session is no longer valid. Please log in again.";
// If the user has cancelled a login, we will do nothing.
// You can also choose to show the user a message if cancelling login will result in
// the user not being able to complete a task they had initiated in your app
// (like accessing FB-stored information or posting to Facebook)
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
NSLog(@"user cancelled login");
// For simplicity, this sample handles other errors with a generic message
// You can checkout our error handling guide for more detailed information
// https://developers.facebook.com/docs/ios/errors
} else {
alertTitle = @"Something went wrong";
alertMessage = @"Please try again later.";
NSLog(@"Unexpected error:%@", error);
}
if (alertMessage) {
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
@end