How to retrieve values from settings.bundle in TVML?

坚强是说给别人听的谎言 提交于 2019-12-11 11:18:10

问题


In my tvOS app I created a settings bundle, but I don't know how to get the values in TVML. I know how to do it in Obj-c or Swift.

var standardUserDefaults = NSUserDefaults.standardUserDefaults()
var us: AnyObject? = standardUserDefaults.objectForKey("your_preference")
if us==nil {
    self.registerDefaultsFromSettingsBundle();
}

Any ideas about TVML way? Any kind of help is highly appreciated.


回答1:


I am not aware of a direct TVJS access method... but you could easily set up a Swift/Obj-C-"proxy". Somewhat along those lines:

AppDelegate.swift

func appController(appController: TVApplicationController, evaluateAppJavaScriptInContext jsContext: JSContext) {
    let jsInterface: cJsInterface = cJsInterface();
    jsContext.setObject(jsInterface, forKeyedSubscript: "swiftInterface")
}

JsInterface.swift

@objc protocol jsInterfaceProtocol : JSExport {
    func getSetting(setting: String) -> String
}
class cJsInterface: NSObject, jsInterfaceProtocol {
    func getSetting(setting: String) -> String {
        return "<yourSetting>"
    }
}

on the JS side...

swiftInterface.getSetting(...)


来源:https://stackoverflow.com/questions/35384084/how-to-retrieve-values-from-settings-bundle-in-tvml

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