Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

ios - Force reload watchOS 2 Complications

I have issues getting Complications to work. It would be helpful if I was able to reliably refresh them.

Therefore I linked a force-press menu button to the following method

@IBAction func updateComplication() {
    let complicationServer = CLKComplicationServer.sharedInstance()
    for complication in complicationServer.activeComplications {
        complicationServer.reloadTimelineForComplication(complication)
    }        
}

Unfortunately this leads to the app crashing. with a fatal error: unexpectedly found nil while unwrapping an Optional value.

I understand that calling reloadTimelineForComplication(complication) is budgeted but that can't be the issue here as it doesn't work from the very beginning.

I am currently using watchOS2 + Xcode 7 GM

I'd appreciate any ideas on making Complications refresh while the app is running?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Trace or use the exception breakpoint and focus on reading the whole error message where it tells you exactly on which line it found the nil unexpectedly (I do suspect the complicationServer). Use 'if let' instead of 'let' to force unwrap the respective variable.

private func reloadComplications() {        
    if let complications: [CLKComplication] = CLKComplicationServer.sharedInstance().activeComplications {
        if complications.count > 0 {
            for complication in complications {
                CLKComplicationServer.sharedInstance().reloadTimelineForComplication(complication)
                NSLog("Reloading complication (complication.description)...")
            }
            WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Click) // haptic only for debugging
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...