Reactive Location - Handle success and failure block - ios

It might be simple question.
I'm using Reactive Location, to get user's current location, please find my below code,
ReactiveLocation.authorizeAction.apply(.whenInUse).startWithResult {
switch $0 {
case let .success(status):
print("Current user permission status on WhenInUse is \(status)")
case let .failure(error):
print(error.localizedDescription)
}
}
Here error is .restricted and .denied, I want user to be presented with the error message according to error. How to identify it.
In above code, completion block looks like this,
Please help me to solve the issue.

I suggest learning more about Swift enum.
You can check for .restricted and .denied the same way you checked for .success and .failure.
The only difference is LocationAuthorizationError doesn't have associated values.
ReactiveLocation.authorizeAction.apply(.whenInUse).startWithResult {
switch $0 {
case let .success(status):
print("Current user permission status on WhenInUse is \(status)")
case let .failure(actionError):
switch actionError {
case .producerFailed(.restricted):
print("Authorization Restricted")
case .producerFailed(.denied):
print("Authorization Denied")
default:
break
}
}
}

Related

ATTrackingManager.AuthorizationStatus always returns notDetermined

I don't know can I use this functionality in my UI tests on iOS, but I try it, an have problem with this.
In my UI tests I can choose Allow tracking for my app or I can decline tracking, but after all these actions, I want checkout status IDFA via ATTrackingManager.AuthorizationStatus, but this method always returns notDetermined. If I go to Settings > Privacy > Tracking, here I see that settings applied correctly (switch Allow App To Request To Track is on and switch for my app in right state (on or off)).
I don't have any idea why I recieve wrong AuthorizationStatus.
Here is my code in my XCTestCase:
import AppTrackingTransparency
enum TrackingStatus {
case authorized
case denied
case notDetermined
}
func map(_ status: ATTrackingManager.AuthorizationStatus) -> TrackingStatus {
switch ATTrackingManager.trackingAuthorizationStatus {
case .notDetermined:
return .notDetermined
case .authorized:
return .authorized
default:
return .denied
}
}
func advertisingTrackingStatusCheckout(status: TrackingStatus) {
print("IDFA status: \(ATTrackingManager.trackingAuthorizationStatus)")
var currentTrackingStatus: TrackingStatus {
return map(ATTrackingManager.trackingAuthorizationStatus)
}
guard currentTrackingStatus == status else {
XCTFail("IDFA status: \(currentTrackingStatus), expected: \(status)")
return
}
}
After settings IDFA status in my UI test, i call this method, ex. advertisingTrackingStatusCheckout(status: TrackingStatus.denied)
But it always returns notDetermined.
It behaviors have only one exception: If I manually set switch Allow App To Request To Track to off-state, calling the ATTrackingManager.trackingAuthorizationStatus will returns denied.
Delete the App, And call your function in sceneDidBecomeActive with delay. So once your app become active then it will shown. Am facing the same issue now its resolved. Like this
func sceneDidBecomeActive(_ scene: UIScene) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.requestPermission()
}
}
func requestPermission() {
if #available(iOS 14, *) {
ATTrackingManager.requestTrackingAuthorization { status in
switch status {
case .authorized:
// Tracking authorization dialog was shown
// and we are authorized
print("Authorized Tracking Permission")
// Now that we are authorized we can get the IDFA
case .denied:
// Tracking authorization dialog was
// shown and permission is denied
print("Denied Tracking Permission")
case .notDetermined:
// Tracking authorization dialog has not been shown
print("Not Determined Tracking Permission")
case .restricted:
print("Restricted Tracking Permission")
#unknown default:
print("Unknown Tracking Permission")
}
}
} else {
// Fallback on earlier versions
}
}

What are the delegate methods for ReplayKit's initial alert choices?

