The application.ios.addNotificationObserver method now returns an observer object which you should supply as the first argument when calling app.ios.removeNotificationObserver. (Fixed #473)

This commit is contained in:
Rossen Hristov
2015-09-01 12:34:20 +03:00
parent 007360fc76
commit 7a2e41b1b3
6 changed files with 25 additions and 36 deletions

View File

@@ -512,14 +512,15 @@ declare module "application" {
* @param notificationName A string containing the name of the notification.
* @param onReceiveCallback A callback function that will be called each time the observer receives a notification.
*/
addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void): void;
addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void): any;
/**
* Removes the observer for the specified notification from the default notification center.
* For more information, please visit 'https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/#//apple_ref/occ/instm/NSNotificationCenter/addObserver:selector:name:object:'
* @param observer The observer that was returned from the addNotificationObserver method.
* @param notificationName A string containing the name of the notification.
* @param onReceiveCallback A callback function that will be called each time the observer receives a notification.
*/
removeNotificationObserver(notificationName: string): void;
removeNotificationObserver(observer: any, notificationName: string): void;
}
}

View File

@@ -37,14 +37,14 @@ class Window extends UIWindow {
}
}
class NotificationReceiver extends NSObject {
class NotificationObserver extends NSObject {
private _onReceiveCallback: (notification: NSNotification) => void;
static new(): NotificationReceiver {
return <NotificationReceiver>super.new();
static new(): NotificationObserver {
return <NotificationObserver>super.new();
}
public initWithCallback(onReceiveCallback: (notification: NSNotification) => void): NotificationReceiver {
public initWithCallback(onReceiveCallback: (notification: NSNotification) => void): NotificationObserver {
this._onReceiveCallback = onReceiveCallback;
return this;
}
@@ -62,7 +62,6 @@ class IOSApplication implements definition.iOSApplication {
public rootController: any;
private _delegate: typeof UIApplicationDelegate;
private _registeredObservers = {};
private _currentOrientation = UIDevice.currentDevice().orientation;
private _window: Window;
@@ -88,17 +87,14 @@ class IOSApplication implements definition.iOSApplication {
}
}
public addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void) {
var observer = NotificationReceiver.new().initWithCallback(onReceiveCallback);
public addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void): NotificationObserver {
var observer = NotificationObserver.new().initWithCallback(onReceiveCallback);
NSNotificationCenter.defaultCenter().addObserverSelectorNameObject(observer, "onReceive", notificationName, null);
this._registeredObservers[notificationName] = observer;
return observer;
}
public removeNotificationObserver(notificationName: string) {
var observer = this._registeredObservers[notificationName];
if (observer) {
NSNotificationCenter.defaultCenter().removeObserverNameObject(observer, notificationName, null);
}
public removeNotificationObserver(observer: any, notificationName: string) {
NSNotificationCenter.defaultCenter().removeObserverNameObject(observer, notificationName, null);
}
private didFinishLaunchingWithOptions(notification: NSNotification) {