--- description: How to integrate with a Xamarin app --- # Xamarin SDK [![LLM | View as markdown](https://img.shields.io/badge/LLM-View%20as%20markdown-blue)](https://raw.githubusercontent.com/authgear/docs/refs/heads/main/get-started/native-mobile-app/xamarin.md) This guide provides instructions on integrating Authgear with a Xamarin app. Supported packages include: * Xamarin.Essentials 1.7.2 or higher * Xamarin.Forms 5.0.0.2401 or higher ## Setup Application in Authgear Signup for an Authgear Portal account in [https://portal.authgear.com/](https://portal.authgear.com/). Or you can use your self-deployed Authgear. From the Project listing, create a new Project or select an existing Project. After that, we will need to create an application in the project. **Step 1: Create an application in the Portal** 1. Go to **Applications** on the left menu bar. 2. Click **⊕Add Application** in the top tool bar. 3. Input the name of your application and select **Native App** as the application type. Click "Save". 4. You will see a list of guides that can help you for setting up, then click "Next".
**Step 2: Configure the application** 1. In your IDE (e.g. Visual Studio), define a custom URI scheme that the users will be redirected back to your app after they have authenticated with Authgear, e.g. `com.myapp.example://host/path`.\[^1] 2. Head back to Authgear Portal, fill in the Redirect URI that you have defined in the previous steps. 3. Click "Save" in the top tool bar and keep the **Client ID**. You can also obtain it again from the Applications list later.
## Create a Xamarin app * Open Visual Studio * Create a new project * Choose the Xamarin.Forms template ## Install the SDK * Search for "Authgear.Xamarin" on nuget.org and add it to your base project.\ [https://www.nuget.org/packages/Authgear.Xamarin/](https://www.nuget.org/packages/Authgear.Xamarin/) * Authgear.Xamarin targets MonoAndroid 12.0 on Android, and Xamarin.iOS10 on iOS. Update the target framework of the Android and iOS projects to match Authgear.Xamarin's target frameworks. * Update Android and iOS project's Xamarin.Essentials to 1.7.2. ## Platform Integration To finish the integration, setup the app to handle the redirectURI specified in the application. This part requires platform specific integration. ### Android #### Define your own callback activity ```csharp using System; using Android.App; using Android.Content.PM; using Android.Runtime; using Android.OS; using Authgear.Xamarin; using Xamarin.Forms; namespace MyApp.Droid { [Activity(NoHistory = true, LaunchMode = LaunchMode.SingleTop, Exported = true)] [IntentFilter(new[] { Android.Content.Intent.ActionView }, Categories = new[] { Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable }, DataScheme = "com.myapp.example")] public class WebAuthenticationCallbackActivity : Xamarin.Essentials.WebAuthenticatorCallbackActivity { } } ``` #### Targeting API level 30 or above (Android 11 or above) If your Android app is targeting API level 30 or above (Android 11 or above), you need to add a `queries` section to `AndroidManifest.xml`. ```xml ``` #### Initialize a global AuthgearSdk instance In your MainActivity.cs ```csharp using System; using Android.App; using Android.Content.PM; using Android.Runtime; using Android.OS; using Xamarin.Forms; using Authgear.Xamarin; namespace MyApp.Droid { public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { // ... var authgear = new AuthgearSdk(this, new AuthgearOptions { ClientId = CLIENT_ID, AuthgearEndpoint = ENDPOINT }); DependencyService.RegisterSingleton(authgear); LoadApplication(new App()); // ... } // other methods are omitted for brevity. } } ``` ### iOS #### Add the following key-value pair in your iOS project Info.plist ```xml CFBundleURLTypes CFBundleURLName com.myapp.example://host/path CFBundleURLSchemes com.myapp.example CFBundleTypeRole Editor ``` #### Initialize a global AuthgearSdk instance In your AppDelegate.cs ```csharp using Xamarin.Essentials; using Xamarin.Forms; using Authgear.Xamarin; namespace MyApp.iOS { [Register("AppDelegate")] public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate { public override bool FinishedLaunching(UIApplication app, NSDictionary options) { global::Xamarin.Forms.Forms.Init(); var authgear = new AuthgearSdk(app, new AuthgearOptions { ClientId = CLIENT_ID, AuthgearEndpoint = ENDPOINT }); DependencyService.RegisterSingleton(authgear); LoadApplication(new App()); return base.FinishedLaunching(app, options); } public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options) { return Xamarin.Essentials.Platform.OpenUrl(app, url, options); } public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler) { if (Xamarin.Essentials.Platform.ContinueUserActivity(application, userActivity, completionHandler)) return true; return base.ContinueUserActivity(application, userActivity, completionHandler); } } } ``` ## Try authenticate ### Edit your MainPage.xaml ```xml