Type 'NSNotification.Name' has no member 'UIResponder' - swift5

I'm getting this error with Swift 5
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIResponder.keyboardWillHideNotification, object: nil)
also I'm getting below error
'Name' is not a member type of 'Notification'
public let ImagePickerTrayDidHide: Notification.Name = Notification.Name(rawValue: "ch.laurinbrandner.ImagePickerTrayDidHide")
how I can fix that?

As I can guess initially you had the following code:
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
When you compiled it in Xcode 10.1 you received the following errors: 'keyboardWillShowNotification' has been renamed to 'NSNotification.Name.UIKeyboardWillShow', Replace 'keyboardWillShowNotification' with 'NSNotification.Name.UIKeyboardWillShow' and 'keyboardWillHideNotification' has been renamed to 'NSNotification.Name.UIKeyboardWillHide', Replace 'keyboardWillHideNotification' with 'NSNotification.Name.UIKeyboardWillHide'.
Then you pressed "Fix" twice and get the incorrect code you already added to your question. You should use the following:
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

Replace the NotificationCenter.Name.UIResponder with UIResponder
For Eg:
NotificationCenter.default.addObserver(
self,
selector: #selector(self.keyboardWillShow(_:)),
name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(self.keyboardWillHide(_:)),
name: UIResponder.keyboardWillHideNotification, object: nil)
For More details refer https://stackoverflow.com/a/52325564/8331006

Replace UIResponder.NSNotification.Name.UIKeyboardWillShow with .UIKeyboardWillShow

Related

Fetching Global Events and Notifications in command line application with swift

I have a command line application and I would like to receive global mousemove and keypress events as well as some notifications. However this will not work. The console Application does have accessibility rights when started.
There is no error printed but it simply does not work. What could be the issue?
The Notification is working while the NSEvent is not working.
NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.keyDown, handler: logEvent)
NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.mouseMoved, handler: logEvent)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(logNotification), name: NSWorkspace.willSleepNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(logNotification), name: NSWorkspace.willPowerOffNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(logNotification), name: NSWorkspace.screensDidSleepNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(logNotification), name: NSWorkspace.sessionDidResignActiveNotification, object: nil)
#objc dynamic func logEvent(_ incomingEvent: NSEvent?) -> Void{
print("here was a keypress or mousemove ")
}
#objc dynamic func setInactive(aNotification : NSNotification){
Log("Event was called " + aNotification.name.rawValue)
}

Swift Notification is sent but not received

I added an observer to be notified when a charger is connected.
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
nil,
{ (_, _, _, _, _) in
print("Sending...")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "test123"), object: nil)
},
kIOPSNotifyAttach as CFString?,
nil,
.deliverImmediately
)
When the charger is connected the a notification is sent so that I can react in my application.
So I added an observer in my app
NotificationCenter.default.addObserver(
self,
selector: #selector(reactToNotification(_:)),
name: NSNotification.Name(rawValue: "test123"),
object: nil)
The notification is sent correctly but the associated function is never called.
#objc func reactToNotification(_ notification: Notification) {
print("Receiving...")
}
Is there a specific problem in my concept?
It's really mismatch issue
You sent this notification NSNotification.Name(rawValue: "test123")
But expected to get this: NSNotification.Name(rawValue: "test")
It's just naming issue!

How to detect day change in Swift

