[tvOS]Scala + Play에서 tvOS 앱을 만들어 보는 각서

Xcode측



File>New>Project에서 tvOS의 Single View Application을 신규 작성.
다음 파일을 수정.

AppDelegate.swift
import UIKit
import TVMLKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, TVApplicationControllerDelegate {
    var window: UIWindow?
    var appController: TVApplicationController?

    static let TVBaseURL = "http://localhost:9000/"
    static let TVBootURL = "\(AppDelegate.TVBaseURL)assets/javascripts/application.js"

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        window = UIWindow(frame: UIScreen.mainScreen().bounds)

        let appControllerContext = TVApplicationControllerContext()

        if let javaScriptURL = NSURL(string: AppDelegate.TVBootURL) {
            appControllerContext.javaScriptApplicationURL = javaScriptURL
        }

        appControllerContext.launchOptions["BASEURL"] = AppDelegate.TVBaseURL

        if let launchOptions = launchOptions as? [String: AnyObject] {
            for (kind, value) in launchOptions {
                appControllerContext.launchOptions[kind] = value
            }
        }

        appController = TVApplicationController(context: appControllerContext, window: window, delegate: self)

        return true
    }

    // 略
}

서버측


play new client play-scala 에서 새로운 Play 앱.



다음 파일을 수정 & 추가.

public/javascripts/application.js
function getDocument(url) {
    var templateXHR = new XMLHttpRequest();
    templateXHR.responseType = "document";
    templateXHR.addEventListener("load", function() {pushDoc(templateXHR.responseXML);}, false);
    templateXHR.open("GET", url, true);
    templateXHR.send();
    return templateXHR;
}

function pushDoc(document) {
    navigationDocument.pushDocument(document);
}

App.onLaunch = function(options) {
    var url = `${options.BASEURL}alert.tvml`; // 最初に表示するURLを設定。
    getDocument(url);
}

app/controllers/Application.scala
package controllers

import play.api._
import play.api.mvc._

class Application extends Controller {

  def index = Action {
    Ok(views.html.index("Your new application is ready."))
  }

  def alert = Action {
    Ok(views.html.alert("Alert Test."))
  }

}

app/views/alertTemplate.scala.html
@(title: String)(xml: Html)

<document>
    <alertTemplate>
        <title>@title</title>
        @xml
    </alertTemplate>
</document>

app/views/alert.scala.html
@(message: String)

@alertTemplate("Welcome to Play") {
    <description>@message</description>
    <button>
        <text>OK</text>
    </button>
}

conf/routes
# Home page
GET     /                           controllers.Application.index

# Tvml
GET     /alert.tvml                 controllers.Application.alert

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)

디버깅


play run 한 후에,
http://localhost:9000/alert.tvml 에 브라우저로 액세스 하는 것으로, 서버측의 디버그를 비교적 편하게 할 수 있다.

좋은 웹페이지 즐겨찾기