実装に際しては、以下の記事を参考にしました。
コンプリケーションとは?と言う方は、こちらを見てからが良いかもしれません。
http://qiita.com/HIkaruSato/items/66e7857435bbcd4f75f9
Complicationとは
AppleWatchで時間を表示してる時、右下とかに表示されて、直ぐにアプリを起動できるあれです。ここから自分のアプリを起動しようものなら、きっと女子にもモテモテです。
目的:ComplicationControllerのサンプルを増やしたい
海外のサイトも含めてサンプルが少なかった(コンプリケーション弄っている人がそもそも少ない)ので、少しソースを公開。コメントで解説していくスタイルで
以下サンプル
ComplicationController.swift
import Foundation
import ClockKit
class ComplicationController: NSObject, CLKComplicationDataSource {
//complication.familyはプロジェクトで設定したものが落ちてくる
//TARGET -> WatchKit Extension -> General -> Conplications Configurationでチェックつけた奴が落ちてくる
//今回はConfigurationでチェックを付けていないものはnilで返すようにしている
func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimelineEntry?) -> Void) {
//ComplicationのTemplate これを関数の最後のhandlerに掴ませる
var template: CLKComplicationTemplate?
//他の例にならってswitchに
switch complication.family {
case .ModularSmall:
//こっちは小文字がイケる
let modularSmallTemp = CLKComplicationTemplateModularSmallSimpleText()
modularSmallTemp.textProvider = CLKSimpleTextProvider(text: "Test")
template = modularSmallTemp
case .UtilitarianSmall:
//こっちは大文字に統一されるっぽい
let modularSmallTempFlat = CLKComplicationTemplateUtilitarianSmallFlat()
modularSmallTempFlat.textProvider = CLKSimpleTextProvider(text: "Test")
template = modularSmallTempFlat
case .CircularSmall:
//画像を出すときはこんな感じ
let modularTemplate = CLKComplicationTemplateCircularSmallRingImage()
modularTemplate.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "32pxImage")!)
//パーセント表示を外殻の円で表示したい時
//modularTemplate.fillFraction = 0.0
//modularTemplate.ringStyle = .Closed
template = modularTemplate
case .ModularLarge:
template = nil
case .UtilitarianLarge:
template = nil
}
//タイムトラベル機能(いらない)を使う時などはdateを使うようですが今回はNSDate()で大丈夫そうです
let timelineEntry = CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: template!)
handler(timelineEntry)
}
//カスタマイズ画面のプレースホルダーの設定
//ほとんど本番と一緒
func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
var template: CLKComplicationTemplate?
switch complication.family {
case .ModularSmall:
let modularSmallTemp = CLKComplicationTemplateModularSmallSimpleText()
modularSmallTemp.textProvider = CLKSimpleTextProvider(text: "Test")
template = modularSmallTemp
case .UtilitarianSmall:
let modularSmallTempFlat = CLKComplicationTemplateUtilitarianSmallFlat()
modularSmallTempFlat.textProvider = CLKSimpleTextProvider(text: "Test")
template = modularSmallTempFlat
case .CircularSmall:
let modularTemplate = CLKComplicationTemplateCircularSmallRingImage()
modularTemplate.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "32pxImage")!)
template = modularTemplate
case .ModularLarge:
template = nil
case .UtilitarianLarge:
template = nil
}
//最後にhandlerに持たせるのはtemplateになる
handler(template)
}
}
上記の例だと、時間の表示とか全くしないので、ぶっちゃけ審査通るかは知りません(おい
他にもタイムトラベル機能~~(いらない)~~や、プライバシーへ配慮した情報の表示の有無の設定などがあるようですが、上記のあたりを書いておけば、コンプリケーション画面に自分が設定した値がとりあえず出てきますので、テンションが上がること請け合いです。