/app/app_link_hosts 图 API 在 iOS 中对我不起作用

/app/app_link_hosts graph API not working for me in iOS

在发布之前我尝试了这个 link 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 文档对我来说真的缺乏细节。发生这种情况是否是因为任何权限问题?

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

所以我们有两个选择。

  1. 1.If 我们实施了应用程序 linking 这不适用于没有 fb 应用程序的人。

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

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

//获取App Token,是app token不是user 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 setHTTPMethod:POST_METHOD];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:CONTENT_TYPE];
    [request setHTTPBody: myRequestData ];

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

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

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

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

>  [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]]]; } }];

希望这对某些人有所帮助。