Har*_*ain 1 iphone xcode facebook ipad ios
Facebook SDK 3.2
我需要在使用faceboook登录后在Facebook上发布一个墙贴.
这是我正在使用的代码:
在这里,我打开了Facebook会议:
- (IBAction)FBSignup:(id)sender {
[[AppDelegate sharedObject] openSession:^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
Run Code Online (Sandbox Code Playgroud)
在状态变化,我用[self populateUserDetails]填充数据,然后尝试在Facebook墙上发布[self postOnFB],但Facebook墙上没有帖子.
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
[self populateUserDetails];
[self postOnFB];
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:SCSessionStateChangedNotification
object:session];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我用我需要的facebook用户对象弹出数据.
- (void)populateUserDetails
{
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
///////////////////////////////
// Populating Data Where Needed
///////////////////////////////
// [self postOnFB];
}
}];
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我尝试用[self postOnFB]发布墙贴,它给出了错误:
由于未捕获的异常'com.facebook.sdk:InvalidOperationException'终止应用程序,原因:'FBSession:在先前的重新授权调用尚未完成时重新授权无效.
我在这里贴墙.
- (void)postOnFB{
// Ask for publish_actions permissions in context
if ([FBSession.activeSession.permissions
indexOfObject:@"publish_actions"] == NSNotFound) {
// No permissions found in session, ask for it
[FBSession.activeSession requestNewPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
[self fbPostSettings];
}
}];
} else {
// If permissions present, publish the story
[self fbPostSettings];
}
}
Run Code Online (Sandbox Code Playgroud)
为facebook wall post设置参数:
- (void)fbPostSettings{
NSDictionary * facebookParams = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
kFacebookPostLink, @"link",
kFacebookPostImage, @"picture",
kFacebookPostName, @"name",
kFacebookPostCaption, @"caption",
kFacebookPostDescription, @"description",
nil];
[FBRequestConnection startWithGraphPath:@"me/feed"
parameters:facebookParams
HTTPMethod:@"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSString *alertText;
if (error) {
alertText = [NSString stringWithFormat:
@"error: domain = %@, code = %d",
error.domain, error.code];
} else {
alertText = [NSString stringWithFormat:
@"Posted action, id: %@",
[result objectForKey:@"id"]];
}
NSLog(@"%@",alertText);
}];
}
- (void)sessionStateChanged:(NSNotification*)notification {
[self populateUserDetails];
}
Run Code Online (Sandbox Code Playgroud)
如果我在某个按钮操作选择器上调用postOnFB(在通过facebook用户对象完成数据填充之后),它会在墙上发布.但是我需要在startWithCompletionHandler中获取用户对象之后发布它:^(FBRequestConnection*connection,NSDictionary*user,NSError*error)方法.
请帮忙.谢谢大家 :)
小智 6
而不是打开会话然后请求发布权限,直接请求发布权限以打开会话
[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObjects:@"publish_stream",
nil] defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session,
FBSessionState state, NSError *error) {
if (FBSession.activeSession.isOpen && !error) {
[self fbPostSettings];
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3052 次 |
| 最近记录: |