2012-03-29 11 views
1

Ich habe viele Fragen über die Facebook-Login gelesen, aber bis ich nicht gelang es mein Problem zu lösen. Ich habe bisher ein paar Apps gemacht, aber bisher habe ich noch nie mit der AppDelegate zu tun gehabt.facebook fbDidLogin außerhalb appdelegate

Die Sache ist, dass ich die fbDidLogin-Methode, die in meiner Klasse anders als Delegate ist, aufrufen möchte.

Bis jetzt wird fbDidLogin in der AppDelegate aufgerufen, aber ich möchte in der Lage sein, die Ansicht zu ändern und nach der Anmeldung nach Benutzerinformationen zu fragen.

Mein Code so weit: AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window.rootViewController = self.tabBarController; 
    [self.window makeKeyAndVisible]; 

    facebook = [[Facebook alloc] initWithAppId:SHKCONFIG(facebookAppId) andDelegate:self]; 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
     && [defaults objectForKey:@"FBExpirationDateKey"]) { 
     facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"]; 
     facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"]; 
    } 

    return YES; 
} 

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{ 
    return [facebook handleOpenURL:url]; 
} 

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 
{ 
    return [facebook handleOpenURL:url]; 
} 

// This one is called correctly 
- (void)fbDidLogin { 
    NSLog(@"[DELEGATE] Deu login!"); 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"]; 
    [defaults synchronize]; 
} 

Meine eigene Klasse mit der Schaltfläche Facebook Login:

- (void)loginFacebook { 

    if (![facebook isSessionValid]) { 
     NSLog(@"[LOGIN] Facebook session invalid"); 
     NSArray * permissions = [[NSArray alloc] initWithObjects: 
           @"email", 
           @"user_birthday", 
           nil]; 
     [facebook authorize:permissions]; 
     [permissions release]; 
    } else { 
     NSLog(@"[LOGIN] Facebook session valid"); 
     [facebook requestWithGraphPath:@"me" andDelegate:self]; 
    } 
} 
// I want this one to work so I can ask for user info at the end of it 
- (void)fbDidLogin { 
    NSLog(@"[LOGIN][FB] Deu login!"); 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"]; 
    [defaults synchronize]; 
    [facebook requestWithGraphPath:@"me" andDelegate:self]; 
} 

mir leid, wenn dies nicht klar ist, wenn Sie mehr Informationen benötigen, lassen Sie mich kennt.

Antwort

5

Sie können eine Benachrichtigung von der App Delegierter veröffentlichen wie so:

- (void)fbDidLogin { 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"FBDidLoginCalled" object:nil]; 
} 

Und dann in der anderen Klasse:

- (void)loginFacebook { 

    if (![facebook isSessionValid]) { 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fbDidLoginNotificationFired) name:@"FBDidLoginCalled" object:nil] 
     NSLog(@"[LOGIN] Facebook session invalid"); 
     NSArray * permissions = [[NSArray alloc] initWithObjects: 
          @"email", 
          @"user_birthday", 
          nil]; 
     [facebook authorize:permissions]; 
     [permissions release]; 
    } else { 
     NSLog(@"[LOGIN] Facebook session valid"); 
     [facebook requestWithGraphPath:@"me" andDelegate:self]; 
    } 
} 
// I want this one to work so I can ask for user info at the end of it 
- (void)fbDidLoginNotificationFired { 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"FBDidLoginCalled" object:nil]; 

    NSLog(@"[LOGIN][FB] Deu login!"); 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"]; 
    [defaults synchronize]; 
    [facebook requestWithGraphPath:@"me" andDelegate:self]; 
} 
+0

Super ... gute Idee .. !! –

-1

Ich schaffte es, indem Sie diesen Code hinzufügen auf dem -loginFacebook:

Dann auf meiner AppDelegate habe ich eine Eigenschaft von meinem Login-Controller erstellt und dann die co ntrollers -fbDidLogin:

- (void)fbDidLogin { 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"]; 
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"]; 
    [defaults synchronize]; 
    [self.loginController fbDidLogin]; 
} 
+0

Warum wurde das abgelehnt? – Jimmy