NSNotification not working on Swift translation (from Objective-C) - ios

After converting my Objective-C code to Swift, I cannot get my NSNotifications to work. After an hour of searching in the web, I finally gave up. Consider the following example:
func getToUrl(url:String, timeoutInterval:Float) -> Bool {
println("Starting HTTP GET to: \(url)")
// Fire a notification
NSNotificationCenter.defaultCenter().postNotificationName("StartNotification", object: self)
[...]
}
func getJsonFromServer() {
// Add an observer which should fire the method test when desired
NSNotificationCenter.defaultCenter().addObserver(self, selector: "test:", name: "StartNotification", object: self)
// Calls the function
getToUrl("http://www.stackoverflow.com", timeoutInterval: 10)
}
func test(sender: AnyObject) {
println("I am here!")
}
I cannot find the error, I would really appreciate if someone else could!
The code runs, but the test method is never called.

Change in this, self to nil (in order to hear all objects)
func getJsonFromServer() {
// Add an observer which should fire the method test when desired
NSNotificationCenter.defaultCenter().addObserver(self, selector: "test:", name: "StartNotification", object: nil)
// Calls the function
getToUrl("http://www.stackoverflow.com", timeoutInterval: 10)
}

Related

How to execute a function from second ViewController after popViewController?

I am an Android developer trying to port my app to iOS and this is my first time working with Swift and Xcode, or anything Mac in general. The app is to scan for a QR code, if the QR code is invalid then I'd like it to go back to the previous screen and display a toast. I've already made a function that displays the toast stating the QR code was invalid.
func found(code: String) {
print(code)
// convert JSON to object
do {
let qrMessage = try JSONDecoder().decode(QrMessage.self, from: code.data(using: .utf8)!)
print("QrMessage object - device name: " + qrMessage.pcName)
}
catch let error {
print(error)
_ = navigationController?.popViewController(animated: true)
// call ShowInvalidQrToast() from the new VC that is now on top of the ViewController stack
}
EDIT:
I managed to figure out a solution using the NotificationCenter from the first answer in this thread: Calling function from another ViewController in swift. More info in my answer below. If you think there is a better way of doing this, please post your own answer.
My solution using NotificationCenter:
In ConnectViewController
override func viewDidLoad() {
super.viewDidLoad()
...
NotificationCenter.default.addObserver(self, selector: #selector(showInvalidQrCodeToast(_:)), name: Notification.Name(rawValue: "showInvalidQrCodeToast"), object: nil)
}
#objc func showInvalidQrCodeToast(_ notification: Notification) {
self.view.makeToast("Invalid QR code")
}
In ScannerViewController
func found(code: String) {
print(code)
// convert JSON to object
do {
let qrMessage = try JSONDecoder().decode(QrMessage.self, from: code.data(using: .utf8)!)
print("QrMessage object - device name: " + qrMessage.pcName)
}
catch let error {
print(error)
_ = navigationController?.popViewController(animated: true)
NotificationCenter.default.post(name: Notification.Name(rawValue: "showInvalidQrCodeToast"), object: nil)
}

App will terminate Notification

I'm working on SDK and try to catch app termination Notification. It's easy to get this as closure for (ex) NSNotification.Name.UIApplicationWillResignActive
self.resignActiveNotification = NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationWillResignActive,
object: nil,
queue: nil) { _ in
//something goes here and it works like a charm.
}
So I want to have similar behavior for termination:
self.terminateNotification = NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationWillTerminate,
object: nil,
queue: nil) { _ in
NSLog("%#",#function)
}
And this one never gets called!
Of course if I put in AppDelegate:
func applicationWillTerminate(_ application: UIApplication) {
//termination
}
It will work, but since I'm building an SDK I cannot have AppDelegate methods. There is a way to get termination closure call? Or any other way to know that application is about to terminate?
In Apple documentation you can find:
After calling this method, the app also posts a
UIApplicationWillTerminate notification to give interested objects a
chance to respond to the transition.
However this seems to be broken
This seems working for me:
init() {
NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillTerminate(notification:)),
name: UIApplication.willTerminateNotification,
object: nil)
}
#objc func applicationWillTerminate(notification: Notification) {
// Notification received.
}
deinit {
NotificationCenter.default.removeObserver(self)
}

How to pass a Notification to the method called by the Observer in Swift 3

