` 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)
Related
My user is redirected to their inbox when they verify their email. When they reopen the application I need to reload the user's data automatically. I used the following code to do so...
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
Auth.auth().currentUser?.reload()
}
However, this code only works if I completely close the application and reopen it. Is there a way for me to call this without having to close the application?
You can try didBecomeActiveNotification:
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
Auth.auth().currentUser?.reload()
}
A slightly different approach but should do the same,
In ViewDidAppear:
NotificationCenter.default.addObserver(self, selector: #selector(yourFunction), name: UIApplication.didBecomeActiveNotification, object: nil)
#objc function yourFunction(){
print("Returned from background")
// Do something
}
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)
I need help with this code. I followed a tutorial from 2014 and I have an error with dispatch_async. I am trying to create an app that allows the user to play with other player nearby or peer to peer. I think the code needs to be updated because it has been renamed in swift 4 but I don't know what the new name is.
This is the code
// below is where the code are wrong
dispatch_async(dispatch_get_main_queue(),{ () -> Void in
NSNotificationCenter.defaultCenter().postNotificationName("MPC_DidChangeStateNotification",object:nil,userInfo: userInfo)
})
the full code is in this link https://github.com/javaplanet17/test/blob/master/multipeer it is inside of a func on line 36.
the error list
first I got this error
MPCHandler.swift:55:9: 'NSNotificationCenter' has been renamed to 'NotificationCenter'
I pressed fix and renamed it then I got another error
MPCHandler.swift:55:123: Use of unresolved identifier 'userInfo'
I pressed fix and renamed it then I still got an error
MPCHandler.swift:55:45: Cannot call value of non-function type 'NotificationCenter' Replace '()' with ''
Once again I pressed fix and changed it
The code now look like this:
NotificationCenter.defaultCenter.postNotificationName("MPC_DidChangeStateNotification",object:nil,userInfo: userinfo)
Then I updated it to:
dispatch_async(dispatch_get_main_queue(),{ () -> Void in
NotificationCenter.default.post("MPC_DidChangeStateNotification",object:nil,userInfo: userinfo)
})
yet I still got an error
MPCHandler.swift:55:121: Extra argument 'userInfo' in call
I have tried to change it into:
DispatchQueue.main.async(execute:{ () -> Void in NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MPC_DidReceiveDataNotification"), object: nil, userInfo: userinfo))
})
but I still get an error. How do I fix this error?
It worked for me this way might try it out
DispatchQueue.main.async(execute: { _ in
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "MPC_DidChangeStateNotification"), object: nil, userInfo: userInfo)
})
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.
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.