2012-03-24 5 views
0

iOS 프로젝트에서 Facebook SDK를 구현하는 데 문제가 있습니다. UIActionSheet를 통해 내 응용 프로그램의 특정 섹션에서 호출하려고합니다. Facebook에 로그인 할 수 있지만 일단 내 앱으로 리디렉션되면 앱이 로그인되어 있지 않아 공유 기능을 실행할 수 없습니다.앱으로 돌아 가면 Facebook 로그인이 제대로 인식되지 않습니다.

나는 프로젝트 정보에서 "fbMY_APP_ID"을 추가 포함 https://developers.facebook.com/docs/mobile/ios/build/에서 페이스 북의 아이폰 OS 튜토리얼을 따라했지만 그것은 내가 로그인하고 있음을 인식하지 않습니다 이유를 알아낼 수 없습니다. 여기

가 관련 헤더 인 및 방법을 사용하고 있습니다.

//Header 


#import <UIKit/UIKit.h> 
#import <MessageUI/MessageUI.h> 
#import "MWFeedItem.h" 
#import "FBConnect.h" 


@interface DetailTableViewController : UITableViewController <UIAlertViewDelegate, UIActionSheetDelegate, UIApplicationDelegate, FBSessionDelegate>{ 

Facebook *facebook; 
BOOL login; 

} 

@property (nonatomic, strong) Facebook *facebook; 


@end 

및 방법 : 편의를 위해 페이스 북의 구현과 관련이없는 항목을 삭제 한

// Method 

#import "DetailTableViewController.h" 
#import "NSString+HTML.h" 
#import <Twitter/Twitter.h> 

@implementation DetailTableViewController 

@synthesize facebook; 

//Navigation button on detail view does THIS: 

- (void)navAction { 
    NSLog(@"Action sheet activated."); 

    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
           initWithTitle:@"Like this story?" 
           delegate:self 
           cancelButtonTitle:@"Cancel" 
           destructiveButtonTitle:@"Email link" 
           otherButtonTitles:@"Share on Facebook", @"Share on Twitter", @"Open in Safari", nil]; 

     [actionSheet showInView:[self.view window]]; 

} 

-(void)actionSheet: (UIActionSheet *) actionSheet willDismissWithButtonIndex:(NSInteger) buttonIndex{ 

///////THIS SECTION DEFINES THE EMAIL SHARE FUNCTION///// 


if (buttonIndex == 0) { 

    // MFMailComposeViewController implementation 

} 


///////THIS SECTION DEFINES THE FACEBOOK SHARE FUNCTION///// 

    else { 
     if (buttonIndex == 1) { 

      if (login == NO){ 

       NSLog(@"No login detected"); 

       UIAlertView *infoAlert = [[UIAlertView alloc] 
              initWithTitle:@"Login necessary" 
              message:@"You must first login to your Facebook account to use this feature." 
              delegate:self 
              cancelButtonTitle:@"Login" 
              otherButtonTitles:@"Cancel", nil]; 
       [infoAlert show]; 
      }else { 
       if (login == YES){ 

       NSLog(@"Login detected."); 

       NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
       [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"]; 
       [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationKey"]; 
       [defaults synchronize]; 

       //APPLY LOGIN ACTION HERE. Waiting until I can make the system run the NSLog above. So far, the login is not being correctly recorded. 

       } 
      } 


     } 

     ///////THIS SECTION DEFINES THE TWITTER SHARE FUNCTION///// 

     else { 
     if (buttonIndex == 2) { 

      //Twitter implementation here     
     } 

    ///////THIS SECTION DEFINES THE SAFARI FUNCTION///// 


     else{ 
      if (buttonIndex == 3){ 

      //Safari implementation here      
      } 

     } 
     } 

    } 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
     { 
      if (buttonIndex == 0) 

      { 

       // CALL TO FACEBOOK HERE 

       facebook = [[Facebook alloc] initWithAppId:@"MY_APP_ID" andDelegate:self]; 

       NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

       if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]){ 
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; 
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; 

       } 

       if (![facebook isSessionValid]){ 

        NSLog(@"Reached isSessionValid evaluation."); 

        [facebook authorize:nil]; 
       } 

      } 
      else if (buttonIndex == 1) 
      { 
       NSLog(@"Pressed cancel button"); 


      } 
     } 

// THIS METHOD SUPPORTS IOS PRE 4.2 
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ 
return [facebook handleOpenURL:url]; 
} 

