Bjö*_*ser 5 cocoa facebook objective-c
由于山狮现在已经officialy Facebook的整合,我想打一点点用Accounts.framework和Social.frameworkOS X的
我在Facebook Developer App中设置了一个应用程序,并使用以下代码请求访问登录用户的Facebook帐户:
ACAccountStore *acStore = [[ACAccountStore alloc] init];
ACAccountType *accType = [acStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:FB_APP_ID, ACFacebookAppIdKey, @"read_stream, publish_stream", ACFacebookPermissionsKey, ACFacebookAudienceFriends, ACFacebookAudienceKey, nil];
[acStore requestAccessToAccountsWithType:accType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(@"Accounts: %@", acStore.accounts);
NSLog(@"Access granted");
} else {
NSLog(@"Access denied");
}
}];
Run Code Online (Sandbox Code Playgroud)
现在我总是得到错误 Error Domain=XPCObjectsErrorDomain Code=2 "The operation couldn’t be completed. (XPCObjectsErrorDomain error 2.)"
当我从Xcode首次启动应用程序2-3次时,OS X显示确认对话框"XYZ想要访问您的Facebook帐户",我点击确定,我收到一条错误,上面写着"应用程序尚未安装".此错误现在"消失",我收到上述错误.
我的问题是:我是否必须在我的Xcode项目(Info.plist)或Facebook Developer App中配置一些特殊功能才能使其与OS X一起使用?或者我在代码中遗漏了什么?关于此的样本和教程似乎非常罕见: - /
我已经观看了关于Twitter/Facebook集成的WWDC 2012视频,但它几乎使用了我使用的代码.
任何帮助表示赞赏:-)
干杯,Björn
更新
我解决了这个问题,我只是没有仔细阅读文档;-)我必须将权限作为NSArray而不是字符串传递.我已将选项字典修改为如下所示:
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
FB_APP_ID, ACFacebookAppIdKey,
[NSArray arrayWithObjects:@"read_stream", @"publish_stream", @"email", @"user_likes", nil], ACFacebookPermissionsKey,
ACFacebookAudienceFriends, ACFacebookAudienceKey, nil];
现在OS X显示一条警告,询问我是否要允许应用程序代表我发布到Facebook,我单击确定并出现新错误,导致无法访问Facebook帐户:
Error Domain=com.apple.accounts Code=7 "The Facebook server could not fulfill this access request: The app must ask for a basic read permission at install time."
我已经将权限修改为公正email或公正,user_likes但都导致了相同的错误.如果我仅请求访问权限,read_stream则会获得访问权限但会导致OAuth错误190与消息一起Error validating access token: User XXXXXX has not authorized application XXXXXX.
更新2:
我现在解决了所有问题,请参阅下面的答案.
Bjö*_*ser 11
好吧,我终于解决了它:-)
当您第一次请求访问用户Facebook帐户时,您只能从Facebook的权限参考中的"用户和朋友权限"下列出的用户请求权限.
一旦用户授予访问权限并且在Facebook上也安装了该应用程序,您可以请求您的应用程序所需的任何其他权限.重要的是,您可能不会同时要求读取和写入权限,一个接一个!因此,请求权限read_stream并publish_stream在同一请求中将无效.
这基本上就是我使用的代码:
self.store = [[ACAccountStore alloc] init];
ACAccountType *accType = [self.store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObjectsAndKeys:
FB_APP_ID, ACFacebookAppIdKey,
[NSArray arrayWithObjects:@"email", @"user_about_me", @"user_likes", nil], ACFacebookPermissionsKey, ACFacebookAudienceFriends, ACFacebookAudienceKey, nil];
[self.store requestAccessToAccountsWithType:accType options:options completion:^(BOOL granted, NSError *error) {
if (granted && error == nil) {
self.accounts = self.store.accounts;
[options setObject:[NSArray arrayWithObjects:@"read_stream, read_mailbox, read_requests", nil] forKey:ACFacebookPermissionsKey];
[self.store requestAccessToAccountsWithType:accType options:options completion:^(BOOL granted, NSError *error) {
if (granted && error == nil) {
completionHandler(granted, error);
} else {
NSLog(@"Access denied 2");
NSLog(@"%@", [error description]);
}
}];
} else {
NSLog(@"Error: %@", [error description]);
NSLog(@"Access denied");
}
}];
Run Code Online (Sandbox Code Playgroud)
希望这会对某人有所帮助:-)