我的Facebook应用程序崩溃,而身份验证屏幕被解雇

时间:2012-06-26 14:51:06

标签: objective-c ios facebook

我正在尝试将facebook iOS SDK集成到我的应用程序中,在我的app delegate标题中,我执行以下操作:

 #import <UIKit/UIKit.h>
#import "Facebook.h"
#import "FBConnect.h"

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
Facebook *facebook;
}

@property (nonatomic,strong) Facebook *facebook;

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end

并在实现文件的方法didFinishLaunchingWithOptions方法:

MyFacebooDelegate *controllerDelegate = [[MyFacebooDelegate alloc] init];
facebook = [[Facebook alloc] initWithAppId:appID andDelegate:controllerDelegate];

NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
if([userDefault objectForKey:@"FBAccessTokenKey"] && [userDefault objectForKey:@"FBExpirationDateKey"])
{
    facebook.accessToken = [userDefault objectForKey:@"FBAccessTokenKey"];
    facebook.expirationDate = [userDefault objectForKey:@"FBExpirationDateKey"];

}

if(![facebook isSessionValid])
{
    NSArray *permision = [[NSArray alloc]initWithObjects:@"read_stream",nil] ;
    [facebook authorize:permision];
}

MyFacebooDelegate类是我实现Facebook委托的地方,比如FBSessionDelegate和其他人。

我也处理了handleOpenURL和OpenURL,当我运行应用程序时,我在safari中获取facebook身份验证屏幕,然后按“Okay”屏幕被解雇并返回到我的应用程序,但有时候应用程序崩溃并退出这是编译器告诉我错误的地方:

- (void)fbDialogLogin:(NSString *)token expirationDate:(NSDate *)expirationDate {
self.accessToken = token;
self.expirationDate = expirationDate;
[_lastAccessTokenUpdate release];
_lastAccessTokenUpdate = [[NSDate date] retain];
[self reloadFrictionlessRecipientCache];
if ([self.sessionDelegate respondsToSelector:@selector(fbDidLogin)]) {
    [self.sessionDelegate fbDidLogin];
}

具体来说,编译器会指出这一行:

if ([self.sessionDelegate respondsToSelector:@selector(fbDidLogin)]) {

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:3)

穆罕默德

以下行错误:

if ([self.sessionDelegate respondsToSelector:@selector(fbDidLogin)]) {

它应该是这样的:

if ([self.sessionDelegate respondsToSelector:@selector(fbDidLogin:)]) {

答案 1 :(得分:2)

实例化会话代理时:

MyFacebooDelegate *controllerDelegate = [[MyFacebooDelegate alloc] init];
facebook = [[Facebook alloc] initWithAppId:appID andDelegate:controllerDelegate];

您不以任何其他方式保留它。如果查看Facebook SDK文件Facebook.h,您会发现sessionDelegate属性的类型为assign。这意味着您必须负责在发送消息时确保委托对象存在。

要解决此问题,请添加AppDelegate.h文件:

@property (strong, nonatomic) MyFacebooDelegate *controllerDelegate;

didFinishLaunchingWithOptions:中,而不是帖子顶部的代码,请执行:

self.controllerDelegate = [[MyFacebooDelegate alloc] init];
facebook = [[Facebook alloc] initWithAppId:appID andDelegate:self.controllerDelegate];

这样,您的委托对象将保留一个强引用,并且不会过早地释放它。

希望这有帮助!如果您有任何问题,请告诉我。