// THIS METHOD SUPPORTS IOS AFTER 4.2 
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ 
return [facebook handleOpenURL:url]; 
} 


//////////////////////////////////////////// 

- (void)fbDidNotLogin:(BOOL)cancelled{ 

} 

- (void)fbDidExtendToken:(NSString*)accessToken 
      expiresAt:(NSDate*)expiresAt{ 

} 

- (void)fbDidLogout{ 

} 

- (void)fbSessionInvalidated{ 

} 

- (void) fbDidLogin { 

    NSLog(@"Logged in with Facebook credentials"); 

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationKey"]; 
    [defaults synchronize]; 

//APPLY LOGIN ACTION HERE. Waiting until I can make the system run the NSLog above. So far, the login is not being correctly recorded. 

} 

추가 정보 :

나는 또한 주에의 OpenURL 부분을 추가하려고했습니다 응용 프로그램 위임 파일이지만 동일한 결과가있었습니다.

+0

공유 방법은 어디에 있습니까? 어디에서 불러? –

+0

당분간 제거했습니다. 현재 내가하려고하는 것은 앱이 NSLog 활동을 시청하여 로그인했음을 인식하도록하는 것입니다. 필자는 로그인 한 후 fbDidLogin 메서드가 호출되어야하는 앱으로 리디렉션 된 것으로 생각했습니다. 나는 틀린가? –

+0

매번 fbDidLogin : 메서드를 호출하지 않습니다. 이 메소드는 사용자 기본값에 토큰 및 만료 날짜가 없거나 사용자 토큰에 토큰 및 만기 날짜가 유효하지 않은 경우에만 호출됩니다. –

답변

1

프로젝트에서이 코드를 사용해보십시오. 나는 당신과 같은 이슈를 가지고있었습니다. 이제 내 코드가 완벽하게 작동합니다. fbDidLogin 방법으로 sharing method으로 전화하십시오. 코드에서 App id을 변경하는 것을 잊지 말고 .plist 파일로 이동하십시오.

-(void)buttonPressed:(id)sender{ 
facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID" andDelegate:self]; 

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
     if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) { 
      facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; 
      facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; 
     } 
if (![facebook isSessionValid]) { 
    [facebook authorize:nil]; 
}else{ 
[self postWall]; 
} 
// Pre 4.2 support 
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 
    return [facebook handleOpenURL:url]; 
} 
- (void)fbDidLogin { 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"]; 
    [defaults synchronize]; 
    [self postWall]; 
} 
-(void)postWall{ 

    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
            @"https://developers.facebook.com/docs/reference/dialogs/",@"link", 
            @"http://fbrell.com/f8.jpg",@"picture", 
            @"Facebook Dialogs",@"name", 
            @"Reference Documentation",@"caption", 
            @"Using Dialogs to interact with users.",@"description", 
            @"Facebook Dialogs are so easy!",@"message", 
            nil]; 

    [[self facebook] dialog:@"feed" andParams:params andDelegate:self]; 

} 
+0

이렇게하면 같은 결과가 나타납니다. 앱으로 리디렉션 된 후 활동이 없습니다. –

+0

코드에 "BOOL 로그인"에 문제가 있다고 생각합니다. 당신은 어디서나 가치를 바꾸지 않습니다. 그래서 나는 항상 NO라고 말하고 alertView를 호출하고 로그인하지 않는 것 같아요. –

0

일부 비난 된 Facebook API 메소드와 관련 될 수 있습니다. 도움이 필요하면이 게시물을 참조하십시오. related question

+0

수도 있습니다, Brooks. 나는 단지 해결책을 찾기 위해 노력하고있다. 그 이전의 질문도 저였습니다. –

+0

LOL 내 잘못 ... 원하는 경우 내 대답을 삭제합니다 ...;) –

+0

걱정할 필요가 없습니다. 모든 응답을 감사하십시오. –

관련 문제