Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add iOSOpenSettinfsOnDeny option to requestAuthorization to make opening the settings on subsequent calls after the user denied permission optional #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ cordova.plugins.photoLibrary.getLibrary(
requestAuthorization is cross-platform method, that works in following way:

- On android, will ask user to allow access to storage
- On ios, on first call will open permission prompt. If user denies it subsequent calls will open setting page of your app, where user should enable access to Photos.
- On ios, on first call will open permission prompt. If user denies it and `iOSOpenSettingsOnDeny` is set to `true` (default) subsequent calls will open setting page of your app, where user should enable access to Photos.

```js
cordova.plugins.photoLibrary.requestAuthorization(
Expand All @@ -142,7 +142,8 @@ cordova.plugins.photoLibrary.requestAuthorization(
}, // if options not provided, defaults to {read: true}.
{
read: true,
write: true
write: true,
iOSOpenSettingsOnDeny: true
}
);
```
Expand Down
4 changes: 3 additions & 1 deletion src/ios/PhotoLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,15 @@ import Foundation

let service = PhotoLibraryService.instance

let options = command.arguments[0] as! NSDictionary
let openSettingsOnDeny = options["iOSOpenSettingsOnDeny"] as! Bool
service.requestAuthorization({
let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId )
}, failure: { (err) in
let pluginResult = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: err)
self.commandDelegate!.send(pluginResult, callbackId: command.callbackId )
})
}, openSettingsOnDeny: openSettingsOnDeny )

}

Expand Down
20 changes: 12 additions & 8 deletions src/ios/PhotoLibraryService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ final class PhotoLibraryService {

}

func requestAuthorization(_ success: @escaping () -> Void, failure: @escaping (_ err: String) -> Void ) {
func requestAuthorization(_ success: @escaping () -> Void, failure: @escaping (_ err: String) -> Void, openSettingsOnDeny: Bool ) {

let status = PHPhotoLibrary.authorizationStatus()

Expand All @@ -583,14 +583,18 @@ final class PhotoLibraryService {
return
}

// Permission was manually denied by user, open settings screen
let settingsUrl = URL(string: UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
UIApplication.shared.openURL(url)
// TODO: run callback only when return ?
// Do not call success, as the app will be restarted when user changes permission
if (openSettingsOnDeny) {
// Permission was manually denied by user, open settings screen
let settingsUrl = URL(string: UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
UIApplication.shared.openURL(url)
// TODO: run callback only when return ?
// Do not call success, as the app will be restarted when user changes permission
} else {
failure("could not open settings url")
}
} else {
failure("could not open settings url")
failure("user denied photo access before")
}

}
Expand Down
3 changes: 2 additions & 1 deletion www/PhotoLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,9 @@ var getRequestAuthenticationOptionsWithDefaults = function (options) {
}

options = {
read: options.read || true,
read: options.read === undefined ? true : options.read,
write: options.write || false,
iOSOpenSettingsOnDeny: options.iOSOpenSettingsOnDeny === undefined ? true : options.iOSOpenSettingsOnDeny,
};

return options;
Expand Down