How to use both native and TVML in a tvOS app?

2019-06-01 06:49发布

问题:

Is it possible to use both native views/controllers and TVML in a tvOS app? It seems that using TVML requires you to set an "app controller" instead of a normal view controller. How can you use both TVML and native components in a tvOS app?

回答1:

Yes, you can use both Native and TVML in the same app. You can register your class object in evaluateAppJavaScriptInContext method of App Delegate.

func appController(appController: TVApplicationController, evaluateAppJavaScriptInContext jsContext: JSContext)
    {

        jsContext.setObject(TestClass(), forKeyedSubscript: "testClassObj")
    }

Your TestClass should adopt the TestClassExport Protocol. (My TestClass is in Objective C. you can write it in Swift also.)

@protocol TestClassExport <JSExport>

- (NSString*)log:(NSString*)string;

@end

@interface TestClass : NSObject <TestClassExport>

-(NSString*)log:(NSString*)string;

@end

Now you can call the Test Class Log method from Javascript:

testClassObj.log('Call from JS');

If you want to display any controller then you can implement the method which will push the Controller in the TVApplicationController.

[_tvAppController.navigationController pushViewController:controller animated:YES completion:nil];

Check this link for more details- https://forums.developer.apple.com/thread/18430



标签: tvos