2013-06-07 10 views
0

내가 올바르게 사용자가 FB 연결을 통해 요청 이메일 권한 (아이폰 OS 6) 3.1 SDK를 로그인 할 수 있도록 내 응용 프로그램을 구성한 :Facebook Connect 사용자의 FB 세션/액세스 토큰 ID에 액세스 하시겠습니까?

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를 결정해야합니다. 액세스 토큰과 다른가요? 세션/토큰 ID가 'AAAFTZCN54f1EBA'로 시작한다고 생각합니까? 이 정보를 어떻게 확인할 수 있습니까?

미리 감사드립니다.

+0

'FBSession.activeSession.accessToken'을 (를) 찾고 계십니까? – Mar0ux

답변

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: 
    { 
    [email protected]"The internet is down."; 
    internetActive = NO; 
    break; 
    } 
    case ReachableViaWiFi: 
    { 
    [email protected]"The internet is working via WIFI."; 
    internetActive = YES; 
    break; 
    } 
    case ReachableViaWWAN: 
    { 
    [email protected]"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]; 
    } 
} 
+0

"Facebook"클래스 란 무엇입니까? 나는 그들의 문서에서 그것을 보지 못하며 프레임 워크에 없다. – sudo