Question

I am using below code to post to Facebook. It is working perfectly but when i replace MY_URL with myapp://test_page// the post won't appear in Facebook timeline.

If i'm doing something wrong then please tell me how can i deep-link my app to Facebook app. I've searched almost all stackoverflow.com pages & other Facebook developer tutorials but i can not understand that.

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
[params setObject:@"Check out my post in myapp" forKey:@"message"];
[params setObject:MY_URL forKey:@"link"];
[params setObject:MY_IMAGE_URL forKey:@"picture"];
[params setObject:self.strQuestion forKey:@"name"];
[params setObject:@"" forKey:@"caption"];
[params setObject:self.strDescription forKey:@"description"];

[FBRequestConnection startWithGraphPath:@"me/feed" parameters:params HTTPMethod:@"POST"
 completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
     NSLog(@"FACEBOOK DONE");
 }];
Was it helpful?

Solution

Thanks for answer @Jai Govindani, but it was much simpler than your answer. I found out this from Facebook documentations.

First i changes App settings in Facebook account:

enter image description here

I added Bundle identifier in app's settings. And enabled Facebook deep linking And of course, i also needed "App store ID"

And wrote the below method with completionBlock -

- (void)shareOnFacebookWithName:(NSString *)strName withDescription:(NSString *)strDesc withLink:(NSString *)strLink WithPictureLink:(NSString *)strPicLink withCaption:(NSString *)strCaption withCompletionBlock:(shareCompletion)completionBlock
{
    __block NSString *strLinkBlock = strLink;

    [FBSession openActiveSessionWithPublishPermissions: [NSArray arrayWithObjects: @"publish_stream", nil] defaultAudience: FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler: ^(FBSession *session,FBSessionState status,NSError *error)
     {
         if (error)
         {
             completionBlock(NO);
             return;

             NSLog(@"error");
         }
         else
         {
             [FBSession setActiveSession:session];
             NSLog(@"LOGIN SUCCESS");

             // Put together the dialog parameters

             NSArray* actionLinks = [NSArray arrayWithObjects:
                                     [NSDictionary dictionaryWithObjectsAndKeys:
                                      @"Scisser",@"name",
                                      @"http://m.facebook.com/apps/uniquenamespace/",@"link",
                                      nil],
                                     nil];

             NSString *actionLinksStr = [actionLinks JSONRepresentation];

             if (strLink.length == 0)
             {
                 strLinkBlock = @"http://www.scisser.com";
             }

             NSString *strDescription = strDesc;
             if (!strDescription)
             {
                 strDescription = @"";
             }

             NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                            strName, @"name",
                                            strCaption, @"caption",
                                            strDescription, @"description",
                                            strLinkBlock, @"link",
                                            strPicLink, @"picture",
                                            @"foo", @"ref",
                                            actionLinksStr, @"actions",
                                            nil];

             // Make the request
             [FBRequestConnection startWithGraphPath:@"/me/feed"
                                          parameters:params
                                          HTTPMethod:@"POST"
                                   completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                       if (!error)
                                       {
                                           // Link posted successfully to Facebook

                                           [self alertshowWithTitle:@"Congratulations" message:@"Post was successfully shared on your Facebook timeline."];

                                           NSLog(@"%@",[NSString stringWithFormat:@"result: %@", result]);

                                           completionBlock(YES);
                                           return;
                                       }
                                       else
                                       {
                                           // An error occurred, we need to handle the error
                                           // See: https://developers.facebook.com/docs/ios/errors
                                           NSLog(@"%@",[NSString stringWithFormat:@"%@", error.description]);

                                           completionBlock(NO);
                                           return;
                                       }
                                   }];

         }
     }];
}

OTHER TIPS

In order to enable deep-linking with FB, you will need to register your iOS app ID/bundle identifier in your FB app's settings through the FB developer portal https://developers.facebook.com. Once that's done, you need to register a custom URL scheme for your app in Xcode in the Info.plist file. The custom URL scheme should be your FB app ID prefixed with fb - so if you FB app ID is 12345 your custom URL scheme would be fb12345. Once that's done and your app is up and submitted to the App Store, just use the regular http:// URL you would normally use for MY_URL - this URL will be passed as the target_url parameter. You can get this via your AppDelegate's

 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

method which will be called when your app launches with your custom URL scheme. Then you parse it with something like this:

NSRange targetURLRange = [urlAsString rangeOfString:@"target_url="];

if (targetURLRange.location != NSNotFound)
{
    NSString *FBTargetURLAsString = [urlAsString substringFromIndex:(targetURLRange.location + targetURLRange.length)];
    DDLogVerbose(@"After cutting I got: %@", FBTargetURLAsString);

    FBTargetURLAsString = [FBTargetURLAsString stringByReplacingPercentEscapesUsingEncoding:NSStringEncodingConversionAllowLossy];

    DDLogVerbose(@"After processing I got: %@", FBTargetURLAsString);

    NSArray *brokenDownURL = [FBTargetURLAsString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/?&="]];
    DDLogVerbose(@"Broken down URL: %@", brokenDownURL);
}

The actual parameters you inspect for are up to you. As an example, we use the path .../ogfb/object_type/object_id so for that I would use:

    NSString *objectType;
    NSString *objectID;

    for (NSString *currentURLComponent in brokenDownURL)
    {
        if ([currentURLComponent caseInsensitiveCompare:@"ogfb"] == 0)
        {
            NSInteger ogfbIndex = [brokenDownURL indexOfObject:currentURLComponent];

            if ([brokenDownURL count] > ogfbIndex + 2)
            {
                objectType = [brokenDownURL objectAtIndex:ogfbIndex + 1];
                objectID = [brokenDownURL objectAtIndex:ogfbIndex + 2];
            }

        }
    }

    DDLogVerbose(@"Got object type: %@ with ID: %@", objectType, objectID);

Then you do what you need to do with it, and should be good to go! Yes, this will require that you resubmit your app to the App Store (to register the new custom URL scheme). The Info.plist portion with the custom URL scheme looks like this in XML (below). The key path is URL Types (array) -> Item 0 -> URL Schemes (array) -> Item 0 -> fb<yourFBAppIDhere>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb(yourFBAppIDhere)</string>
        </array>
    </dict>
</array>
</plist>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top