老实说,我已经花了几个小时试图让它发挥作用。不幸的是, Facebook & App Link 的文档不够清晰。甚至来自 F8 的 App Links 视频。
应用要求:
- 将 FB 链接作为 Open Graph 故事分享,用户可以单击该链接直接进入我的应用程序并执行特定任务(我的应用程序需要从链接接收特定参数)
- 在没有 FB 登录的情况下共享到 FB 的链接(即通过共享对话框并切换到本机 iOS FB 应用程序,而不是使用 API 调用)。
目前进展:
我正在使用以下代码根据发布 iOS SDK 下的 FB 开发人员网站创建托管的应用程序链接(因为我只有移动内容) 。
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
@"{My app name}", @"name",
{custom URL}, @"al:iphone:url",
@"{app store ID}", @"al:iphone:app_store_id",
@"{My app name}", @"al:iphone:app_name",
@"{\"should_fallback\": false}", @"web",
fbAccessToken, @"access_token",
nil
];
/* make the API call */
[FBRequestConnection startWithGraphPath:@"/{FB app id}/app_link_hosts"
parameters:params
HTTPMethod:@"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
/* handle the result */
NSLog(@"Result = %@",result);
if(error) NSLog(@"error = %@",error);
}];
接下来我将 OG 故事发布到 FB(这帖子很好,但没有正确的 url)
// Create OG object
id<FBGraphObject> object =
[FBGraphObject openGraphObjectForPostWithType:@"{app name}:{FB object_name}"
title:@"Test Link"
image:@"https://cdn3.iconfinder.com/data/icons/picons-social/57/56-apple-512.png" // hosted wallpaper with unique id for background
url:nil // Assuming I need to put the url to the app link host object here??
description:@"Click to on this test link!"];
// Create an action
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
// Link the object to the action
[action setObject:object forKey:@"{FB object name}"];
// Check if the Facebook app is installed and we can present the share dialog
FBOpenGraphActionParams *params = [[FBOpenGraphActionParams alloc] init];
params.action = action;
params.actionType = @"{app name}:{FB action name}";
// If the Facebook app is installed and we can present the share dialog
if([FBDialogs canPresentShareDialogWithOpenGraphActionParams:params]) {
// Show the share dialog
[FBDialogs presentShareDialogWithOpenGraphAction:action
actionType:@"{app name}:{FB action name}"
previewPropertyName:@"{FB object name}"
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(@"Error publishing story: %@", error.description);
} else {
// Success
NSLog(@"result %@", results);
}
}];
}
为了在有人单击 FB OG 故事中的链接时处理传入 URL,我已根据 FB 文档将以下代码添加到 AppDelegate.m - 请参阅处理传入链接
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
BOOL urlWasHandled =
[FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
fallbackHandler:
^(FBAppCall *call) {
// Parse the incoming URL to look for a target_url parameter
NSString *query = [url query];
NSDictionary *params = [self parseURLParams:query];
// Check if target URL exists
NSString *appLinkDataString = [params valueForKey:@"al_applink_data"];
if (appLinkDataString) {
NSError *error = nil;
NSDictionary *applinkData =
[NSJSONSerialization JSONObjectWithData:[appLinkDataString dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:&error];
if (!error &&
[applinkData isKindOfClass:[NSDictionary class]] &&
applinkData[@"target_url"]) {
NSString *targetURLString = applinkData[@"target_url"];
// Show the incoming link in an alert
// Your code to direct the user to the
// appropriate flow within your app goes here
[[[UIAlertView alloc] initWithTitle:@"Received link:"
message:targetURLString
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
}];
return urlWasHandled;
}
// A function for parsing URL parameters
- (NSDictionary*)parseURLParams:(NSString *)query {
NSArray *pairs = [query componentsSeparatedByString:@"&"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:@"="];
NSString *val = [[kv objectAtIndex:1]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[params setObject:val forKey:[kv objectAtIndex:0]];
}
return params;
}
有没有人能够得到这个工作?我仍然不清楚托管的 App Link 是如何工作的以及将它放在哪里(我假设它应该在调用 FBGraphObject openGraphObjectForPostWithType 方法时进入'url'参数。
我真的不想创建一个网站来存储所有 url 并添加 App Link 元标记(我必须通过应用程序完成所有这些操作,因为每个 App Link 对于生成它的每个用户来说都是动态且唯一的来自应用程序中的)。
请帮忙!