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.
| 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 |
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 | |
| Plugin.FacebookKit.Core | |
| Plugin.FacebookKit.Login | |
| Plugin.FacebookKit.Share |
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.FacebookKitVisual 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>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.
- Register with Meta as a developer.
- Create an app.
- (Optional) Select use cases such as authentication and access profile information. Customize.
- 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:
| Package | Version |
|---|---|
| plugin.Facebook.iOS.AEM | |
| plugin.Facebook.iOS.Core | |
| plugin.Facebook.iOS.CoreBasics | |
| plugin.Facebook.iOS.GamingServices | |
| plugin.Facebook.iOS.Login | |
| plugin.Facebook.iOS.Share |
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.
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(); protected override void OnActivityResult(int requestCode, Result resultCode, Intent? data)
{
base.OnActivityResult(requestCode, resultCode, data);
FacebookMainActivity.CallbackManager.OnActivityResult(requestCode, (int)resultCode, data);
} 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}");
}
}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);
}
}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.
The license developers are required to accept is a standard MIT license located here: License.md
- Initial release.