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

时光怂恿深爱的人放手 提交于 2020-01-02 20:45:52

问题


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



来源:https://stackoverflow.com/questions/37765886/how-to-use-both-native-and-tvml-in-a-tvos-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!