Open native iOS audio output destination picker in-app - ios

I would like to open iOS' native audio destination picker UI inside of my application (pictured below). Is there a way to do this with AVAudioSession?

This is the control I was looking for: https://developer.apple.com/documentation/avkit/avroutepickerview. It appears on screen as a button (with the default AirPlay 2 icon) and handles all of the logic for you.
Example
import AVKit
let audioPicker = AVRoutePickerView(frame: ...)
addSubview(audioPicker)
You can subscribe to audio route updates with the following:
NotificationCenter.default.addObserver(self, selector: ...), name: AVAudioSession.routeChangeNotification, object: nil)

Related

Handle IOS app open from tapping on Now Playing

I'm using SwiftAudio to play audio, I want to detect when audio playing in background mode and app become active from tapping on Now Playing.
NotificationCenter
.default
.addObserver(self, selector: #selector(applicationDidBecomeActive),
name: UIApplication.didBecomeActiveNotification,
object: nil)
#objc func applicationDidBecomeActive() {
print("applicationDidBecomeActive")
}
right now I'm trying this way but it trigger applicationDidBecomeActive even when user open app from icon.
If there any other ways to know when user tapping on Now Playing, please let me know.
I'm very new to IOS development and Swift
Edit: I guess I can detect how app is entered foreground from NowPlaying by passing the expected objectsender to addObserver instead of passing nil object. I tried many times but still don't know what sender object it it.
You should look into MPNowPlayingInfoCenter
When your app is active again, you can check the nowPlayingInfo and playbackState to customise what you actually want to do in your app.
What is the difference between launching from Notification or Now Playing in your case? Maybe clarifying this a bit more will allow us to craft a better solution to your problem.

IOS Monitor audio route changes in background

Is it possible to monitor changes to the Audio route without playing music? I have found this question, but it seems to rely on audio being played.
My current setup looks something like this:
NotificationCenter.default.addObserver(self, selector: #selector(routeChange), name: .AVAudioSessionRouteChange, object: nil)
#objc
func routeChange(n: Notification) {
...
}
The background mode "Audio, AirPlay and Picture in Picture" is enabled.
My setup seems to work fine as long as the app is in foreground. As soon as it is in the background, routeChange will not be called anymore.

Block screen recoding but allow HDMI screen mirroring Swift 4

I need to stop Screen recoding, however I need to allow video sharing through HDMI.
I know the captured notification is usually used for this, but I cant find a way to separate out these two things.
UIScreen.main.addObserver(self, forKeyPath: "captured", options: .new, context: nil)
The above triggers notifications when screen recording is started / stopped. But also triggers when an external display is connected / disconnected (though I'm finding it to be intermittent here).
I have tried using the following to detect if a screen within the UIScreen is mirrored or just to check the screen count. Recordings show only one screen in the count, and mirrored screens should show a count of 2
recordingLabel.text = "count \(UIScreen.screens.count)"
for screen in UIScreen.screens {
if screen.mirrored != nil {
recordingLabel.text = "Mirrored - count \(UIScreen.screens.count)"
}
}
But again this is intermittent. Most of the time the count does not change on connecting a HDMI screen, but does change to 2 on disconnecting.
I have also found using UIScreen notifications work for connected / disconnected. But there seems to be a race condition happening with the captured notification that I still need to handle recordings.
NotificationCenter.default.addObserver(self, selector: #selector(connected), name: UIScreen.didConnectNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(disconnected), name: UIScreen.didDisconnectNotification, object: nil)
Is there any way to detect the type of recording or prevent the race from happening when both types of notifications trigger?
EDIT :
I have since found
NotificationCenter.default.addObserver(self, selector: #selector(captureChanged(notification:)), name: UIScreen.capturedDidChangeNotification, object: nil)
Which works better with the connection notifications, but it still triggers before I get a connected notification. I don't really want to add a timer to detect if the didConnect notification triggers after the captureChange, but that might be the only way

detect banner pop up from any other app

In iOS, is there a listening function that will enable an app detect when there is a banner (from any other application like whatsapp/snapchat/iMessage banners) on the screen/UI?
For example, to detect brightness change I have something like this:
NotificationCenter.default.addObserver(
self,
selector: #selector(self.levelChanged),
name: .UIScreenBrightnessDidChange,
object: nil)
I also found this post, which is similar but I want my app to just know (on a binary level) if there is a banner or not, I do not need to know the content or source app of the banner.

NSNotificationCenter not working with Custom Keyboard

I am developing a custom keyboard using Swift. I want to get UIDeviceOrientationDidChangeNotification when Orientation changes; for that purpose, I am using:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeOrientation:", name: UIDeviceOrientationDidChangeNotification, object: nil)
func changeOrientation(notification: NSNotification) { }
It works with a simple app, but doesn't work with custom keyboard extension.
To anyone new to Extensions
Select your extension target, Select 'play' it will ask you to chose an app to run. Select an appropriate app.
Then you are running with the debugger attached to your extension and can use breakpoints.
-Dan
What did you put in your changeOrientation function ?
Breakpoints aren't working for me in the keyboard swift file.

Resources