• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios -/app/app_link_hosts 图形 API 在 iOS 中不适合我

[复制链接]
菜鸟教程小白 发表于 2022-12-12 14:28:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在发布之前,我尝试了这个链接 Using App Links Hosting API for link shared on Facebook from iOS app 没有任何成功。这是我的代码

// I have declared my FBSession object here in app delegate.
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
//Create dict params
NSDictionary *paramsForAppLinksHost = [NSDictionary dictionaryWithObjectsAndKeys:
                                       @"appName", @"name",
                                       @"myScheme://", @"al:ios:url",
                                       @"appStoreId", @"al:ios:app_store_id",
                                       @"appName", @"al:ios:app_name",
                                       @"{should_fallback:false}", @"web",
                                       nil
                                       ];


//Call graph API
FBRequest *request = [[FBRequest alloc] initWithSession:appDelegate.mpFacebookSession
                                              graphPath"/app/app_link_hosts"
                                             parameters: paramsForAppLinksHost
                                             HTTPMethod"GET"];

FBRequestHandler handler =
^(FBRequestConnection *connection, id result, NSError *error)
{
    // output the results of the request
    [self postUrlCompleted:connection result:result error:error];
};
[newConnection addRequest:request completionHandler:handler];
[newConnection start];

结果:打印结果说明: { 数据 = ( ); }

错误:无。

我在这里做错了什么?对我来说,这个 Facebook 文档确实缺乏细节。发生这种情况是因为任何权限问题吗?



Best Answer-推荐答案


首先我想说Facebook doc真的很烂,它从来没有真正详细说明过任何事情,在做了所有这些之后我才知道Mobile Safari不支持应用链接 . 这意味着如果没有 Facebook 应用程序,这将不起作用

所以我们有两个选择。

  1. 1.如果我们实现应用链接,这将不适用于没有 fb 应用的人。

  2. 如果我们使用 URL 并分享到 FB 并单击它重定向并使用服务器端的某些架构来启动您的应用程序,如果您安装了 Facebook 应用程序,这也不起作用,因为新的 FB 应用程序有自己的浏览器,似乎不支持使用 document.location() 进行重定向。同样在 iOS 8.0 中使用 document.location () 重定向(将使用模式)由于某种原因无法正常工作。所以基本上我被迫使用应用链接

来到我的问题,这是由于参数不匹配但API调用没有说明它,我使用以下代码获取App链接ID。由于我的问题中的链接在那个答案中调用它不起作用,所以我这样使用。希望它能节省一些人的时间。

//获取应用 token ,是应用 token 不是用户 token

> NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];   
> NSString* pFaceBookAppID = [infoDict objectForKey"FacebookAppID"];  
> NSString *pStr = [NSString   
> stringWithFormat"https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=%@&client_secret={app_secret_key}",pFaceBookAppID];
> NSURL* pWebUrl = [NSURL URLWithString:pStr]; NSString *fullToken =   
> [NSString stringWithContentsOfURL:pWebUrl   
> encoding:NSUTF8StringEncoding error:nil]; NSArray *components =   
> [fullToken componentsSeparatedByString"="]; self.mpFBAppToken =   
> [components objectAtIndex:1];

//现在可以这样调用获取app ID了,

    NSArray *pArray = @[@{@"url":{URL_SCHEMA},@"app_store_id":{app_store_id},@"app_name":{{app_name}}];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:pArray options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSDictionary *pDict = @{@"should_fallback" "false"};
    jsonData = [NSJSONSerialization dataWithJSONObject:pDict options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonWebString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    pRequestString = [NSString stringWithFormat"access_token=%@&name={app_name}&ios=%@&web=%@",self.mpFBAppToken,jsonString,jsonWebString];

    NSURL* pWebUrl = [NSURL URLWithString"https://graph.facebook.com/app/app_link_hosts"];
    NSData *myRequestData = [NSData dataWithBytes:[pRequestString UTF8String] length:[pRequestString length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:pWebUrl];
    [request setHTTPMethodOST_METHOD];
    [request setValue"application/x-www-form-urlencoded" forHTTPHeaderField:CONTENT_TYPE];
    [request setHTTPBody: myRequestData ];

    mpConnection = [[NSURLConnection alloc]initWithRequest:request delegate:self] ;

//现在你必须实现连接委托(delegate)函数来获取结果,它将包含应用程序链接 ID,使用它来获取 URL,如下所示。 获取网址..

    NSDictionary *pParams = @{@"access_token":self.mpFBAppToken};
    [FBRequestConnection startWithGraphPath:self.mpAPPLinkID
                                 parameters:pParams
                                 HTTPMethod"GET"
                          completionHandler:^(
                                              FBRequestConnection *connection,
                                              id result,
                                              NSError *error
                                              ){
                                                         }];

//现在在 URL 中只需附加您想要作为查询字符串传递的参数,这样您就可以像这样从应用程序委托(delegate)的 openURL 方法轻松解析它...

>  [FBAppCall handleOpenURL:url
>                              sourceApplication:sourceApplication
>                                fallbackHandler:^(FBAppCall *call)
>                                {
>                                    // Retrieve the link associated with the post
>                                    NSURL *targetURL = [[call appLinkData] targetURL];
>                                   if([[targetURL query]length] > 0)
>                                   {
//Parse Query string by some method;                         
>                                       NSMutableDictionary *pDict = [[NSMutableDictionary alloc]initWithDictionary:[self
> parseURLParams:[targetURL query]]]; } }];

希望这对某人有所帮助。

关于ios -/app/app_link_hosts 图形 API 在 iOS 中不适合我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28827537/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap