Search code examples
androidiosxamarin.formsqr-code

how to register app to handle QR code scanned from mobile camera in xamarin forms


I am trying to make a feature where if user scans a QR code from mobile camera or any other QR code scanning app it should show user that they have the app installed and if they want to open the app.

Any kind of help is appreciated. Thank you.


Solution

  • Android:

    Add below line above the MainActivity.cs

    [IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    DataScheme = "https", DataHost = "YourDomainName", AutoVerify = true, Icon = "@drawable/icon")]
    

    OnCreate

    var url = Intent?.Data?.ToString();
    

    and pass it on load application

    iOS:

    Enable Associated domain for identifier on apple developer account Regenerate development and distribution certificate if needed. Download and install

    Create file called apple-app-site-association and add it to root of your domain

    in apple-app-site-association:

     {
           "applinks": {
             "apps": [],
             "details": [
               {
                 "appID": "YourAppId.YourBundleIdentifier",
                 "paths": [ "*"]
               }
             ]
           }
    }
    

    in Entitlements.plist: Enable Associated domain and add "applinks:domainName"

    in appdelegate:

     public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
        {
            if (userActivity.ActivityType == "NSUserActivityTypeBrowsingWeb")
            {
                var url = userActivity.WebPageUrl.AbsoluteString;
                ((App)Xamarin.Forms.Application.Current).AppLinkRequest(url);
                return true;
            }
            return base.ContinueUserActivity(application, userActivity, completionHandler);
        }
    
        public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
        {
            if (url.AbsoluteString != null)
            {
                ((App)Xamarin.Forms.Application.Current).AppLinkRequest(url.AbsoluteString);
                return true;
            }
            return base.OpenUrl(app, url, options);
        }
    

    App.xaml.cs

    in ctor

    if (QRcodeURL != null)
    {
    // Android Implementation for handling QR code scanning
    // Handle code
    }
    

    method

    public void AppLinkRequest(string url)
    {
    // iOS Implementation for handling QR code scanning
    // Handle code
    }