【发布时间】:2016-01-03 13:14:46
【问题描述】:
我一直在关注互联网上的很多教程来学习如何设置复杂功能。按预期设置并发症我没有问题。
直到初始时间线条目过期。 12 小时后,我不知道如何更新它以保持并发症的存在。我将在下面分享我拥有的所有内容,希望有人可以帮助我填写。
在这里,我为要在复杂功能上显示的数据创建变量。
struct data = {
var name: String
var startString: String
var startDate: NSDate
}
以下数组是该数据的容器。
var dataArray = [data]
这允许在手表锁定时显示复杂功能。
func getPrivacyBehaviorForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationPrivacyBehavior) -> Void) {
handler(.ShowOnLockScreen)
}
这允许在复杂功能上进行时间旅行。
func getSupportedTimeTravelDirectionsForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimeTravelDirections) -> Void) {
handler([.Forward])
}
这里,我将时间线的开始时间设置为等于现在。
func getTimelineStartDateForComplication(complication: CLKComplication, withHandler handler: (NSDate?) -> Void) {
handler(NSDate())
}
在这里,我将时间线的结束时间设置为等于从现在开始的 12 小时。
func getTimelineEndDateForComplication(complication: CLKComplication, withHandler handler: (NSDate?) -> Void) {
handler(NSDate(timeIntervalSinceNow: (60 * 60 * 12)))
}
在这里,我创建了并发症的模板。这是为了在用户浏览手表上的所有并发症时看到我的并发症时向他们显示示例数据。
func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
let headerTextProvider = CLKSimpleTextProvider(text: "Some Data")
let body1TextProvider = CLKSimpleTextProvider(text: "Some Data Time")
let template = CLKComplicationTemplateModularLargeStandardBody()
template.headerTextProvider = headerTextProvider
template.body1TextProvider = body1TextProvider
handler(template)
}
这将为并发症创建第一个时间线条目。启用复杂功能后,此代码将运行并立即相应地填充复杂功能。
func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimelineEntry?) -> Void) {
createData()
if complication.family == .ModularLarge {
if dataArray.count != 0 {
let firstData = dataArray[0]
let headerTextProvider = CLKSimpleTextProvider(text: firstData.name)
let body1TextProvider = CLKSimpleTextProvider(text: firstData.startString)
let template = CLKComplicationTemplateModularLargeStandardBody()
template.headerTextProvider = headerTextProvider
template.body1TextProvider = body1TextProvider
let timelineEntry = CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: template)
handler(timelineEntry)
} else {
let headerTextProvider = CLKSimpleTextProvider(text: "No Data")
let body1TextProvider = CLKSimpleTextProvider(text: "Create some data")
let template = CLKComplicationTemplateModularLargeStandardBody()
template.headerTextProvider = headerTextProvider
template.body1TextProvider = body1TextProvider
let timelineEntry = CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: template)
handler(timelineEntry)
}
} else {
handler(nil)
}
}
这是我为我目前拥有的所有数据创建时间线条目的地方。
func getTimelineEntriesForComplication(complication: CLKComplication, afterDate date: NSDate, limit: Int, withHandler handler: ([CLKComplicationTimelineEntry]?) -> Void) {
createData()
var entries = [CLKComplicationTimelineEntry]()
for dataObject in dataArray {
if entries.count < limit && data.startDate.timeIntervalSinceDate(date) > 0 {
let headerTextProvider = CLKSimpleTextProvider(text: dataObject.name)
let body1TextProvider = CLKSimpleTextProvider(text: dataObject.startString)
let template = CLKComplicationTemplateModularLargeStandardBody()
template.headerTextProvider = headerTextProvider
template.body1TextProvider = body1TextProvider
let timelineEntry = CLKComplicationTimelineEntry(date: NSDate(timeInterval: (-10*60), sinceDate: data.startDate), complicationTemplate: template)
entries.append(timelineEntry)
}
}
handler(entries)
}
这告诉手表何时更新并发症数据。
func getNextRequestedUpdateDateWithHandler(handler: (NSDate?) -> Void) {
handler(NSDate(timeIntervalSinceNow: 60 * 60 * 6))
}
这是我遇到问题的地方。
如何创建新数据并重新加载时间线?什么是流量?我不是要延长时间线,而是要完全取代它。我完全不知所措。 Apple's docs 在这一点上非常模糊。我知道我需要实现以下方法,但我不知道如何。有人可以帮我填写此代码吗?
func requestedUpdateDidBegin() {
createData() //I assume createData() goes here? If so, how do I populate the new timeline entries based on the results?
}
func requestedUpdateBudgetExhausted() {
//This can't possibly be the case as I haven't gotten it to work once.
}
func reloadTimelineForComplication(complication: CLKComplication!) {
//This method appears to do nothing.
}
更新:
多亏了 El Tea,我才能正常工作。我需要将 CLKComplicationServer 的实例添加到 requestedUpdateDidBegin 并将 reloadTimeline 方法放入其中。
这是更新后的代码:
func requestedUpdateDidBegin() {
print("Complication update is starting")
createData()
let server=CLKComplicationServer.sharedInstance()
for comp in (server.activeComplications) {
server.reloadTimelineForComplication(comp)
print("Timeline has been reloaded!")
}
}
func requestedUpdateBudgetExhausted() {
print("Budget exhausted")
}
【问题讨论】:
-
曾经在
ComplicationController方面遇到问题,您似乎无法通过WCSession:获取您在ExtensionDelegate中设置的数据?基本上在你的createData()函数中的任何代码中,如果它是通过let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate从ExtensionDelegate中提取的,但实际上并没有提取任何数据。我被困在这里:stackoverflow.com/questions/35542729/…
标签: apple-watch watchos-2 apple-watch-complication clockkit