[英]Show the list of Facebook friends in IPhone app
我正在制作一个连接Facebook的iPhone应用程序。我在第一个视图控制器的按钮单击上将其连接。现在我添加第二个按钮并在其中打开第二个视图控制器,我想按字母顺序显示我的朋友列表我在网上搜索它,但是当我在应用程序中应用它时,它在运行时崩溃,请帮助我-
在我的代表班上
(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"];
}
我想在其中显示好友列表的第二个视图控制器
- (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];
}
任何帮助表示赞赏。
问题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方案。
ps。 您不应该将您的AppId发布到公共论坛上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.