Swift isLockingFocusWithCustomLensPositionSupported always returns false - swift

I want to set the lens distance of my iPhoneX to a constant.
In order to check if that is supported, I check the isLockingFocusWithCustomLensPositionSupported property of my device, as described in the documentation here: https://developer.apple.com/documentation/avfoundation/avcapturedevice/2361529-islockingfocuswithcustomlensposi
The method always returns false, even when the device is locked for configuration, which means that calling the method setFocusModeLocked(lensPosition, completionHandler) will alway throw an error.
Why is this the case, and how to correctly call the setFocusModeLocked() ?
Below is my approach:
let device = self.deviceInput.device
do {
try device.lockForConfiguration()
if device.isFocusPointOfInterestSupported && device.isFocusModeSupported(focusMode)
{
// this returns true
}
if device.isLockingFocusWithCustomLensPositionSupported
{
//this always returns false
device.setFocusModeLocked(lensPosition: focusDistance, completionHandler: nil)
}
device.unlockForConfiguration()
} catch {
print("Could not lock device for configuration: \(error)")
}
Tested on iPhoneX, iOS 12

Related

AVFoundation crash when setting isEnabled on AVCaptureAudioChannel

For some AVCaptureDevices, trying to set isEnabled on an AVCaptureAudioChannel results in a crash:
Assertion failed: (pSrcASBD->mChannelsPerFrame == [_internal->audioChannels count]), function -[AVCaptureConnection_Tundra copyPostSplitSummaryAudioFormatDescription], file AVCaptureConnection.m, line 817.
guard let channel = connection.audioChannels[safe: channelIndex] else { return }
if channel.isEnabled != enabled {
channel.isEnabled = enabled
}
The same issue does not occur when setting the volume:
guard let channel = connection.audioChannels[safe: channelIndex] else { return }
if channel.volume != volume {
channel.volume = volume
}
On a slightly unrelated note I would love to know why any issues I seem to see with AVFoundation result in the _tundra suffix. I believe this is the codename, but I do not see other crashes and log entries using this online.

AudioKit AKMicrophone not outputting any data

I am trying to capture FFT data from a microphone. I've managed to get it to work before with a similar codebase but since macOS Mojave it's broken - the fft data constantly stays 0.
Relevant Code:
var fft: AKFFTTap?
var inputDevice: AKDevice? {
didSet {
inputNode = nil
updateAudioNode()
}
}
var inputNode: AKNode? {
didSet {
if fft != nil {
// According to AKFFTTap class reference, it will always be on tap 0
oldValue?.avAudioNode.removeTap(onBus: 0)
}
fft = inputNode.map { AKFFTTap($0) }
}
}
[...]
guard let device = inputDevice else {
inputNode = ViewController.shared.player.mixer
return
}
do {
try AudioKit.setInputDevice(device)
}
catch {
print("Error setting input device: \(error)")
return
}
let microphoneNode = AKMicrophone()
do {
try microphoneNode.setDevice(device)
}
catch {
print("Failed setting node input device: \(error)")
return
}
microphoneNode.start()
microphoneNode.volume = 3
print("Switched Node: \(microphoneNode), started: \(microphoneNode.isStarted)")
inputNode = microphoneNode
try! AudioKit.start()
All the code is called, no errors are output, but the fft simply stays blank. With some code reordering I get varying errors.
A full version of the class, for completeness, is here.
Finally, I also tried implementing one to one the examples from the playground. Since XCode playgrounds seem to crash with AudioKit, I tried it in my own codebase, but there's no difference there either. AKFrequencyTracker, for example, gets 0s for both amplitude and frequency.
I am not 100% positive of this, but I'd like you to try AudioKit v4.5.1 out. We definitely fixed a bug in AKMicrophone, and that could have downstream consequences. I'll withdraw this answer and keep looking if it is not fixed. Let me know.

cannot call value of non-function type 'AVAudioSession.RecordPermission'

