How i can use permissionplugin with zxing nuget pkg in xamarin.forms app?

DevMaranDevMaran Member ✭✭

As per the documentation both need to override the "OnRequestPermissionsResult" method.

// OnRequestPermissionsResult mentioned in permissionplugin pkg by "jamesmontemagno"

 public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }

// OnRequestPermissionsResult mentioned in Zxing pkg

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
            {

                global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            }



I am trying to achieve it using below code.

 protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);
            Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState);

            ZXing.Net.Mobile.Forms.Android.Platform.Init();

            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }


        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            //zxing returning request code: 101
            PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        }

Please give your valuable suggestions.

Answers

  • Amit_Malhi01Amit_Malhi01 Member ✭✭

    It will work as :

    Add following to an activity that runs before initializing the scanner page.

    protected async override void OnCreate(Bundle savedInstanceState)
    {
    base.OnCreate(savedInstanceState);
    ZXing.Net.Mobile.Forms.Android.Platform.Init();
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
    global::ZXing.Net.Mobile.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions,
    grantResults);
    }

    Init Scanner Page:

    ZXingScannerPage scanner = new ZXingScannerPage
    {
    HorizontalOptions = LayoutOptions.FillAndExpand,
    VerticalOptions = LayoutOptions.FillAndExpand,
    AutomationId = "zxingScannerView",
    IsScanning = true,
    Options = new ZXing.Mobile.MobileBarcodeScanningOptions
    {
    UseFrontCameraIfAvailable = false,//update later to come from settings
    PossibleFormats = new List<ZXing.BarcodeFormat>(),
    TryHarder = true
    }
    };

    scanner.OnScanResult += Scanner_OnScanResult

    private async void Scanner_OnScanResult(ZXing.Result result)
    {
    var resultString = result != null ? result.ToString() : null;
    }

Sign In or Register to comment.