Facebook与iOS应用进行深度链接

我正在使用下面的代码发布到Facebook。 它工作正常,但是当我用myapp:// test_pagereplaceMY_URL时 ,post将不会出现在Facebook时间轴上。

如果我做错了,那么请告诉我如何深入链接我的应用程序Facebook应用程序。 我search了几乎所有的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"); }]; 

谢谢你的回答@Jay Govindani,但是这比你的回答简单得多。 我从Facebook文档中find了这个。

首先,我更改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; } }]; } }]; } 

为了启用与FB的深层链接,您需要通过FB开发人员门户https://developers.facebook.com在您的FB应用程序设置中注册您的iOS应用程序ID /软件包标识符。 完成之后,您需要在Info.plist文件中的Xcode中为您的应用程序注册自定义URLscheme。 自定义URLscheme应该是您的FB应用程序ID前缀fb – 所以如果您的FB应用程序ID是12345您的自定义URLscheme将是fb12345 。 一旦完成并且您的应用程序启动并提交到App Store,只需使用您通常用于MY_URL的常规http:// URL – 此URL将作为target_urlparameter passing。 你可以通过你的AppDelegate的

  - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 

方法将在您的应用程序与您的自定义URLscheme一起启动时被调用。 然后你用这样的东西parsing它:

 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); } 

您检查的实际参数取决于您。 作为一个例子,我们使用path.../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(注册新的自定义URLscheme)。 具有自定义URLscheme的Info.plist部分在XML中看起来像这样(下面)。 关键path是URL Types (数组) – > Item 0 – > URL Schemes (array) – > Item 0 – > 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>