This repository was archived by the owner on Oct 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAppDelegate.swift
91 lines (78 loc) · 2.63 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// AppDelegate.swift
// SampleRecipe
//
// Created by toshi0383 on 12/28/15.
// Copyright © 2015 toshi0383. All rights reserved.
//
import UIKit
import TVMLKitchen
import JavaScriptCore
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, TVApplicationControllerDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
_ = prepareMyKitchen(launchOptions, delegate: self)
return true
}
// Custom TVApplicationControllerDelegate
func appController(_ appController: TVApplicationController, evaluateAppJavaScriptIn jsContext: JSContext) {
print("\(#function): hello")
}
}
private func prepareMyKitchen(_ launchOptions: [AnyHashable: Any]?, delegate: TVApplicationControllerDelegate) -> Bool
{
let cookbook = Cookbook(launchOptions: launchOptions)
cookbook.evaluateAppJavaScriptInContext = {appController, jsContext in
/// set Exception handler
/// called on JS error
jsContext.exceptionHandler = {context, value in
#if DEBUG
debugPrint(context)
debugPrint(value)
#endif
assertionFailure("You got JS error. Check your javascript code.")
}
// - SeeAlso: http://nshipster.com/javascriptcore/
}
cookbook.actionIDHandler = {
actionID in
print("actionID: \(actionID)")
}
cookbook.playActionIDHandler = {
playActionID in
print("playActionID: \(playActionID)")
}
cookbook.httpHeaders = [
"hello": "world"
]
cookbook.responseObjectHandler = { response in
/// Save cookies
if let fields = response.allHeaderFields as? [String: String],
let url = response.url
{
let cookies = HTTPCookie.cookies(withResponseHeaderFields: fields, for: url)
for c in cookies {
HTTPCookieStorage.sharedCookieStorage(
forGroupContainerIdentifier: "group.jp.toshi0383.tvmlkitchen.samplerecipe").setCookie(c)
}
}
return true
}
Kitchen.prepare(cookbook, delegate: delegate)
openViewController()
return true
}
struct SearchTab: TabItem {
let title = "Search"
func handler() {
let search = MySearchRecipe(type: .tabSearch)
Kitchen.serve(recipe: search)
}
}
private func openViewController() {
let sb = UIStoryboard(name: "ViewController", bundle: Bundle.main)
let vc = sb.instantiateInitialViewController()!
Kitchen.navigationController.pushViewController(vc, animated: true)
}