WCSession を使用して iPhone (iOS 9 GM) から取得した値を表示する、watchOS 2 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)
}
何か案は?
前もって感謝します!