How to check in Swift if the user gave permission to access the photolibrary with UIImagePicker?
You can use the below method to check the permission status for the gallery.
and if the permission is denied then also it navigates you to the app setting screen.
func checkGalleryPermission()
{
let authStatus = PHPhotoLibrary.authorizationStatus()
switch authStatus
{
case .denied : print("denied status")
let alert = UIAlertController(title: "Error", message: "Photo library status is denied", preferredStyle: .alert)
let cancelaction = UIAlertAction(title: "Cancel", style: .default)
let settingaction = UIAlertAction(title: "Setting", style: UIAlertAction.Style.default) { UIAlertAction in
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url, options: [:], completionHandler: { _ in })
}
}
alert.addAction(cancelaction)
alert.addAction(settingaction)
Viewcontoller.present(alert, animated: true, completion: nil)
break
case .authorized : print("success")
//open gallery
break
case .restricted : print("user dont allowed")
break
case .notDetermined : PHPhotoLibrary.requestAuthorization({ (newStatus) in
if (newStatus == PHAuthorizationStatus.authorized) {
print("permission granted")
//open gallery
}
else {
print("permission not granted")
}
})
break
case .limited:
print("limited")
@unknown default:
break
}
}
how to know whether the app currently has the 'All Files Access' permission or not. Android 11
declare this permission in your manifest.xml
:
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
for asking MANAGE_EXTERNAL_STORAGE
:
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
startActivityForResult(intent, 2296);
} catch (Exception e) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivityForResult(intent, 2296);
}
get your result in onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2296) {
if (SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
// perform action when allow permission success
} else {
Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
}
}
}
}
so, basically you can check permission is granted or not in Android11 like this;
if (SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
// Permission Granted
}
}
How to check if Allow management of all files is allowed?
Environment.isExternalStorageManager().
How to properly check if user has enabled Allow Full Access to custom keyboard extension
this is what I use in my code in swift
var hasAccess: Bool {
get{
if #available(iOSApplicationExtension 11.0, *) {
return self.hasFullAccess
} else {
return UIDevice.current.identifierForVendor != nil
}
}
}
How to check if permission is granted by user at runtime on Android?
Use onRequestPermissionResult, It handles the action if user press ALLOW and DENY, Just call the intent in the condition "if the user presses allow":
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 123: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//If user presses allow
Toast.makeText(Main2Activity.this, "Permission granted!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num.getText().toString()));
startActivity(in);
} else {
//If user presses deny
Toast.makeText(Main2Activity.this, "Permission denied", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
Hope this helps.
Related Topics
Why am I Getting Com.Facebook.Sdk.Login Error 308
Navigation with Only Back Button and Transparent Background
How to Fill a Circle Color by Percentage Value
-[Uithreadsafenode Canperformaction:Withsender:]: Unrecognized Selector Sent to Instance
Converting an Existing Project into Customizable Framework
How to Detect Which Annotation Was Selected in Mapview
How to Remove the Black Border of a Textfield
Exc_Bad_Access in Parent Class Init() with Xcode 10.2
Swift: Enums That Use Closures
How to Get the Original Filename of a Sprite/Texture in Swift
Auto Focus and Auto Exposure in Avfoundation on Custom Camera Layer
Saving Contact Address to Unified Contact Results in (Cnerrordomain Error 500)
iOS Healthkit How to Save Heart Rate (Bpm) Values? Swift
How to Convert This Opengl Pointer Math to Swift
How to Get the Today's and Tomorrow's Date in Swift 4