This is the code I had in swift 2.
How do I use the same thing in swift 3?
NotificationCenter.default.addObserver(self, selector: "handleInterruption", name: AVAudiosessionInterruptionNotification, object: nil)
Thanks in advance!
Have a look at this fine answer
As it says:
All of the system notification types are now defined as static constants on Notification.Name; i.e. .UIApplicationDidFinishLaunching, .UITextFieldTextDidChange, etc.
So, in your case, you are probably looking for Notification.Name.AVAudioSessionInterruption
And I think this should work for you:
NotificationCenter.default.addObserver(self, selector: #selector(handleInterruption), name: .AVAudioSessionInterruption, object: nil)
Hope that helps.
Related
The code below was working fine before Swift 4.2:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
When I click the 'Fix' option, it becomes:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
But it is still marked an error. Here is the explanation:
Type 'NSNotification.Name' has no member 'UIResponder'
And then I tried to delete 'UIResponder':
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.
...but I don't know how should I complete it.
The correct form is:
UIResponder.keyboardWillShowNotification
...so, your code becomes:
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillChange(notification:)),
name: UIResponder.keyboardWillShowNotification,
object: nil
)
This is a known issue with Xcode 10. Automatic Fix-it is not working correctly for Swift 4.2 when it comes to correcting notification names.
In Swift 4.2, lots of Notification.Name instances became instance variables in other classes. For example, keyboardWillShowNotification is now an instance variable of UIResponder.
For someone else out there, I was building (what I thought was) a UI-Independent class and did not import UIKit.
Nothing worked until I added at the top of my file, this:
import UIKit
It appears some notifications (those in UIApplication, UIResponder etc..) may have been refactored into UIKIt.
The selected answer is incomplete and produce compilers error,
Cannot invoke 'addObserver' with an argument list of type
'(RegistrationViewController, selector: Selector, name:
NSNotification.Name)'
Here is the working format,
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
` setSession()
UIApplication.shared.beginReceivingRemoteControlEvents()
becomeFirstResponder()
NotificationCenter.defaultCenter.addObserver(self, Selector("handleInterruption"), name: AVAudioSessionInterruptionNotification, object: nil)
//the Error is the line above this comment
player = Player()
player.playStream(fileUrl: "http:wwwwww.mp3")
updatePlayButton()`
Im trying to check when a phone call has finished by using AVAudiointerruptionNotification but I'm doing something wrong I keep getting the Argument labels _, _, name, object do not match any available overloads...
The new way of using the Notification center in swift 3 is as fallows. I was able to compile with no errors or warnings!
NotificationCenter.default.addObserver(self, selector:
(Selector("handleInterruption")), name:
NSNotification.Name.AVAudioSessionInterruption, object: nil)
I create a class named DecryptAudioPlayer,it inherit NSObject,this class refrenced a AVPlayer,and observe the notice AVPlayerItemDidPlayToEndTimeNotification when init.like below:
override init() {
super.init()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.playToEnd(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
}
and i have the method:
func playToEnd(notification:NSNotification) {
Log.printLog("notification:\(notification)")
}
but sometimes the player's ower can't recived the AVPlayerItemDidPlayToEndTimeNotification,i am very confused.it seems the AVPlayerItem may don't post AVPlayerItemDidPlayToEndTimeNotification when its end time ,who can tell me why?
I can get the stalled notification to solve the problem. So the question could be put down for a while.
When my watchKit app goes to background it fires the delegate method applicationWillResignActive. Method documentation says it can be used to pause ongoing tasks.
I have an ongoing method that i want to be stopped or broken by the use of the external method. How do i do that?
Example
func method1(){
// performing some actions
}
func breakMethod1(){
// running this method can stop (break) the execution of method1
}
This is, of course, assuming that your app has been architected so that breakMethod1() will definitely cancel the action occurring in method1().
You should set up an observer for an NSNotification at the beginning of method1() like so:
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "breakMethod1", name: UIApplicationWillResignActiveNotification, object: nil)
And for the sake of cleanup, you should also remove this observer after it's been triggered like so:
notificationCenter.removeObserver(self, name: UIApplicationWillResignActiveNotification, object: nil)
I have an iOS Swift App and i want to detect which element is selected by the voiceover.
Have tried to send an UIAccessibilitySwitchControlStatusDidChangeNotification but i don't get this to work.
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "handleVoiceoverSelection",
name: UIAccessibilitySwitchControlStatusDidChangeNotification,
object: nil)
...
func handleVoiceoverSelection(){
println("!!!! element selected !!!!")
}
Is there any way?
Maybe check out UIAccessibilityFocus?
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAccessibilityFocus_Protocol/index.html