0

I am making an IPhone application in which I connect the Facebook.I am connecting it on button click of my first view controller.Now I add second button and open second view controller in this I want to show the list of my friends in alphabetical order.I search it on net but when I applied it in my application ,it crash on run time please help me-

in my delegate class

 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    navController =[[UINavigationController alloc] initWithRootViewController:self.viewController];

    self.window.rootViewController = navController;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }            
    [self.window makeKeyAndVisible];
    return YES;
}

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

My second view controller in which I want to show the list of my friends in loading

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.        
    NSArray   *_permissions = [[NSArray arrayWithObjects:@"read_stream", @"publish_stream", @"offline_access",@"read_friendlists",nil] retain];        
    [facebook authorize:_permissions];        
    facebook = [[Facebook alloc] initWithAppId:@"APP_ID_HERE" andDelegate:self];        
    [facebook requestWithGraphPath:@"me" andDelegate:self];
    [facebook requestWithGraphPath:@"me/friends" andDelegate:self];
  //  NSMutableDictionary*params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"4",@"uids", @"name", @"fields", nil];
    [facebook requestWithGraphPath:@"me/friends" 
                         andParams:[ NSDictionary dictionaryWithObjectsAndKeys:@"picture,id,name,link,gender,last_name,first_name",@"fields",nil]
                       andDelegate:self];        
}

Any help is appreciated.

4

1 回答 1

0

问题 #1:这一行:

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

在尝试读取 NSUserDefaults 并访问尚未初始化的 Facebook 对象之前,需要在 didFinishLaunchingWithOptions 中的应用程序委托中。它也应该从您的视图控制器中删除。

问题 #2:您的应用程序委托和视图控制器中似乎都声明了一个 facebook 变量。您的整个应用程序中应该只有一个 Facebook 实例。当您从视图控制器调用它时,您应该执行以下操作来获取对存储在应用程序委托中的 Facebook 实例的引用:

Facebook *facebook = ((MyAppDelegate *)[[UIApplication sharedApplication] delegate]).facebook;

您还需要确保您在应用程序委托中实现了 openURL 方法,并按照设置说明在您的 plist 文件中创建 URL 方案。

附言。您不应该将您的 AppId 发布到公共论坛上。

于 2012-05-17T06:58:40.870 回答