When the the user first decides to use ReplayKit there is an alert that appears. It gives 3 choices:
-Record Screen and Microphone
-Record Screen Only
-Don’t Allow
What are the delegate methods so that I can find out which option a user choose?
There aren't any delegate methods to determine which option a user chooses. You have to use the completionHandler and .isMicrophoneEnabled to determine the chosen option.
Once an option is chosen the completionHandler is called:
If the user chooses Don't Allow then the error code will run
If the user chooses Record Screen & Microphone then .isMicrophoneEnabled will get set to true
If the user chooses Record Screen Only then .isMicrophoneEnabled will get set to false
Inside the completionHandler you can check to see their choice then do whatever you need to do from that point on. Read the 2 comments in the completionHandler part of the code below.
let recorder = RPScreenRecorder.shared()
recorder.startCapture(handler: { [weak self](buffer, bufferType, err) in
// ...
}, completionHandler: { (error) in
// 1. If the user chooses "Dont'Allow", the error message will print "The user declined application recording". Outside of that if an actual error occurs something else will print
if let error = error {
print(error.localizedDescription)
print("The user choose Don't Allow")
return
}
// 2. Check the other 2 options
if self.recorder.isMicrophoneEnabled {
print("The user choose Record Screen & Microphone")
} else {
print("The user choose Record Screen Only")
}
})
The safe way to do this so that you know how to respond to each error code is to use a switch statement on the error codes:
}, completionHandler: { (error) in
if let error = error as NSError? {
let rpRecordingErrorCode = RPRecordingErrorCode(rawValue: error.code)
self.errorCodeResponse(rpRecordingErrorCode)
}
})
func errorCodeResponse(_ error: RPRecordingErrorCode?) {
guard let error = error else { return }
switch error {
case .unknown:
print("Error cause unknown")
case .userDeclined:
print("User declined recording request.")
case .disabled:
print("Recording disabled via parental controls.")
case .failedToStart:
print("Recording failed to start.")
case .failed:
print("Recording error occurred.")
case .insufficientStorage:
print("Not enough storage available on the device.")
case .interrupted:
print("Recording interrupted by another app.")
case .contentResize:
print("Recording interrupted by multitasking and content resizing.")
case .broadcastInvalidSession:
print("Attempted to start a broadcast without a prior session.")
case .systemDormancy:
print("Recording forced to end by the user pressing the power button.")
case .entitlements:
print("Recording failed due to missing entitlements.")
case .activePhoneCall:
print("Recording unable to record due to active phone call.")
default: break
}
}
If you only want to detect when Don't Allow is tapped, here is a simple solution:
recorder.startCapture(handler: { [self] (sampleBuffer, sampleType, passedError) in
if let passedError = passedError {
print(passedError.localizedDescription)
return
}
}) { err in
if let error = err {
if error._code == RPRecordingErrorCode.userDeclined.rawValue {
print("User didn't allow recording")
}
}
}

AudioKit microphone does not init on first run

Good afternoon,
I am using the latest version of AudioKit (4.9 Master Branch) with Xcode 11.3 and am running into a strange permission problem. When a user launches an app for the first time, the OS asks for permission to use the microphone. After the user selects "Allow", the microphone does not work, but if you force quit the app and relaunch it the microphone works fine.
I created a function to test the state of the permission and placed it in viewDidLoad() :
func checkMic() {
switch AVAudioSession.sharedInstance().recordPermission {
case AVAudioSessionRecordPermission.granted:
print("Permission granted")
case AVAudioSessionRecordPermission.denied:
print("Pemission denied")
case AVAudioSessionRecordPermission.undetermined:
print("Request permission here")
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
print("Mic Enabled")
})
#unknown default:
print("Not Working")
}
}
The function is only triggered during the first run but I have already initialized my AudioKit singleton class by this point. I have tried to reinitialize with no success. I have not encountered this before and am wondering if anyone else is experiencing this change or had a suggestion for it.
It seems that both the AKAnalizer and AKMicrophone are disabled when when iOS asks for permission to use the microphone. I needed to start both again. Hope this helps anyone in the same situation.
var audioCore = AudioCore.sharedInstance()
func checkMic() {
switch AVAudioSession.sharedInstance().recordPermission {
case AVAudioSessionRecordPermission.granted:
print("Permission granted")
case AVAudioSessionRecordPermission.denied:
print("Pemission denied")
case AVAudioSessionRecordPermission.undetermined:
print("Request permission here")
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
print("Mic Enabled")
self.audioCore.microphone.start()
self.audioCore.analyzer.start()
print(self.audioCore.microphone.isStarted)
print(self.audioCore.analyzer.isStarted)
})
#unknown default:
print("Not Working")
}

Unable to upload image using photo library

I am getting this error after choosig image from gallery in ios app
errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}
I have also added- Privacy- Photo Library photo description, camera description.
Also tried- OS_ACTIVITY_MODE disable
but still unable to fix
I am not sure what code you are using but one of the problems might be that you are not properly handling all the authorisation cases.
func performPermissionCheck() {
let authorisationStatus = PHPhotoLibrary.authorizationStatus()
switch authorisationStatus {
case .authorized:
print("Access granted")
case .notDetermined:
PHPhotoLibrary.requestAuthorization({ newStatus in
if newStatus == PHAuthorizationStatus.authorized {
print("success")}
})
case .restricted:
print("No access to album")
case .denied:
print("Permission Denied")
}
}

How to go to settings directly if the permission to access photo library is denied in swift

class func checkPhotoLibraryPermission() {
// TODO: #Umer Complete this process as discussed
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthorizationStatus {
case .authorized:
print("Access is granted by user")
case .notDetermined:
PHPhotoLibrary.requestAuthorization({
(newStatus) in
print("status is \(newStatus)")
if newStatus == PHAuthorizationStatus.authorized {
/* do stuff here */
print("success")
}
else {
}
})
print("It is not determined until now")
case .restricted:
print("User do not have access to photo album.")
case .denied:
print("User has denied the permission.")
}
}
in case .denied
If we come first time to access photo library access permission is asked to user and if user do not allow permission, next time when user tries to access photo library it directly goes to settings where user can manually set permissions.

Resources