I want my app to act when there is a change to another day.
So, in my appDelegate, I put
func applicationSignificantTimeChange(_ application: UIApplication){
//this one fires
}
and in the ViewController that should update its content I do:
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(self.dayChanged(notification:)), name: Notification.Name("significantTimeChangeNotification"), object: nil)
}
and
#objc func dayChanged(notification: NSNotification){
//this one doesn't fire
}
somehow, while the func in AppDelegate is called, the observer seems to be blind for that event.
Is this syntax, or just plain misunderstanding of the mechanism?
You need to add an observer for "UIApplicationSignificantTimeChangeNotification":
NotificationCenter.default.addObserver(self, selector: #selector(dayChanged), name: UIApplicationSignificantTimeChangeNotification, object: nil)
For Swift 4.2 or later
NotificationCenter.default.addObserver(self, selector: #selector(dayChanged), name: UIApplication.significantTimeChangeNotification, object: nil)
Note: If your intent is to be notified when the day changes you can use .NSCalendarDayChanged ("NSCalendarDayChangedNotification") instead of UIApplication.significantTimeChangeNotification.
NotificationCenter.default.addObserver(self, selector: #selector(dayChanged), name: .NSCalendarDayChanged, object: nil)
And add the selector method to the view controller where you would like to monitor the day changes:
#objc func dayChanged(_ notification: Notification) {
}

Not getting screenLock notification on Swift 4 on mac

For some reason I'm not getting the ScreenIsLocked and ScreenIsUnlocked notifications. I defined that the screen get locked 0 seconds after the screen saver starts and yet no log:
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#objc func screenLocked() {
NSLog("yes")
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
NotificationCenter.default.addObserver(
self,
selector: #selector(AppDelegate.screenLocked),
name: Notification.Name("com.apple.screenIsLocked"),
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(AppDelegate.screenLocked),
name: Notification.Name("com.apple.screenIsUnlocked"),
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(AppDelegate.screenLocked),
name: Notification.Name("com.apple.screensaver.didstart"),
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(AppDelegate.screenLocked),
name: Notification.Name("com.apple.screensaver.didstop"),
object: nil)
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
Use NSDistributedNotificationCenter
This is from a post by Kane Cheshire:
https://kanecheshire.com/blog/2014/10/13/351/
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver:self
selector:#selector(screenLocked)
name:#"com.apple.screenIsLocked"
object:nil];
[center addObserver:self
selector:#selector(screenUnlocked)
name:#"com.apple.screenIsUnlocked"
object:nil];
}
In Swift:
DistributedNotificationCenter.default().addObserver(self,
selector: #selector(screenLocked),
name: "com.apple.screenIsLocked",
object: nil)
DistributedNotificationCenter.default().addObserver(self,
selector: #selector(screenLocked),
name: "com.apple.screenIsUnlocked",
object: nil)
It's slightly different syntax now for the notification names.
DistributedNotificationCenter.default().addObserver(forName: NSNotification.Name(rawValue: "com.apple.screenIsLocked"),
object: nil,
queue: .main) { [weak self] _ in
// Handle it
}
DistributedNotificationCenter.default().addObserver(forName: NSNotification.Name(rawValue: "com.apple.screenIsUnlocked"),
object: nil,
queue: .main) { [weak self] _ in
// Handle it
}

NsNotificationCenter is not working

Actually i'm new in swift i got stuck here,Anyone can solve this Problem. actOnSpecialNotification Func is not calling on fireNotification in ViewController.swift
In ViewController.swift
func fireNotification() -> Void {
NotificationCenter.default.addObserver(self, selector:
#selector(vikas.updateNotificationSentLabel), name:
NSNotification.Name(rawValue: mySpecialNotificationKey), object: nil)
}
func updateNotificationSentLabel() {
print("sent")
}
in SecondVC.swift
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector:
#selector(ViewController.actOnSpecialNotification), name:
NSNotification.Name(rawValue: mySpecialNotificationKey), object: nil)
}
func actOnSpecialNotification() {
print("listen")
}
First of all add Observer to your FirstViewConroller.
FirstViewConroller
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name:”test”, object: nil)
Now, add the relevant selector method in same ViewController which will be called once the notification will be fired.
func methodOfReceivedNotification(notification: Notification){
//Take Action on Notification
}
Now, you can fire the notification using below lines which will call the above method which resides in FirstViewController
SecondViewController
NSNotificationCenter.defaultCenter().postNotificationName(“test”, object: nil)