gpt4 book ai didi

ios - 将 App Links Hosting API 用于从 iOS 应用程序在 Facebook 上共享的链接

转载 作者:可可西里 更新时间:2023-11-01 03:18:41 24 4
gpt4 key购买 nike

老实说,我已经花了好几个小时试图让它发挥作用。遗憾Facebook & App Link's documentation不够清楚。即使是 App Links video from F8 .

应用要求:

  1. 将链接作为 Open Graph 故事分享到 FB,用户可以点击该链接将他们直接带入我的应用程序并执行特定任务(我的应用程序需要从链接接收特定参数)
  2. 在没有 FB 登录的情况下分享到 FB 的链接(即通过分享对话框并切换到 native iOS FB 应用程序,而不是使用 API 调用)。

到目前为止的进展:

我根据 FB developer's website under Publishing iOS SDK. 使用以下代码创建托管应用程序链接(因为我只有移动内容)

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 - 请参阅 Handling incoming links

- (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 对于每个用户来说都是动态且唯一的从应用程序中生成它)。

请帮忙!

最佳答案

在 FB 的 MingLi 的帮助下,我设法使用以下代码让它工作:

- (void)shareToOpenGraphCountdownInvite
{
NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id={insert your FB app ID here}&client_secret={insert client secret here}"];
NSString *fullToken = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSArray *components = [fullToken componentsSeparatedByString:@"="];
FBAppAccessToken = [components objectAtIndex:1];

NSDictionary *paramsForAppLinksHost = [NSDictionary dictionaryWithObjectsAndKeys:
FBAppAccessToken, @"access_token",
@"{your app name}", @"name",
@"{your app's custom url}", @"al:ios:url",
@"{app store ID}", @"al:ios:app_store_id",
@"{your app name}", @"al:ios:app_name",
@"{\"should_fallback\": false}", @"web",
nil
];

[FBRequestConnection startWithGraphPath:@"/{FB app ID}/app_link_hosts"
parameters:paramsForAppLinksHost
HTTPMethod:@"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
AppLinksHostURL_ID = [result objectForKey:@"id"]; // store this ID in an NSString
[self postOGStoryWithCustomURL];
if(error) NSLog(@"error = %@", error.description);

}];
}

- (void)postOGStoryWithCustomURL
{
NSString *urlString = [NSString stringWithFormat:@"https://fb.me/%@/%@", AppLinksHostURL_ID, customURL];

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self pathForS3ObjectWithFilename:previewImageFilename]]]];

// Create OG object
id<FBGraphObject> object =
[FBGraphObject openGraphObjectForPostWithType:@"timeflyz:countdown_invite"
title:eventBeingShared.eventName
image:image
url:urlString // fb.me app links hosted url here
description:@"{insert description here}"];

// Create an action
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];

// Link the object to the action
[action setObject:object forKey:@"countdown_invite"];

// Check if the Facebook app is installed and we can present the share dialog
FBOpenGraphActionParams *params = [[FBOpenGraphActionParams alloc] init];
params.action = action;
params.actionType = @"timeflyz:create";

// 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:@"timeflyz:create"
previewPropertyName:@"countdown_invite"
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 {
// NSLog(@"result %@", results);
if([[results objectForKey:@"completionGesture"] isEqualToString:@"post"]) {
NSLog(@"Posted successfully!");
[[NSNotificationCenter defaultCenter] postNotificationName:@"showShareSuccessfullMessage" object:self userInfo:nil];
} else
NSLog(@"Something else happened - user didn't post");
}
}];


}

请注意,“customURL”是一个遵循模式“?variable1=result1&variable2=result2...”的 NSString

关于ios - 将 App Links Hosting API 用于从 iOS 应用程序在 Facebook 上共享的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24156885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com