Ios 通过硬件按钮获取事件的任何可能性。

Ios 通过硬件按钮获取事件的任何可能性。,ios,swift,Ios,Swift,是否可以通过按下硬件按钮来获取应用程序上的任何事件 例如:音量按钮/主页按钮 我希望它能帮助你 当用户按下主页按钮时,您可以通过appDelegates方法了解情况。 func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for c

是否可以通过按下硬件按钮来获取应用程序上的任何事件

例如:音量按钮/主页按钮

我希望它能帮助你

当用户按下主页按钮时,您可以通过appDelegates方法了解情况。

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func listenVolumeButton(){

    let audioSession = AVAudioSession.sharedInstance()
    audioSession.setActive(true, error: nil)
    audioSession.addObserver(self, forKeyPath: "outputVolume",
        options: NSKeyValueObservingOptions.New, context: nil)
}

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject,
    change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
    if keyPath == "outputVolume"{
        print("got in here")
    }
}
当用户按下音量按钮时,如果您想检测它。

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func listenVolumeButton(){

    let audioSession = AVAudioSession.sharedInstance()
    audioSession.setActive(true, error: nil)
    audioSession.addObserver(self, forKeyPath: "outputVolume",
        options: NSKeyValueObservingOptions.New, context: nil)
}

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject,
    change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
    if keyPath == "outputVolume"{
        print("got in here")
    }
}
func listenVolumeButton(){
让audioSession=AVAudioSession.sharedInstance()
audioSession.setActive(真,错误:无)
audioSession.addObserver(self,forKeyPath:“outputVolume”,
选项:NSKeyValueObservingOptions.New,上下文:nil)
}
重写func observeValueForKeyPath(键路径:字符串,对象对象:AnyObject,
更改:[NSObject:AnyObject],上下文:UnsafeMutablePointer){
如果keyPath==“outputVolume”{
打印(“进入这里”)
}
}

您可以观察
outputVolume
属性来检测音量按钮动作

func volumeAction() {
    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setActive(true, options: [])
        audioSession.addObserver(self, forKeyPath: "outputVolume",
                                 options: .new, context: nil)
    } catch {
        print("Error")
    }
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "outputVolume"{
        print("volume changed")
    }
}

由于较新的设备中不再提供Home按钮,因此您可以使用生物认证(面部ID或触摸ID)进行相同的操作。

Home按钮和的可能重复:新的iOS设备没有Home按钮,可能生物认证就足够了