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