Skip to content

Repository files navigation

Plugin.FacebookKit

This is a wrapper library around the native Android and iOS Facebook SDKs which includes cross-platform APIs for Authentication and Sharing with Facebook Login and Facebook Sharing.

Supported Features

Feature Plugin Platforms
Core Plugin.FacebookKit.Core Android, iOS, MacCatalyst
Login Plugin.FacebookKit.Login Android, iOS, MacCatalyst
Share Plugin.FacebookKit.Share Android, iOS, MacCatalyst
[Plugin] Plugin.FacebookKit Android, iOS, MacCatalyst

Nuget Packages

Versions of these packages and android dependencies have been chosen to align transitively with dotnet maui versions. For instance, selecting version 10.0.80 will align android transitive depencies with the same ones used with .NET Maui Version 10.0.80. Transitive dependencies can always be locked by adding package references to the project.

Package Version
Plugin.FacebookKit NuGet
Plugin.FacebookKit.Core NuGet
Plugin.FacebookKit.Login NuGet
Plugin.FacebookKit.Share NuGet

Installing a package

The easiest way to install a package on Windows platforms is through Visual Studio Nuget package manager. InstallNuget

The easiest way to install a package on Mac platform is add it using the terminal. Windows users can also do this too using the dotnet command line interface. InstallNuget

dotnet package add Plugin.FacebookKit

Visual studio can resolve the dependency automatically by adding the package reference directly to the .csproj file with the following text

<ItemGroup>
  <PackageReference Include="Plugin.FacebookKit" Version="10.0.80" />
</ItemGroup>

Intro

This a custom cross platform implementation of binding packages that bind the native SDK's to C#. The information below shows developers the original SDK intent and how they were meant to be used. When reviewing app behavior, developers should always refer to the official Facebook docs. Follow the steps here to get started.

  1. Register with Meta as a developer.
  2. Create an app.
  3. (Optional) Select use cases such as authentication and access profile information. Customize.
  4. Implement facebook app project Id's, client tokens, and protocols into the AndroidManifest.xml and Info.plist files. See the sample/FacebookSample project or the following quickstart guides for additional setup details:

iOS Dependencies

Package Version
plugin.Facebook.iOS.AEM NuGet
plugin.Facebook.iOS.Core NuGet
plugin.Facebook.iOS.CoreBasics NuGet
plugin.Facebook.iOS.GamingServices NuGet
plugin.Facebook.iOS.Login NuGet
plugin.Facebook.iOS.Share NuGet

Android Dependencies

Package Version
plugin.Facebook.Android.AudienceNetwork NuGet
plugin.Facebook.Android NuGet
plugin.Facebook.Android.Applinks NuGet
plugin.Facebook.Android.Core NuGet
plugin.Facebook.Android.Common NuGet
plugin.Facebook.Android.GamingServices NuGet
plugin.Facebook.Android.Login NuGet
plugin.Facebook.Android.Messenger NuGet
plugin.Facebook.Android.Share NuGet

Project Configuration

Be sure to change the TargetFramework this project uses (net10.0-*)

If using the native Facebook Button Controls is desired, they can be loaded by calling ".UseFacebookControls" in the MauiProgram.cs like this:

using Plugin.FacebookKit;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        return MauiApp
            .CreateBuilder()
            .UseMauiApp<App>()
            ...
            .UseFacebookControls()
            .Build();
    }
}

If only using Login is desired, those individual controls can be loaded by calling ".UseFacebookLoginControls" in the MauiProgram.cs like this:

using Plugin.FacebookKit;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        return MauiApp
            .CreateBuilder()
            .UseMauiApp<App>()
            ...
            .UseFacebookLoginControls()
            .Build();
    }
}

The same applies for the Sharing component.

Windows specifics (Not Implemented)

Calling the implementation instance on Windows

NOTE: Windows is not implemented. This plugin does not crash production Apps when deploying to Windows but calls to the implementation on Windows will throw a NullReferenceException. When deploying to Windows, Calls to implementations should be null checked like this:

