'How can I make UIAlertController appear over the top of anything and everything?
I am developing an app using Visual Studio 2022 that uses ZXing.Net.Mobile,Forms to scan barcodes, everything is working as expected except I am unable to display the UIAlertController message over the top of everything. My code is:
btnScan.TouchUpInside += (object sender, EventArgs e) =>
{
// First we must get permission to use the camera
AVAuthorizationStatus authStatus = AVCaptureDevice.GetAuthorizationStatus(AVAuthorizationMediaType.Video);
if (authStatus == AVAuthorizationStatus.Authorized)
{
scan();
}
else if (authStatus == AVAuthorizationStatus.NotDetermined)
{
Utils.ShowMessage(this,"Camera access not determined. Ask for permission.", "Error");
AVCaptureDevice.RequestAccessForMediaType(AVAuthorizationMediaType.Video, (granted) =>
{
if (granted)
{
scan();
}
else
{
camDenied();
}
});
}
else if (authStatus == AVAuthorizationStatus.Restricted)
{
Utils.ShowMessage(this, "This device doesn't have the camera feature", "Error");
}
else
{
// Denied
camDenied();
}
};
}
void HandleScanResult(ZXing.Result result)
{
var msg = "";
if (result != null && !string.IsNullOrEmpty(result.Text))
msg = "Found Barcode: " + result.Text;
else
msg = "Scanning Canceled!";
// I need this alert to appear over the top of everything
InvokeOnMainThread(() =>
{
Utils.ShowMessage(this, msg, "Scan Success");
});
}
void camDenied()
{
UIAlertController alert = UIAlertController.Create("Warning", "It looks like your privacy settings are preventing us from accessing your camera. You can fix this by doing the following...", UIAlertControllerStyle.Alert);
UIAlertAction goAction = UIAlertAction.Create("Go", UIAlertActionStyle.Default, (action) =>
{
// Open Settings
UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
});
alert.AddAction(goAction);
PresentViewController(alert, true, null);
}
void scan ()
{
InvokeOnMainThread(() => {
scanner = new MobileBarcodeScanner(this.NavigationController);
scanner.UseCustomOverlay = false;
var opt = new MobileBarcodeScanningOptions() { DelayBetweenContinuousScans = 3000 };
scanner.TopText = "Hold camera up to barcode to scan";
scanner.BottomText = "Barcode will automatically scan";
//Start scanning
scanner.ScanContinuously(opt, false, HandleScanResult);
});
}
The UIAlertController code executes without an error but no alert appears. Is there a way to force the alert to appear over the top of everything?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
