感谢@Jai Govindani 的回答,但它比您的回答简单得多。我是从 Facebook 文档中发现的。
首先我更改 Facebook 帐户中的应用设置:
我在应用程序的设置中添加了 Bundle 标识符。并启用 Facebook deep linking
当然,我还需要 "App store ID"
并用completionBlock写了下面的方法 -
- (void)shareOnFacebookWithName:(NSString *)strName withDescription:(NSString *)strDesc withLink:(NSString *)strLink WithPictureLink:(NSString *)strPicLink withCaption:(NSString *)strCaption withCompletionBlock:(shareCompletion)completionBlock
{
__block NSString *strLinkBlock = strLink;
[FBSession openActiveSessionWithPublishPermissions: [NSArray arrayWithObjects: @"publish_stream", nil] defaultAudience: FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler: ^(FBSession *session,FBSessionState status,NSError *error)
{
if (error)
{
completionBlock(NO);
return;
NSLog(@"error");
}
else
{
[FBSession setActiveSession:session];
NSLog(@"LOGIN SUCCESS");
// Put together the dialog parameters
NSArray* actionLinks = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
@"Scisser",@"name",
@"http://m.facebook.com/apps/uniquenamespace/",@"link",
nil],
nil];
NSString *actionLinksStr = [actionLinks JSONRepresentation];
if (strLink.length == 0)
{
strLinkBlock = @"http://www.scisser.com";
}
NSString *strDescription = strDesc;
if (!strDescription)
{
strDescription = @"";
}
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
strName, @"name",
strCaption, @"caption",
strDescription, @"description",
strLinkBlock, @"link",
strPicLink, @"picture",
@"foo", @"ref",
actionLinksStr, @"actions",
nil];
// Make the request
[FBRequestConnection startWithGraphPath:@"/me/feed"
parameters:params
HTTPMethod:@"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error)
{
// Link posted successfully to Facebook
[self alertshowWithTitle:@"Congratulations" message:@"Post was successfully shared on your Facebook timeline."];
NSLog(@"%@",[NSString stringWithFormat:@"result: %@", result]);
completionBlock(YES);
return;
}
else
{
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(@"%@",[NSString stringWithFormat:@"%@", error.description]);
completionBlock(NO);
return;
}
}];
}
}];
}