我使用下面的代码发布到Facebook。它工作得很好但是当我用 myapp:// test_page // 替换 MY_URL 时,帖子将不会出现在Facebook时间轴中。
如果我做错了,请告诉我如何将我的应用程序深入链接到Facebook应用程序。我搜索了几乎所有的stackoverflow.com页面和其他Facebook开发人员的教程,但我无法理解。
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
[params setObject:@"Check out my post in myapp" forKey:@"message"];
[params setObject:MY_URL forKey:@"link"];
[params setObject:MY_IMAGE_URL forKey:@"picture"];
[params setObject:self.strQuestion forKey:@"name"];
[params setObject:@"" forKey:@"caption"];
[params setObject:self.strDescription forKey:@"description"];
[FBRequestConnection startWithGraphPath:@"me/feed" parameters:params HTTPMethod:@"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(@"FACEBOOK DONE");
}];
答案 0 :(得分:7)
感谢你回答@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;
}
}];
}
}];
}
答案 1 :(得分:3)
为了启用与FB的深层链接,您需要通过FB开发人员门户https://developers.facebook.com
在FB应用程序的设置中注册您的iOS应用程序ID /包标识符。完成后,您需要在Info.plist文件中的Xcode中为您的应用注册自定义URL方案。自定义网址方案应该是前缀为fb
的FB应用ID - 因此,如果您的FB应用ID为12345
,则您的自定义网址方案将为fb12345
。完成操作并且您的应用程序启动并提交到App Store后,只需使用您通常用于http://
的常规MY_URL
网址 - 此网址将作为target_url
参数传递。您可以通过AppDelegate的
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
使用自定义网址方案启动应用时调用的方法。然后你用这样的东西解析它:
NSRange targetURLRange = [urlAsString rangeOfString:@"target_url="];
if (targetURLRange.location != NSNotFound)
{
NSString *FBTargetURLAsString = [urlAsString substringFromIndex:(targetURLRange.location + targetURLRange.length)];
DDLogVerbose(@"After cutting I got: %@", FBTargetURLAsString);
FBTargetURLAsString = [FBTargetURLAsString stringByReplacingPercentEscapesUsingEncoding:NSStringEncodingConversionAllowLossy];
DDLogVerbose(@"After processing I got: %@", FBTargetURLAsString);
NSArray *brokenDownURL = [FBTargetURLAsString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/?&="]];
DDLogVerbose(@"Broken down URL: %@", brokenDownURL);
}
您检查的实际参数由您决定。例如,我们使用路径.../ogfb/object_type/object_id
,所以我会使用:
NSString *objectType;
NSString *objectID;
for (NSString *currentURLComponent in brokenDownURL)
{
if ([currentURLComponent caseInsensitiveCompare:@"ogfb"] == 0)
{
NSInteger ogfbIndex = [brokenDownURL indexOfObject:currentURLComponent];
if ([brokenDownURL count] > ogfbIndex + 2)
{
objectType = [brokenDownURL objectAtIndex:ogfbIndex + 1];
objectID = [brokenDownURL objectAtIndex:ogfbIndex + 2];
}
}
}
DDLogVerbose(@"Got object type: %@ with ID: %@", objectType, objectID);
然后你做你需要做的事情,应该好好去!是的,这将要求您将应用程序重新提交到App Store(以注册新的自定义URL方案)。带有自定义URL方案的Info.plist部分在XML中如下所示(如下所示)。关键路径是URL Types
(数组) - >项目0 - > URL Schemes
(数组) - &gt;项目0 - &gt; fb<yourFBAppIDhere>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb(yourFBAppIDhere)</string>
</array>
</dict>
</array>
</plist>