mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Resolved Issue #451: Improve the Network Stack
Resolved Issue #473: Add support for Notification Observers (iOS) and Broadcast Receivers (Android)
This commit is contained in:
@@ -121,11 +121,33 @@ class TNSAppDelegate extends UIResponder implements UIApplicationDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
class NotificationReceiver extends NSObject {
|
||||
private _onReceiveCallback: (notification: NSNotification) => void;
|
||||
|
||||
static new(): NotificationReceiver {
|
||||
return <NotificationReceiver>super.new();
|
||||
}
|
||||
|
||||
public initWithCallback(onReceiveCallback: (notification: NSNotification) => void): NotificationReceiver {
|
||||
this._onReceiveCallback = onReceiveCallback;
|
||||
return this;
|
||||
}
|
||||
|
||||
public onReceive(notification: NSNotification): void {
|
||||
this._onReceiveCallback(notification);
|
||||
}
|
||||
|
||||
public static ObjCExposedMethods = {
|
||||
"onReceive": { returns: interop.types.void, params: [NSNotification] }
|
||||
};
|
||||
}
|
||||
|
||||
class IOSApplication implements definition.iOSApplication {
|
||||
|
||||
public nativeApp: any;
|
||||
public rootController: any;
|
||||
private _tnsAppdelegate: TNSAppDelegate;
|
||||
private _registeredObservers = {};
|
||||
|
||||
constructor() {
|
||||
// TODO: in iOS there is the singleton instance, while in Android such does not exist hence we pass it as argument
|
||||
@@ -135,6 +157,19 @@ class IOSApplication implements definition.iOSApplication {
|
||||
public init() {
|
||||
this._tnsAppdelegate = new TNSAppDelegate();
|
||||
}
|
||||
|
||||
public addNotificationObserver(notificationName: string, onReceiveCallback: (notification: NSNotification) => void) {
|
||||
var observer = NotificationReceiver.new().initWithCallback(onReceiveCallback);
|
||||
NSNotificationCenter.defaultCenter().addObserverSelectorNameObject(observer, "onReceive", notificationName, null);
|
||||
this._registeredObservers[notificationName] = observer;
|
||||
}
|
||||
|
||||
public removeNotificationObserver(notificationName: string) {
|
||||
var observer = this._registeredObservers[notificationName];
|
||||
if (observer) {
|
||||
NSNotificationCenter.defaultCenter().removeObserverNameObject(observer, notificationName, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: If we have nested require(application) calls we may enter unfinished module state, which will create two delegates, resulting in an exception
|
||||
|
||||
Reference in New Issue
Block a user