简体   繁体   English

在iPhone应用程序中显示Facebook朋友列表

[英]Show the list of Facebook friends in IPhone app

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- 我正在制作一个连接Facebook的iPhone应用程序。我在第一个视图控制器的按钮单击上将其连接。现在我添加第二个按钮并在其中打开第二个视图控制器,我想按字母顺序显示我的朋友列表我在网上搜索它,但是当我在应用程序中应用它时,它在运行时崩溃,请帮助我-

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. 任何帮助表示赞赏。

Issue #1: This line: 问题1:此行:

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

needs to be in your app delegate in didFinishLaunchingWithOptions before you try and read NSUserDefaults and access the Facebook object that has not been initialized yet. 在尝试读取NSUserDefaults并访问尚未初始化的Facebook对象之前,需要先在didFinishLaunchingWithOptions中的应用程序委托中。 It should also be removed from your view controller. 它也应该从您的视图控制器中删除。

Issue #2: It appears you have a facebook variable declared in both your app delegate and your view controller. 问题2:看来您在应用程序委托和视图控制器中都声明了一个facebook变量。 You should only have one Facebook instance in your whole application. 您的整个应用程序中应该只有一个Facebook实例。 When you are calling it from your view controller you should do something like this to get a reference to the Facebook instance stored at your app delegate: 从视图控制器调用它时,您应该执行以下操作以获取对存储在应用程序委托中的Facebook实例的引用:

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

You also need to make sure you implemented the openURL method in your app delegate and followed the setup instructions to create the URL scheme in your plist file. 您还需要确保在应用程序委托中实现了openURL方法,并按照设置说明在plist文件中创建URL方案。

ps. ps。 You shouldn't post your AppId onto a public forum. 您不应该将您的AppId发布到公共论坛上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM