Using App Links Hosting API for link shared on Facebook from iOS app

前端 未结 3 1869
梦如初夏
梦如初夏 2021-02-03 13:13

I\'ve honestly spent hours on this trying to get it to work. Unfortunately Facebook & App Link\'s documentation is not clear enough. Even the App Links video from F8.

<
3条回答
  •  孤城傲影
    2021-02-03 14:07

    This is working for Graph v2.5 and SDK 4.8. Definitely not easy and not documented. But thanks Tim For inspiring. I'm using a custom graph object, I don't know how it works with default ones. I'm also using App Links Host since I don't have web app. I obtained a the {app_access_token} from Graph API Explorer, select your app then get app access token

    First I created graph stories, actions and object types on Facebook developer (your facebook app). Make sure you have an app query scheme for your app in info.plist LSApplicationQueriesSchemes should contain a scheme pointing to your app I will refer to it as {app_scheme}.

    Then in my iOS app I create a new link for every share.

    - (void)createAppLinkHost:(void(^)(NSString* appLinkURL))success error:(void(^)(NSError* error))failure{
    
    NSString *url = [NSString stringWithFormat:@"{app_scheme}://%li",self.object.identifier];
    NSString *appAccessToken = {app_access_token};
    NSString *iosLink = [NSString stringWithFormat:@"[{\"url\":\"%@\",\"app_name\":\"%@\",\"app_store_id\":%i},]",url,@"{app_name}",{app_store_id_int}];
    NSDictionary *appLinkHostParams = @{@"access_token":appAccessToken,
                                        @"name":@"{link name}",
                                        @"web":@"{\"should_fallback\": false}",
                                        @"ios":iosLink
                                        };
    
    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]initWithGraphPath:@"/{fb_appId}/app_link_hosts"
                                                                  parameters:appLinkHostParams
                                                                 tokenString:appAccessToken
                                                                     version:@"v2.5"
                                                                  HTTPMethod:@"POST"];
    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
        if (!error) {
            NSString *appLinkHostId = [result objectForKey:@"id"];
            NSString *urlString = [NSString stringWithFormat:@"https://fb.me/%@", appLinkHostId];
            success(urlString);
        }
        else{
            NSLog(@"--ERROR-- [FACEBOOK APP LINK HOST] %@",error);
            failure(error);
        }
    }];
    

    }

    If error, check it. Errors from App Link host make more sense than the rest of Facebook SDK. Graph API Explorer is useful to know what data you should send.

    Once you get a success get that AppLinkHost and share it.

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fbauth2://"]]){
    [self createAppLinkHost:^(NSString *appLinkURL) {
        SURL *imageURL = [NSURL URLWithString:self.activity.imageURL];;
        FBSDKSharePhoto *photo = [FBSDKSharePhoto photoWithImageURL:imageURL userGenerated:NO];
        //Custom objects needs to be set from facebook first.
        //Set object properties
        NSDictionary *properties = @{
                                     @"og:type": @"{namespace}:{graph_object}",
                                     @"og:title": @"title",
                                     @"og:description": @"body",
                                     @"og:image": @[photo],
                                     @"og:url":appLinkURL,
                                     @"fb:app_id":@"{fb_appId}",
                                     @"{namespace}:{custom_property}":@(self.object.identifier), //optional
                                     };            
        //Create GraphObject
        FBSDKShareOpenGraphObject *object = [FBSDKShareOpenGraphObject objectWithProperties:properties];
    
        //Create Action
        FBSDKShareOpenGraphAction *action = [FBSDKShareOpenGraphAction actionWithType:@"{namespace}:{graph_action}" object:object key:@"{namespace}:{graph_object}"];
    
        FBSDKShareOpenGraphContent *openGraphcontent = [[FBSDKShareOpenGraphContent alloc] init];
        openGraphcontent.action = action;
        openGraphcontent.previewPropertyName = @"{namespace}:{graph_object}";
    
        [FBSDKShareDialog showFromViewController:self withContent:openGraphcontent delegate:self];
    } error:^(NSError *error) {}];
    

    }

提交回复
热议问题