I would like to access the Notification object that is sent from the method below.
var currentTrack:MPMediaItem? {
get{
return playlist?.items[index]
}
set{
print(newValue?.title!)
//self.index = locateIndex(track: newValue!)
let notif = Notification.init(name: Playlist.SongChangedName, object:self)
NotificationCenter.default.post(notif)
}
}
The Notifications name is defined as:
static let SongChangedName = Notification.Name("SongChangedNotification")
Here is the observer:
override init() {
super.init()
NotificationCenter.default.addObserver(self,
selector: #selector(testSelector),
name: Playlist.SongChangedName, //Notification.Name("songChanged"),
object: nil)
}
Here is the Method it calls:
func testSelector(notification:Notification){
queueNextTrack()
}
How do I pass testSelector a notification object? I know it has something to do with the object parameter of the addObserver method.
Thank you.
You can now get rid of your problem entirely by not using selectors in your notifications, timers, etc. There are new block based API to replace target-selector such as
NotificationCenter.default.addObserver(forName: Playlist.SongChangedName, object: nil, queue: nil, using: { notification in
self.testSelector(testSelector)
})
For the most part you won't need access to the notifications in your blocks so you could do this too
func testSelector(){
queueNextTrack()
}
NotificationCenter.default.addObserver(forName: Playlist.SongChangedName, object: nil, queue: nil) { _ in
self.testSelector()
}
or most my preferred in most scenarios:
override init() {
super.init()
let testBlock: (Notification) -> Void = {
self.queueNextTrack()
}
NotificationCenter.default.addObserver(forName: Playlist.SongChangedName, object: nil, queue: nil, using: testBlock)
}
EDIT I'd also suggest you take a look at the sample code in the description for this API

NotificationCenter issue on Swift 3 [duplicate]

