我正在研究tvOS
,我发现苹果公司提供了一套很好的使用TVML
编写的模板。我想知道如果一个tvOS
应用程序,利用TVML
模板也可以使用UIKit
。
我可以混合UIKit和TVMLKit在一个应用程序?
我在苹果开发者论坛上发现了一个帖子,但它没有完全回答这个问题,我正在通过文档找到答案。
可以。显示TVML模板需要使用一个对象来控制JavaScript Context: TVApplicationController.
var appController: TVApplicationController?
该对象有一个与之关联的UINavigationController属性。当你觉得合适的时候,你可以调用:
let myViewController = UIViewController()
self.appController?.navigationController.pushViewController(myViewController, animated: true)
这允许你推送一个自定义UIKit视图控制器到导航堆栈。如果你想回到TVML模板,只要把viewController从导航堆栈中弹出就可以了。
如果你想知道如何在JavaScript和Swift之间进行通信,这里有一个方法,它创建了一个名为的JavaScript函数pushMyView()
func createPushMyView(){
//allows us to access the javascript context
appController?.evaluateInJavaScriptContext({(evaluation: JSContext) -> Void in
//this is the block that will be called when javascript calls pushMyView()
let pushMyViewBlock : @convention(block) () -> Void = {
() -> Void in
//pushes a UIKit view controller onto the navigation stack
let myViewController = UIViewController()
self.appController?.navigationController.pushViewController(myViewController, animated: true)
}
//this creates a function in the javascript context called "pushMyView".
//calling pushMyView() in javascript will call the block we created above.
evaluation.setObject(unsafeBitCast(pushMyViewBlock, AnyObject.self), forKeyedSubscript: "pushMyView")
}, completion: {(Bool) -> Void in
//done running the script
})
}
一旦你在Swift中调用了createPushMyView(),你就可以在javascript代码中调用pushMyView(),它就会把一个视图控制器推送到栈上。
SWIFT 4.1 UPDATE
只是对方法名和类型转换进行了一些简单的更改:
appController?.evaluate(inJavaScriptContext: {(evaluation: JSContext) -> Void in
和
evaluation.setObject(unsafeBitCast(pushMyViewBlock, to: AnyObject.self), forKeyedSubscript: "pushMyView" as NSString)
正如公认的答案所提到的,您可以在JavaScript上下文中调用几乎任何Swift函数。请注意,顾名思义,除了块之外,setObject:forKeyedSubscript:
还将接受对象(如果它们遵循继承自JSExport的协议),从而允许您访问该对象的方法和属性。下面是一个例子
import Foundation
import TVMLKit
// Just an example, use sessionStorage/localStorage JS object to actually accomplish something like this
@objc protocol JSBridgeProtocol : JSExport {
func setValue(value: AnyObject?, forKey key: String)
func valueForKey(key: String) -> AnyObject?
}
class JSBridge: NSObject, JSBridgeProtocol {
var storage: Dictionary<String, String> = [:]
override func setValue(value: AnyObject?, forKey key: String) {
storage[key] = String(value)
}
override func valueForKey(key: String) -> AnyObject? {
return storage[key]
}
}
然后在你的应用控制器中:
func appController(appController: TVApplicationController, evaluateAppJavaScriptInContext jsContext: JSContext) {
let bridge:JSBridge = JSBridge();
jsContext.setObject(bridge, forKeyedSubscript:"bridge");
}
然后你可以在JS中这样做:bridge.setValue(['foo', 'bar'], "baz")
不仅如此,您还可以覆盖现有元素的视图,或者定义标记中使用的自定义元素,并使用本地视图支持它们:
// Call lines like these before you instantiate your TVApplicationController
TVInterfaceFactory.sharedInterfaceFactory().extendedInterfaceCreator = CustomInterfaceFactory()
// optionally register a custom element. You could use this in your markup as <loadingIndicator></loadingIndicator> or <loadingIndicator /> with optional attributes. LoadingIndicatorElement needs to be a TVViewElement subclass, and there are three functions you can optionally override to trigger JS events or DOM updates
TVElementFactory.registerViewElementClass(LoadingIndicatorElement.self, forElementName: "loadingIndicator")
快速定制元素示例:
import Foundation
import TVMLKit
class LoadingIndicatorElement: TVViewElement {
override var elementName: String {
return "loadingIndicator"
}
internal override func resetProperty(resettableProperty: TVElementResettableProperty) {
super.resetProperty(resettableProperty)
}
// API's to dispatch events to JavaScript
internal override func dispatchEventOfType(type: TVElementEventType, canBubble: Bool, cancellable isCancellable: Bool, extraInfo: [String : AnyObject]?, completion: ((Bool, Bool) -> Void)?) {
//super.dispatchEventOfType(type, canBubble: canBubble, cancellable: isCancellable, extraInfo: extraInfo, completion: completion)
}
internal override func dispatchEventWithName(eventName: String, canBubble: Bool, cancellable isCancellable: Bool, extraInfo: [String : AnyObject]?, completion: ((Bool, Bool) -> Void)?) {
//...
}
}
下面是如何设置自定义接口工厂:
class CustomInterfaceFactory: TVInterfaceFactory {
let kCustomViewTag = 97142 // unlikely to collide
override func viewForElement(element: TVViewElement, existingView: UIView?) -> UIView? {
if (element.elementName == "title") {
if (existingView != nil) {
return existingView
}
let textElement = (element as! TVTextElement)
if (textElement.attributedText!.length > 0) {
let label = UILabel()
// Configure your label here (this is a good way to set a custom font, for example)...
// You can examine textElement.style or textElement.textStyle to get the element's style properties
label.backgroundColor = UIColor.redColor()
let existingText = NSMutableAttributedString(attributedString: textElement.attributedText!)
label.text = existingText.string
return label
}
} else if element.elementName == "loadingIndicator" {
if (existingView != nil && existingView!.tag == kCustomViewTag) {
return existingView
}
let view = UIImageView(image: UIImage(named: "loading.png"))
return view // Simple example. You could easily use your own UIView subclass
}
return nil // Don't call super, return nil when you don't want to override anything...
}
// Use either this or viewForElement for a given element, not both
override func viewControllerForElement(element: TVViewElement, existingViewController: UIViewController?) -> UIViewController? {
if (element.elementName == "whatever") {
let whateverStoryboard = UIStoryboard(name: "Whatever", bundle: nil)
let viewController = whateverStoryboard.instantiateInitialViewController()
return viewController
}
return nil
}
// Use this to return a valid asset URL for resource:// links for badge/img src (not necessary if the referenced file is included in your bundle)
// I believe you could use this to cache online resources (by replacing resource:// with http(s):// if a corresponding file doesn't exist (then starting an async download/save of the resource before returning the modified URL). Just return a file url for the version on disk if you've already cached it.
override func URLForResource(resourceName: String) -> NSURL? {
return nil
}
}
不幸的是,view/viewControllerForElement:不会对所有元素调用。一些现有的元素(如集合视图)将处理它们的子元素本身的渲染,而不涉及你的接口工厂,这意味着你必须覆盖一个更高级别的元素,或者可能使用类别/swizzling或UIAppearance来获得你想要的效果。
最后,正如我刚才暗示的那样,你可以使用UIAppearance来改变某些内置视图的外观。下面是更改TVML应用程序标签栏外观的最简单方法,例如:
// in didFinishLaunching...
UITabBar.appearance().backgroundImage = UIImage()
UITabBar.appearance().backgroundColor = UIColor(white: 0.5, alpha: 1.0)
如果你已经有了tvOS的原生UIKit应用程序,但想通过使用TVMLKit来扩展它的某些部分,你可以。
使用TVMLKit作为原生tvOS应用程序中的子应用程序。下面的应用程序展示了如何做到这一点,通过保留TVApplicationController
并从TVApplicationController
中呈现navigationController
。TVApplicationControllerContext
用于将数据传输到JavaScript应用程序,因为url在这里传输:
class ViewController: UIViewController, TVApplicationControllerDelegate {
// Retain the applicationController
var appController:TVApplicationController?
static let tvBaseURL = "http://localhost:9001/"
static let tvBootURL = "(ViewController.tvBaseURL)/application.js"
@IBAction func buttonPressed(_ sender: UIButton) {
print("button")
// Use TVMLKit to handle interface
// Get the JS context and send it the url to use in the JS app
let hostedContContext = TVApplicationControllerContext()
if let url = URL(string: ViewController.tvBootURL) {
hostedContContext.javaScriptApplicationURL = url
}
// Save an instance to a new Sub application, the controller already knows what window we are running so pass nil
appController = TVApplicationController(context: hostedContContext, window: nil, delegate: self)
// Get the navigationController of the Sub App and present it
let navc = appController!.navigationController
present(navc, animated: true, completion: nil)
}
是。参见TVMLKit框架,它的文档以:
开头TVMLKit框架使您能够在二进制应用程序中合并JavaScript和TVML文件来创建客户端-服务器应用程序。
从这些文档的快速浏览,它看起来像你使用各种TVWhateverFactory
类从TVML创建UIKit视图或视图控制器,之后你可以将它们插入到UIKit应用程序。