NativeScript / ios-jsc

NativeScript for iOS using JavaScriptCore
http://docs.nativescript.org/runtimes/ios
Apache License 2.0
298 stars 59 forks source link

Subclass UIApplication #1170

Closed NickIliev closed 5 years ago

NickIliev commented 5 years ago

@Lelelo1 commented on Wed Jul 03 2019

Some of us would like to be able to subclass UIApplication so we can override the sendEvent method on ios. This is needed to detect user events that occur anywhere in the application like on the keyboard and without having to attach listeners everywhere - to each and every one of the controls.

Describe the solution you'd like Just like we can implement UIApplicationDelegate:

class MyDelegate extends UIResponder implements UIApplicationDelegate {
    public static ObjCProtocols = [UIApplicationDelegate];

    touchesBeganWithEvent(touches: NSSet<UITouch>, event: _UIEvent): void {
        console.log("touches began");
        // will not be invoken when touch is on keybaord or is on a control
    }
}
application.ios.delegate = MyDelegate;

...described here

It would be nice if we could:

class MyApplication extends UIApplication {
    sendEvent(event: _UIEvent) {
        console.log("event was: " + event);
    }
}
application.ios.nativeApp = MyApplication; // <-- now throwing TypeError: Attempted to assign to readonly property.

before running app application.run({ moduleName: "app-root"});.

Describe alternatives you've considered Add an additional property in application.ios that contains the UIApplication subclass to be used if given.

Additional context

From application.ios.js inside application module:

function _start(entry) {
    mainEntry = typeof entry === "string" ? { moduleName: entry } : entry;
    started = true;
    if (!iosApp.nativeApp) {
        UIApplicationMain(0, null, null, iosApp && iosApp.delegate ? NSStringFromClass(iosApp.delegate) : NSStringFromClass(Responder));
    }

The third argument should probably be NSStringFromClass(iosApp.nativeApp), and the if(!iosApp.nativeApp) check has to redesigned.

UIApplicationMain where principalClassName should be the name of the UIApplication subclass if given.

NickIliev commented 5 years ago

Issue moved to NativeScript/NativeScript #7461 via ZenHub