facebook fbDidLogin外面appdelegate [英] facebook fbDidLogin outside appdelegate
问题描述
我已经阅读了许多关于Facebook登录的问题,但直到我没有成功解决我的问题。我已经做了几个应用程序到目前为止,但我从来没有搞过appDelegate之前。
事情是,我想调用我的类别中的fbDidLogin方法其他而不是委托。
到目前为止,fbDidLogin正在appDelegate中被调用,但是我希望能够在登录发生后更改视图并请求用户信息。 / p>
我的代码到目前为止:
AppDelegate:
(BOOL)应用程序:(UIApplication *)应用程序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]
&&&&(&);默认值为ObjectForKey:@FBExpirationDateKey]){
facebook.accessToken = [defaults objectForKey:@FBAccessTokenKey ];
facebook.expirationDate = [defaults objectForKey:@FBExpirationDateKey];
}
返回YES;
}
- (BOOL)应用程序:(UIApplication *)应用程序openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication注释:(id)注释
{
return [facebook handleOpenURL:url];
}
- (BOOL)应用程序:(UIApplication *)应用程序handleOpenURL:(NSURL *)url
{
return [facebook handleOpenURL:url];
}
//这个被正确调用
- (void)fbDidLogin {
NSLog(@[DELEGATE] Deu login!);
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@FBAccessTokenKey];
[defaults setObject:[facebook expirationDate] forKey:@FBExpirationDateKey];
[默认同步];
}
我自己的课程与Facebook登录按钮:
- (void)loginFacebook {
if(![facebook isSessionValid]){
NSLog(@[LOGIN ] Facebook会话无效);
NSArray * permissions = [[NSArray alloc] initWithObjects:
@email,
@user_birthday,
nil];
[facebook authorize:permissions];
[权限释放];
} else {
NSLog(@[LOGIN] Facebook会话有效);
[facebook requestWithGraphPath:@meandDelegate:self];
}
}
//我想要这个工作,所以我可以在它的末尾要求用户信息
- (void)fbDidLogin {
NSLog( @[LOGIN] [FB] Deu login!);
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@FBAccessTokenKey];
[defaults setObject:[facebook expirationDate] forKey:@FBExpirationDateKey];
[默认同步];
[facebook requestWithGraphPath:@meandDelegate:self];
}
我很抱歉,如果这不清楚,如果您需要更多信息让我知道
您可以从App代理发布通知,如下所示:
- (void)fbDidLogin {
[[NSNotificationCenter defaultCenter] postNotificationName:@FBDidLoginCalledobject:nil];
}
然后在您的其他类中:
- (void)loginFacebook {
if(![facebook isSessionValid]){
[[NSNotificationCenter defaultCenter] addObserver:self选择器:@selector(fbDidLoginNotificationFired)名称:@FBDidLoginCalled对象:nil]
NSLog(@[LOGIN] Facebook会话无效);
NSArray * permissions = [[NSArray alloc] initWithObjects:
@email,
@user_birthday,
nil];
[facebook authorize:permissions];
[权限释放];
} else {
NSLog(@[LOGIN] Facebook会话有效);
[facebook requestWithGraphPath:@meandDelegate:self];
}
}
//我想要这个工作,所以我可以在它的结尾要求用户信息
- (void)fbDidLoginNotificationFired {
[[ NSNotificationCenter defaultCenter] removeObserver:self name:@FBDidLoginCalledobject:nil];
NSLog(@[LOGIN] [FB] Deu login!);
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@FBAccessTokenKey];
[defaults setObject:[facebook expirationDate] forKey:@FBExpirationDateKey];
[默认同步];
[facebook requestWithGraphPath:@meandDelegate:self];
}
I have read many questions about the facebook login but until not I didnt succeed on solving my problem. I have made a few apps so far but I have never messed with the appDelegate before.
The thing is that I want to call the fbDidLogin method that is inside my class other than delegate.
Until now, fbDidLogin is being called in the appDelegate, but I want to be able to change the view and ask for user info after the login happens.
My code so far: 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];
}
My own class with the facebook login button:
- (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];
}
I sorry if this is not clear, if you need more info let me know.
You can post a notification from the App Delegate like so:
- (void)fbDidLogin {
[[NSNotificationCenter defaultCenter] postNotificationName:@"FBDidLoginCalled" object:nil];
}
And then in your other class:
- (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];
}
这篇关于facebook fbDidLogin外面appdelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!