I am getting this error: (on switching to swift 4.2 from 4.0)
error: cannot call value of non-function type
'AVAudioSession.RecordPermission'
switch AVAudioSession.sharedInstance().recordPermission() {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
the following code should have no issues according to link
switch AVAudioSession.sharedInstance().recordPermission() {
case AVAudioSession.RecordPermission.granted:
NSLog("granted")
hasPermissions = true
break
case AVAudioSession.RecordPermission.denied:
NSLog("denied")
hasPermissions = false
break
case AVAudioSession.RecordPermission.undetermined:
NSLog("undetermined")
AVAudioSession.sharedInstance().requestRecordPermission() { [unowned self] allowed in
DispatchQueue.main.async {
if allowed {
self.hasPermissions = true
} else {
self.hasPermissions = false
}
}
}
break
default:
break
}
I am new to swift and was trying to refactor a flutter plugin, whats the issue here?
recordPermission is a property, not a function. Remove the ().

torchlevel not setting properly on iphone xs max

I made this app dozing that helps you meditate by adjusting the torch on your phone. It goes from a low-level brightness to a high-level birghtness over a certain time period.
It worked great for iPhone 6gen, 7gen, 8gen, and X. I just got the XS max, and for whatever reason instead of the brightness adjusting, it just stays turned on at maximum brightness.
It still works on the iPhone 7 with iOS 12. Weird thing is, SOMETIMES it RANDOMLY works on the XS Max, I just can't figure out what causes it to adjust properly and what causes it to sometimes be stuck on maximum brightness.
func updateTorch() {
guard let device = AVCaptureDevice.default(for: AVMediaType.video)
else {
return
}
if device.hasTorch && device.isTorchAvailable {
do {
try device.lockForConfiguration()
if torchMode == 0 {
device.torchMode = .off
} else {
try device.setTorchModeOn(level: torchMode) // HERE
}
device.unlockForConfiguration()
} catch {
print("Torch is not working.")
}
} else {
print("Torch not compatible with device.")
}
}
That's my main method that updates the torch. If I print "torchMode" where I marked "//HERE," it gives an ajusting Float value between 0 and 1. Also no errors are thrown from the setTorchModeOn(level:) method.

Hockey app did crash on last session

I'm using this function to detect if the app did crashed on last session, but in always returns the same result, no matter how and where i put "fatalError", or any other errors. What i'm doing wrong?
private func didCrashInLastSessionOnStartup() -> Bool {
//returns false
NSLog("\(BITHockeyManager.sharedHockeyManager().crashManager.didCrashInLastSession)")
//returns -1
NSLog("\(BITHockeyManager.sharedHockeyManager().crashManager.timeIntervalCrashInLastSessionOccurred)")
return (BITHockeyManager.sharedHockeyManager().crashManager.didCrashInLastSession) &&
(BITHockeyManager.sharedHockeyManager().crashManager.timeIntervalCrashInLastSessionOccurred < 5)
}
Here is my didFinishLaunchingWithOptions:
BITHockeyManager.sharedHockeyManager().configureWithIdentifier("<id>", delegate: self)
BITHockeyManager.sharedHockeyManager().crashManager.crashManagerStatus = .AutoSend;
BITHockeyManager.sharedHockeyManager().debugLogEnabled = true
BITHockeyManager.sharedHockeyManager().startManager()
BITHockeyManager.sharedHockeyManager().authenticator.authenticateInstallation();
if self.didCrashInLastSessionOnStartup() {
NSLog("Crashed on last session")
} else {
self.setupApplication()
}
And my delegate functions:
func crashManagerWillCancelSendingCrashReport(crashManager: BITCrashManager!) {
if self.didCrashInLastSessionOnStartup() {
self.setupApplication()
}
}
func crashManager(crashManager: BITCrashManager!, didFailWithError error: NSError!) {
if self.didCrashInLastSessionOnStartup() {
self.setupApplication()
}
}
func crashManagerDidFinishSendingCrashReport(crashManager: BITCrashManager!) {
if self.didCrashInLastSessionOnStartup() {
self.setupApplication()
}
}
The problem is that you are using additional 3rd party SDKs which incorporate a crash reporting feature and initialize those after the HockeySDK in your code. (Found that out via your support request and that information was never part of your question describing the situation)
You can only use one 3rd party crash reporting library in your app, the last one you initialize will always be the only one that works.