3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[tvOS]Scala + PlayでtvOSアプリを作ってみる覚書

Posted at

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アプリを新規作成。

スクリーンショット 2015-11-28 16.21.49.png

以下のファイルを修正&追加。

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 にブラウザでアクセスすることで、サーバー側のデバッグが割と楽に出来る。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?