0

I am looking at how to implement a Facebook invite dialog and noticed that Facebook now offers a new FB App Invite product. Is there a way to use the new App Invite product with Xamarin iOS?

Thanks!

doorman
  • 15,707
  • 22
  • 80
  • 145

3 Answers3

5

Here is the implemention of app invites

You need to create Native implementaion for appinvites

Just create one interface called IFacebookService

Define Method defination as shown in example

public interface IFacebookservice{
  InviteFriends (string appLinkURL, string previewImageURL);
}

and do that implementation on platform side as shown in below

IOS FacebookService.CS

public void InviteFriends (string appLinkURL, string previewImageURL)
{
            var fromController = UIApplication.SharedApplication.KeyWindow.RootViewController;
            var content = new AppInviteContent {
                AppLinkURL = new NSUrl (appLinkURL),
                PreviewImageURL = new NSUrl (previewImageURL)
            };
            AppInviteDialog.Show (fromController, content, null);
}

Android FacebookService.Cs

  public void InviteFriends(string appLinkURL, string previewImageURL)
    {
        if (AppInviteDialog.CanShow())
        {
            var activity = Xamarin.Forms.Forms.Context as Activity;
           var content =new AppInviteContent.Builder().SetApplinkUrl(appLinkURL).SetPreviewImageUrl(previewImageURL).Build() as AppInviteContent;
            AppInviteDialog.Show(activity, content);
        }
    }

Usage in Xamarin Forms

  var facebookservice=Dependency.Get<IFacebookservice>()
    button.click+=delegate{facebookservice.InviteFriends("appurl","previewimageurl")}
Jagadeesh Govindaraj
  • 6,977
  • 6
  • 32
  • 52
  • I followed the above implentation and its work fine with read droid device and iOS Simulator. But on real iPhone it doesn't show Invite dialog. No error, no exception, nothing. any though on this? – SoftSan Feb 20 '17 at 16:51
  • Do you guys get this to show the native dialog or the web dialog? We are using the above implementation but still get the web browser version – Magnus Ahlin Mar 06 '17 at 19:23
  • @https://stackoverflow.com/users/2086778/softsan Try to implement the error handler from my post below – Geert Jan Oct 17 '17 at 20:30
2

It's currently not supported by the Xamarin FB SDK. The invite feature is available from version 4.0, where the transformed version of FB SDK was done on version 3.6.

So you basically have two options:

1) Ask Xamarin dev team to update the FB sdk to version 4.0, you can check it here

2) Since it will probably won't be ready soon, you can take the FB iOS native sdk, and generate a xamarin library by yourself. More details can be found in this link

If you do decide to go on option #2, it would be great if you would share your transformed sdk.

IdoT
  • 2,831
  • 1
  • 24
  • 35
1

The answer from Jagadeesh is completely correct. Also consider implementing a result handler when returning from the Facebook multi-friend-selector. If at least one friend was invited, the Facebook API feedback will show it. For iOS the result can be 'canceled by the user' or completed

  • {completionGesture = cancel; didComplete = 1;}
  • {didComplete = 1;}

For Android nothing when 'canceled by the user', but if completed:

  • Bundle[{didComplete=1}]

Android

        AppInviteDialog AppInv = new AppInviteDialog(activity);
        AppInv.RegisterCallback(callbackManager, invitecallback);
        AppInv.Show(content);
        var invitecallback = new FacebookCallback<AppInviteDialog.Result>()
        {
            HandleSuccess = Result =>
            {
                if ((string)Result.Data == "Bundle[{didComplete=1}]")
                {
                    textbox.Text = "Friend Invited";
                }
            },
            HandleCancel = () =>
            {
                textbox.Text = "Cancelled";
            },
            HandleError = Error =>
            {
                textbox.Text = "Error" + Error.Message;
            }
        };

iOS

    var dlgt = new MyAppInviteDialogDelegate();
    var dialog = AppInviteDialog.Show(fromController, content, dlgt);
    public class MyAppInviteDialogDelegate : AppInviteDialogDelegate
    {

        public override void DidComplete(AppInviteDialog appInviteDialog, NSDictionary results)
        {
            textbox.Text = "Friend Invited";
            if (results.Description.Contains("cancel")) 
            {
                textbox.Text = "Cancelled";
            }
        }

        public override void DidFail(AppInviteDialog appInviteDialog, NSError error)
        {
            textbox.Text = "Error" + error.Description;
        }
    }
Geert Jan
  • 408
  • 1
  • 6
  • 22