我一直在网上跟随很多教程学习如何设置复杂性。我没有问题按预期设置复杂性。
直到最初的时间轴条目过期。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))
}
这就是我遇到问题的地方。
如何创建新的数据并重新加载时间轴?流程是什么?我不是试图扩展时间轴,而是要完全替换它。我完全不知道该怎么做。苹果文档在这一点上相当模糊。我知道需要实现以下方法,但不知道具体如何操作。有人能帮我填写这段代码吗?
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的帮助,我已经解决了问题。我需要在requestedUpdateDidBegin中添加一个CLKComplicationServer实例,并将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
中获取,但实际上没有获取任何数据。我在这里卡住了:https://dev59.com/UpTfa4cB1Zd3GeqPPFFT - SRMR