我已将我的应用配置为正确允许用户通过FB Connect 3.1 SDK(适用于iOS 6)登录,并请求电子邮件权限:
NSArray *permissons = [[NSArray alloc] initWithObjects:@"email", nil];
[FBSession openActiveSessionWithReadPermissions:permissons
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
之后,我需要确定用户的FB会话ID。这与访问令牌不同吗?我相信Session / Token ID可能以'AAAFTZCN54f1EBA'开头?如何在身份验证后检索此信息?
提前致谢!
答案 0 :(得分:1)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
internetActive=YES;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
self.navController=[[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.navController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
self.window.rootViewController = self.navController;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
_internetReachable = [Reachability reachabilityForInternetConnection];
[_internetReachable startNotifier];
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
// Yes, so just open the session (this won't display any UX).
[self openSession];
} else {
// No, display the login page.
[self showLoginView];
}
[self.window makeKeyAndVisible];
return YES;
}
-(void)checkNetworkStatus:(NSNotification *)notice
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Internet Connection" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
// called after network status changes
NetworkStatus internetStatus = [_internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
alert.message=@"The internet is down.";
internetActive = NO;
break;
}
case ReachableViaWiFi:
{
alert.message=@"The internet is working via WIFI.";
internetActive = YES;
break;
}
case ReachableViaWWAN:
{
alert.message=@"The internet is working via WWAN.";
internetActive = YES;
break;
}
}
[alert show];
}
#pragma mark- Facebook methods
-(void)getEmailId
{
[facebook requestWithGraphPath:@"me" andDelegate:self];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}
-(void)fblogout{
NSLog(@"logout called ");
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"fbemail"];
[FBSession.activeSession closeAndClearTokenInformation];
[facebook logout];
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
UIViewController *topViewController =[self.navController topViewController];
if ([topViewController isKindOfClass:[Sport_InceptionViewController class]]) {
[topViewController dismissViewControllerAnimated:YES completion:nil];
// [topViewController dismissModalViewControllerAnimated:YES];
}
}
// Initiate a Facebook instance
facebook = [[Facebook alloc]
initWithAppId:FBSession.activeSession.appID
andDelegate:nil];
// Store the Facebook session information
facebook.accessToken = FBSession.activeSession.accessToken; **Here is you token**
facebook.expirationDate = FBSession.activeSession.expirationDate;
if (![[NSUserDefaults standardUserDefaults] valueForKey:@"fbemail"]) {
[MBProgressHUD showHUDAddedTo:self.viewController.view animated:YES];
[self getEmailId];
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
// Once the user has logged in, we want them to
// be looking at the root view.
[self.navController popToRootViewControllerAnimated:NO];
[FBSession.activeSession closeAndClearTokenInformation];
facebook = nil;
[self showLoginView];
break;
default:
break;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:FBSessionStateChangedNotification
object:session];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (void)openSession
{
if (internetActive) {
NSArray *permissions=[NSArray arrayWithObjects:@"read_stream",@"email",nil];
[FBSession openActiveSessionWithPermissions:permissions allowLoginUI:YES completionHandler: ^(FBSession *session,FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}else
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:@"Internet Not Connected" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
}
#pragma mark- request delegate methods
- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(@"request did load successfully....");
// __block NSDictionary *dictionary=[[NSDictionary alloc] init];
if ([result isKindOfClass:[NSDictionary class]]) {
NSDictionary* json = result;
NSLog(@"email id is %@",[json valueForKey:@"email"]);
NSLog(@"json is %@",json);
[[NSUserDefaults standardUserDefaults] setValue:[json valueForKey:@"email"] forKey:@"fbemail"];
[self.viewController login:YES];
}
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"" message:@"Server not responding.." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
[self fblogout];
[self showLoginView];
NSLog(@"request did fail with error");
}
- (void)showLoginView
{
UIViewController *topViewController = [self.navController topViewController];
if (![topViewController isKindOfClass:[MyViewController class]]) {
MyViewController *loginViewController=[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[topViewController presentViewController:loginViewController animated:YES completion:nil];
}
else {
MyViewController* loginViewController=(MyViewController*)topViewController;
[loginViewController loginFailed];
}
}