This question already has answers here:
NSNotificationCenter addObserver in Swift
(16 answers)
Closed 6 years ago.
I'm learning Swift 3 and I'm trying to using NSNotificationCenter. Here is my code:
func savePost(){
let postData = NSKeyedArchiver.archivedData(withRootObject: _loadedpost)
UserDefaults.standard().object(forKey: KEY_POST)
}
func loadPost(){
if let postData = UserDefaults.standard().object(forKey: KEY_POST) as? NSData{
if let postArray = NSKeyedUnarchiver.unarchiveObject(with: postData as Data) as? [Post]{
_loadedpost = postArray
}
}
//codeerror
NotificationCenter.default().post(NSNotification(name: "loadedPost" as NSNotification.Name, object: nil) as Notification)
}
and this is the observer:
override func viewDidLoad() {
super.viewDidLoad()
//codeerorr
NotificationCenter.default().addObserver(self, selector: Selector(("onPostLoaded")), name: "loadedPost", object: nil)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
It always gives me the error "signal SIGBRT". When I try to change the name in the observer, it's not an error, but obviously it didn't show anything. How do I fix this?
Swift 3 & 4
Swift 3, and now Swift 4, have replaced many "stringly-typed" APIs with struct "wrapper types", as is the case with NotificationCenter. Notifications are now identified by a struct Notfication.Name rather than by String. For more details see the now legacy Migrating to Swift 3 guide
Swift 2.2 usage:
// Define identifier
let notificationIdentifier: String = "NotificationIdentifier"
// Register to receive notification
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification(_:)), name: notificationIdentifier, object: nil)
// Post a notification
NSNotificationCenter.defaultCenter().postNotificationName(notificationIdentifier, object: nil)
Swift 3 & 4 usage:
// Define identifier
let notificationName = Notification.Name("NotificationIdentifier")
// Register to receive notification
NotificationCenter.default.addObserver(self, selector: #selector(YourClassName.methodOfReceivedNotification), name: notificationName, object: nil)
// Post notification
NotificationCenter.default.post(name: notificationName, object: nil)
// Stop listening notification
NotificationCenter.default.removeObserver(self, name: notificationName, object: nil)
All of the system notification types are now defined as static constants on Notification.Name; i.e. .UIApplicationDidFinishLaunching, .UITextFieldTextDidChange, etc.
You can extend Notification.Name with your own custom notifications in order to stay consistent with the system notifications:
// Definition:
extension Notification.Name {
static let yourCustomNotificationName = Notification.Name("yourCustomNotificationName")
}
// Usage:
NotificationCenter.default.post(name: .yourCustomNotificationName, object: nil)
Swift 4.2 usage:
Same as Swift 4, except now system notifications names are part of UIApplication. So in order to stay consistent with the system notifications you can extend UIApplication with your own custom notifications instead of Notification.Name :
// Definition:
UIApplication {
public static let yourCustomNotificationName = Notification.Name("yourCustomNotificationName")
}
// Usage:
NotificationCenter.default.post(name: UIApplication.yourCustomNotificationName, object: nil)
Notifications appear to have changed again (October 2016).
// Register to receive notification
NotificationCenter.default.addObserver(self, selector: #selector(yourClass.yourMethod), name: NSNotification.Name(rawValue: "yourNotificatioName"), object: nil)
// Post notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "yourNotificationName"), object: nil)
For all struggling around with the #selector in Swift 3 or Swift 4, here a full code example:
// WE NEED A CLASS THAT SHOULD RECEIVE NOTIFICATIONS
class MyReceivingClass {
// ---------------------------------------------
// INIT -> GOOD PLACE FOR REGISTERING
// ---------------------------------------------
init() {
// WE REGISTER FOR SYSTEM NOTIFICATION (APP WILL RESIGN ACTIVE)
// Register without parameter
NotificationCenter.default.addObserver(self, selector: #selector(MyReceivingClass.handleNotification), name: .UIApplicationWillResignActive, object: nil)
// Register WITH parameter
NotificationCenter.default.addObserver(self, selector: #selector(MyReceivingClass.handle(withNotification:)), name: .UIApplicationWillResignActive, object: nil)
}
// ---------------------------------------------
// DE-INIT -> LAST OPTION FOR RE-REGISTERING
// ---------------------------------------------
deinit {
NotificationCenter.default.removeObserver(self)
}
// either "MyReceivingClass" must be a subclass of NSObject OR selector-methods MUST BE signed with '#objc'
// ---------------------------------------------
// HANDLE NOTIFICATION WITHOUT PARAMETER
// ---------------------------------------------
#objc func handleNotification() {
print("RECEIVED ANY NOTIFICATION")
}
// ---------------------------------------------
// HANDLE NOTIFICATION WITH PARAMETER
// ---------------------------------------------
#objc func handle(withNotification notification : NSNotification) {
print("RECEIVED SPECIFIC NOTIFICATION: \(notification)")
}
}
In this example we try to get POSTs from AppDelegate (so in AppDelegate implement this):
// ---------------------------------------------
// WHEN APP IS GOING TO BE INACTIVE
// ---------------------------------------------
func applicationWillResignActive(_ application: UIApplication) {
print("POSTING")
// Define identifiyer
let notificationName = Notification.Name.UIApplicationWillResignActive
// Post notification
NotificationCenter.default.post(name: notificationName, object: nil)
}
I think it has changed again.
For posting this works in Xcode 8.2.
NotificationCenter.default.post(Notification(name:.UIApplicationWillResignActive)

How to send and receive data in Today extensions

I want to develop an app for iOS that have a Widget for notification center, but I don't know how I should send and receive data (pass data) between View Controller and And Today Extension.
I tried to use structs, but it doesn't work, and also I used app groups but I don't want to use this method.
let shared = NSUserDefaults(suiteName: "group.Demo.Share-Extension-Demo.mahdi")
shared?.setObject("Hello", forKey: "kkk")
Apart from NSUserDefaults, you can use the NSNotificationCenter to send or receive data anywhere.
You need to set the observer where you can receive the data like below:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "dataReceived:", name: "SpecialKey", object: nil)
}
Funciton to catch data:
func dataReceived(notification: NSNotification) {
//deal with notification.userInfo
println(notification.userInfo)
println("Received Data")
}
And you need to define NSNotificationCenter from where you need to send the data:
NSNotificationCenter.defaultCenter().postNotificationName("SpecialKey", object: nil, userInfo: ["MyData": "Demo"])
References:
The complete guide to NSNotificationCenter
Hope it helps!
http://moreindirection.blogspot.in/2014/08/nsnotificationcenter-swift-and-blocks.html
For the people who haven't found a way to implement calling function or button click from App Extension (Widget):
Note: This is using Swift
Note 2: Replace the names of NSNotification and methods with your implementations
First, create NotificationCenter post method (In Swift 2.0 - NSNotification Center)
Create methods in App Delegate class -
var scheme: String!
var host: String!
Then, add the following function on the bottom of the class (after the last one):
func application(_ app: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
scheme = url.scheme
host = url.host
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NOTIFICATION_NAME"), object: nil)
return true
}
In your ViewController class, where you want to execute the function or the statement from clicking Widget, add the following in super.viewDidLoad():
NotificationCenter.default.addObserver(self,selector: #selector(self.YOUR_METHOD_NAME),
name: NSNotification.Name(rawValue: "NOTIFICATION_NAME"),
object: nil)
And the method you want to call:
func YOUR_METHOD_NAME(notification: NSNotification) {
let appDelegate =
UIApplication.shared.delegate as! AppDelegate
if appDelegate.scheme != nil {
startRecording()
}
}
I'm assuming that you have already created your widget target, and the view for it. Add this to your button in TodayViewController from which you want to handle the click:
#IBAction func openApp(_ sender: UIButton) {
openApp()
}
And the function to handle opening app by URl Scheme:
func openApp(){
let myAppUrl = NSURL(string: "YOUR_URL_SCHEME://YOUR_HOST_NAME")!
extensionContext?.open(myAppUrl as URL, completionHandler: { (success) in
if (!success) {
self.textView.text = "There was a problem opening app!"
}
})
}
For YOUR_URL_SCHEME, add your scheme that you have specified in Info.plist, if your don't, go to this link and follow instructions:
Add URL Scheme to Xcode
For YOUR_HOST_NAME, you can remove this, and only open app by URL Scheme.
Happy coding!

Resources