问题
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