Reputation: 73
Trying to make a swift file with all kinds of permissions for iPhone including
So I tried searching, How to know if user has access to Camera?
and mostly getting functions with Switch statements
. What I am looking for is universal functions to call anywhere in project e:g after calling function elsewhere in project I want to able to perform if case .authorised
then do code A elseif
.denied then do code B else .notDetermined
do code C.
func checkPhotoLibraryPermission() {
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
print("Authorized")
case .denied, .restricted :
case .notDetermined:
PHPhotoLibrary.requestAuthorization() { status in
switch status {
case .authorized:
print("Authorized")
case .denied, .restricted:
print("No access")
case .notDetermined:
print("Not determined")
}
}
}
}
Upvotes: 4
Views: 7571
Reputation: 3337
You need a callback function as PHPhotoLibrary.requestAuthorization()
is an async operation.
func checkPhotoLibraryPermission(callback:(authorized:Bool)->Void) {
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
callback(true)
case .denied, .restricted :
callback(false)
case .notDetermined:
PHPhotoLibrary.requestAuthorization() { status in
switch status {
case .authorized:
callback(true)
case .denied, .restricted,.notDetermined:
callback(false)
}
}
}
}
Upvotes: 2
Reputation: 5081
Yes, you can do this with the help of blocks.
public typealias CompletionHandler = ((Bool)->Void)?
@objc func yourMethod(completionBlock : CompletionHandler){
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
completionBlock(true);
case .denied, .restricted :
completionBlock(false);
case .notDetermined:
PHPhotoLibrary.requestAuthorization() { status in
switch status {
case .authorized:
completionBlock(true);
case .denied, .restricted:
completionBlock(false);
case .notDetermined:
completionBlock(false);
}
}
}
}
This is just a sample you can write accordingly.
Upvotes: 2
Reputation: 804
func checkPhotoLibraryPermission() -> Bool {
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
print("Authorized")
return true
case .denied:
return true
case .restricted :
return false
case .notDetermined:
return false
}
}
}
something like this ?? if yes then it's great and if no then let me know what exactly you want so i can try to code switch case as per your requirement :)
Upvotes: 1
Reputation: 1165
Update your function with return type
func isPhotoLibraryPermission() -> bool {
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
print("Authorized")
return true
case .denied, .restricted :
return false
case .notDetermined:
PHPhotoLibrary.requestAuthorization() { status in
switch status {
case .authorized:
print("Authorized")
return true
case .denied, .restricted:
print("No access")
return false
case .notDetermined:
print("Not determined")
return false
}
}
}
}
PS: If you need something more generic, create enum with all cases you want to return and then return that :-)
Upvotes: 0