var dialog = FacebookPluginShare.Current?.CreateShareDialog();
var loginManager = FacebookPluginLogin.Current?.CreateLoginManager();

Android specifics

To receive callbacks from the native Facebook SDK, register the callback manager.

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent? data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        FacebookMainActivity.CallbackManager.OnActivityResult(requestCode, (int)resultCode, data);
    }

(Login Only) Call initialize in the MainActivity.cs by overriding OnCreate

    protected override void OnCreate(Bundle? savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        FacebookPluginLogin.Initialize();
    }

Ensure the ApplicationId in the .csproj file matches the bundle_id and package_name inside of the [Info.plist|AndroidManifest.xml] files:

<ApplicationId>com.example.myapp</ApplicationId>

Facebook requires adding the HashKey to the project App for testing purposes prior to publishing. To generate the HashKey, it can be computed on AppStart by implementing this in the MainActivity.cs by overriding OnCreate like this:

protected override void OnCreate(Bundle? savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    
    PrintFacebookKeyHash();
}

private void PrintFacebookKeyHash()
{
    try
    {
        var packageInfo = PackageManager!.GetPackageInfo(PackageName!, Android.Content.PM.PackageInfoFlags.SigningCertificates);
        var signingInfo = packageInfo.SigningInfo;

        if (signingInfo is null)
        {
            Android.Util.Log.Debug("Facebook", "SigningInfo is null.");
            Console.WriteLine("Facebook", "SigningInfo is null.");
            Debug.WriteLine("Facebook", "SigningInfo is null.");
            return;
        }

        var signatures = signingInfo.HasMultipleSigners ? signingInfo.GetApkContentsSigners() : signingInfo.GetSigningCertificateHistory();

        foreach (var signature in signatures!)
        {
            using var sha1 = MessageDigest.GetInstance("SHA");

            sha1.Update(signature.ToByteArray());

            var hash = Android.Util.Base64.EncodeToString(sha1.Digest()!, Android.Util.Base64Flags.NoWrap);

            Android.Util.Log.Debug("Facebook", $"Key Hash: {hash}");
            Console.WriteLine($"Facebook HashKey: {hash}");
            Debug.WriteLine($"Facebook HashKey: {hash}");
        }
    }
    catch (Exception ex)
    {
        Android.Util.Log.Error("Facebook", $"Failed to get key hash: {ex}");
        Console.WriteLine($"Facebook Failed to get key hash: {ex}");
        Debug.WriteLine($"Facebook Failed to get key hash: {ex}");
    }
}

iOS specifics

To intialize Facebook SDK and get callbacks from the native Facebook SDK, register the SDK's AppDelegate. If using Login, call initialize here.

using Foundation;
using UIKit;
using Plugin.FacebookKit.Core;

namespace ShareMauiSample;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
	protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

    public override bool FinishedLaunching(UIApplication application, NSDictionary? launchOptions)
    {
        FacebookPluginLogin.Initialize();
        FacebookApplicationDelegate.DidFinishLaunching(application, launchOptions);
        return base.FinishedLaunching(application, launchOptions);;
    }

    public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
    {
        return FacebookApplicationDelegate.OpenUrl(app, url, options);
    }
    
    public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
    {
        FacebookApplicationDelegate.Application(application, userActivity);
        return base.ContinueUserActivity(application, userActivity, completionHandler);
    }

    public override void OnActivated(UIApplication application)
    {
        FacebookApplicationDelegate.ActivateApp();
        base.OnActivated(application);
    }
}

Documentation and samples

See the docs for more details on feature implementation.

In the samples folder there are two sample projects, one for sharing and one for login. Though there is no combinded sample, if using both features, developers can simply use Plugin.FacebookKit.

To run the sample successfully, ensure to update the Info.plist, Entitlements.plist, String.xml, AndroidManifest.xml files to match the app project information in your facebook app project.

License

The license developers are required to accept is a standard MIT license located here: License.md

Release notes

Version 10.0.80

  • Initial release.

About

A cross platform implementation of Facebook SDKs

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages