我正在制作一个IPhone应用程序,我在其中连接Facebook。我在第一个视图控制器的按钮上连接它。现在我添加第二个按钮并在此打开第二个视图控制器我想显示我的朋友列表按字母顺序排序。我在网上搜索但是当我在我的网站上应用它时 应用程序,它在运行时崩溃请帮助我 -
在我的委托类
中 (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];
}
感谢任何帮助。
答案 0 :(得分:0)
问题#1:这一行:
facebook = [[Facebook alloc] initWithAppId:@"XXXXXXXXXXXXX" andDelegate:self];
在尝试读取NSUserDefaults并访问尚未初始化的Facebook对象之前,需要位于didFinishLaunchingWithOptions中的app委托中。它也应该从视图控制器中删除。
问题#2:您的app delegate和视图控制器中都显示了一个facebook变量。您应该在整个应用程序中只有一个Facebook实例。当您从视图控制器中调用它时,您应该执行以下操作以获取对应用程序代理中存储的Facebook实例的引用:
Facebook *facebook = ((MyAppDelegate *)[[UIApplication sharedApplication] delegate]).facebook;
您还需要确保在app委托中实现了openURL方法,并按照设置说明在plist文件中创建URL方案。
PS。你不应该将你的AppId发布到公共论坛上。