应用要求:
>分享FB作为Open Graph故事的链接,用户可以点击该故事将其直接带入我的应用程序并执行特定任务(我的应用程序需要从链接接收特定参数)
>在没有FB登录的情况下将链接共享到FB(即通过共享对话框并切换到本机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(这是很好但没有正确的网址)
// Create OG objectID<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 actionID<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 dialogFBOpenGraphActionParams *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 dialogif([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’参数中.
我真的不想创建一个网站来存储所有的网址并添加App link元标记(我必须通过应用程序完成所有这些,因为每个App link都将是动态的,并且对于生成它的每个用户都是唯一的来自应用程序).
请帮忙!
解决方法@H_403_29@ 在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,@"{your app name}",@"{your app's custom url}",@"al:ios:url",@"al:ios:app_store_ID",@"al:ios:app_name",nil ]; [FBRequestConnection startWithGraPHPath:@"/{FB app ID}/app_link_hosts" parameters:paramsForApplinksHost httpMethod:@"POST" completionHandler:^( FBRequestConnection *connection,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,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.
总结以上是内存溢出为你收集整理的使用App Links Hosting API从iOS应用程序在Facebook上共享链接全部内容,希望文章能够帮你解决使用App Links Hosting API从iOS应用程序在Facebook上共享链接所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)