使用iPhone数据进行复杂时间线更新

时间:2015-09-17 17:47:24

标签: ios swift watch-os-2 apple-watch-complication

我正在尝试为watchOS 2 GM编写一个复杂功能,它使用WCSession显示从iPhone(iOS 9 GM)获取的值。

不幸的是,发送邮件时出现以下错误:

Error Domain=WCErrorDomain Code=7014 "Payload could not be delivered." UserInfo={NSLocalizedDescription=Payload could not be delivered.}

这是我的代码在ComplicationController.swift中的样子:

import ClockKit
import WatchConnectivity

class ComplicationController: NSObject, CLKComplicationDataSource,WCSessionDelegate {

// MARK: - Timeline Configuration

var session : WCSession.defaultSession()
var myValue : Int?

...

func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: ((CLKComplicationTimelineEntry?) -> Void)) {

    getInfo()

    if self.myValue != nil {
        if complication.family == .CircularSmall {
            let template = CLKComplicationTemplateCircularSmallRingText()
            template.textProvider = CLKSimpleTextProvider(text: "\(self.myValue)")
            template.fillFraction = Float(self.myValue!) / 100
            template.ringStyle = CLKComplicationRingStyle.Closed

            let timelineEntry = CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: template)
            handler(timelineEntry)
        } else {
            handler(nil)
        }
    }

}

func requestedUpdateDidBegin(){
   getInfo()
}

// MARK: - Update Scheduling

func getNextRequestedUpdateDateWithHandler(handler: (NSDate?) -> Void) {
    // Call the handler with the date when you would next like to be given the opportunity to update your complication content
    handler(NSDate(timeIntervalSinceNow: 5)); // only that low for debugging
}

func getInfo(){
        if (WCSession.defaultSession().reachable) {

            let messageToSend = ["Value":"Info"]
            session.sendMessage(messageToSend, replyHandler: { replyMessage in
                //handle and present the message on screen
                let value:[String:AnyObject] = replyMessage

                if value.indexForKey("myValue") != nil{
                    self.myValue = value["myValue"]! as? Int
                    print("Value: \(self.myValue)")
                }

             }, errorHandler: {error in
                    // catch any errors here
                    print(error)  
            })
        }
}

这是我的ExtensionDelegate.swift:

import WatchKit
import WatchConnectivity

class ExtensionDelegate: NSObject, WKExtensionDelegate,WCSessionDelegate {
var session:WCSession!

func applicationDidFinishLaunching() {
    // Perform any final initialization of your application.
    if (WCSession.isSupported()) {
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }
}

...

最后我的iOS AppDelegate:

import UIKit
import WatchConnectivity

class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {

var window: UIWindow?
var myDevice: UIDevice?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    if (WCSession.isSupported()) {
        let session = WCSession.defaultSession()
        session.delegate = self // conforms to WCSessionDelegate
        session.activateSession()
    }

    application.statusBarStyle = UIStatusBarStyle.LightContent

    return true

}

func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
    var reply = [String:AnyObject]()

    // some logic
    let value = //some Int value
    reply.updateValue(value, forKey: "myValue")

    replyHandler(reply)
}

有什么想法吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

有些事情可以帮助您进行设置,以便更新并发症。

通常,您希望在调用CLKComplicationDataSource方法时,您的时间轴数据已经可用于这些点。 (并不总是很容易)。

看起来你的ComplicationController和ExtensionDelegate都被用作WCSessionDelegates。在手表上的一个地方(可能是ExtensionDelegate)而不是另一个地方使用它。

您已将AppDelegate设置为响应消息,但该didReceiveMessage方法处理的任何消息都只会来自您的观看。

确定您的邮件最初来自哪里(可能是外部通知?),并通过WCSession'发送'将该信息作为字典发送到手表。方法

让你的ExtensionDelegate(或任何响应WCSessionDelegate方法的人)回复相应的' receive'捕获已发送信息的方法。

然后:让CLKComplicationServer重新加载你的时间线,开始刷新你的时间线。