PluggableApplicationDelegate icon indicating copy to clipboard operation
PluggableApplicationDelegate copied to clipboard

How to separate for remoteControlEvents

Open anishroff opened this issue 8 years ago • 1 comments

I tried this to separate remoteControlEvents and my code looks like

import Foundation
import PluggableApplicationDelegate

final class MusicControlsAppService: NSObject, ApplicationService {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        UIApplication.shared.beginReceivingRemoteControlEvents()
        print("It has started!")
        return true
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        print("It has entered background")
    }
    
    override func remoteControlReceived(with event: UIEvent?) {
        if event?.type == .remoteControl {
            switch event!.subtype {
            case .remoteControlPlay:
                PlayerManager.shared.playTapped()
            case .remoteControlPause:
                PlayerManager.shared.pauseTapped()
            case .remoteControlTogglePlayPause:
                if PlayerManager.shared.isPauseActive {
                    PlayerManager.shared.playTapped()
                } else {
                    PlayerManager.shared.pauseTapped()
                }
            case .remoteControlNextTrack:
                PlayerManager.shared.nextTapped()
            case .remoteControlPreviousTrack:
                PlayerManager.shared.previousTapped()
            default:
                break
            }
        }
    }
}

This gives error for the method Method does not override any method from its superclass , also when override keyword is removed it doesn't get calls.

Can you assist to solve this ? As of now I am keeping that method in the main App delegate which is subclass of PluggableApplicationDelegate which works.

anishroff avatar Apr 11 '17 09:04 anishroff

This is due to method -remoteControlReceived is not implemented in pluggableApplicationDelegate and it is not redirects is to any service. To make this work in your appDelegate override this method like this:

override func remoteControlReceived(with event: UIEvent?) { services.forEach{ if let service = $0 as? UIResponder { service.remoteControlRecieved(with event) } } }

you should some how store initialized services to prevent recreating them on every call of services property.

NikolayShubenkov avatar May 31 '17 10:05 NikolayShubenkov