天道酬勤,学无止境

watchos-3

WCErrorCodeDeliveryFailed:无法交付有效负载(WCErrorCodeDeliveryFailed: Payload could not be delivered)

问题 我正在使用WCSession方法sendMessage:replyHandler:errorHandler: 实施该方法后,我收到如下错误: WCSession _onqueue_notifyOfMessageError:withErrorHandler: errorHandler: YES with WCErrorCodeDeliveryFailed。 错误 = 无法交付有效负载。 import Foundation import WatchKit import WatchConnectivity class ResultInterfaceController: WKInterfaceController, WCSessionDelegate { override func awake(withContext context: Any?) { super.awake(withContext: context) let applicationData = ["name": "ViratKohli"] self.sendToPhone(data: applicationData) } func sendToPhone(data: [String: Any]) { if WCSession.isSupported() { let session = WCSession

2022-01-15 00:29:29    分类:技术分享    ios   swift   watchos-3   watchconnectivity   wcsession

WCErrorCodeDeliveryFailed: Payload could not be delivered

I'm working on an app that share data between iPhone and Apple Watch, using WCSession method sendMessage:replyHandler:errorHandler: After implementing that method I get the error like: WCSession _onqueue_notifyOfMessageError:withErrorHandler: errorHandler: YES with WCErrorCodeDeliveryFailed. Error = Payload could not be delivered. import Foundation import WatchKit import WatchConnectivity class ResultInterfaceController: WKInterfaceController, WCSessionDelegate { override func awake(withContext context: Any?) { super.awake(withContext: context) let applicationData = ["name": "ViratKohli"] self

2022-01-15 00:05:27    分类:问答    ios   swift   watchos-3   watchconnectivity   wcsession

swift如何在后台在Watch和iPhone之间共享数据(swift how to share data between Watch and iPhone on background)

问题 我有一个用于在 iPhone 和 Watch 之间共享数据(共享文本)的功能应用程序,即使手表设置在后台,我也想让它工作(当 Watch 在后台时,将数据从 iPhone 发送到 Watch)。 我读了很多关于如何制作它的文章,但我的应用程序似乎没有任何问题。 请添加代码以使应用程序正常工作,如我之前所说。 或者给我一些适合这个应用程序的资源。 谢谢! iPhone 的代码: import UIKit import WatchConnectivity class ViewController: UIViewController, WCSessionDelegate { @IBOutlet weak var iPhoneLabel: UILabel! var session : WCSession!; func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { } func sessionDidBecomeInactive(_ session: WCSession) { } func sessionDidDeactivate(_ session: WCSession) { } func

2022-01-14 22:23:01    分类:技术分享    ios   swift   background   watchos-3   watchconnectivity

当 WKExtension.scheduleBackgroundRefresh 应该调用 scheduleCompletion 处理程序?(When WKExtension.scheduleBackgroundRefresh is supposed to call scheduledCompletion handler?)

问题 我正在尝试使用这样的行来安排后台任务: WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: Date(timeIntervalSinceNow: TimeInterval(5) * 60), userInfo: nil, scheduledCompletion: self.scheduledCompletion) 在哪里 func scheduledCompletion(error: Error?) { if error == nil { print("successfully scheduled application background refresh") } else { print("error scheduling background refresh, error: \(error)") } } 根据文档: scheduledCompletion后台应用刷新任务完成后系统调用的块。 但由于未知原因,在安排后台刷新任务后立即调用它。 后台刷新任务在正确的时间被调用,tho,并且在此之后没有调用scheduledCompletion 。 那么这是文档中的错误,WatchKit 中的错误还是我做错了什么? 回答1 尽管有文档描述

2022-01-14 10:29:34    分类:技术分享    swift3   watchos-3   wkrefreshbackgroundtask

URLSessionTask 运行时总是出错(URLSessionTask runs always into an error)

问题 在我的 ExtensionDelegate 中,我使用以下代码启动 URLSessionTask: func scheduleNextURLSessionTask() { let backgroundConfigObject = URLSessionConfiguration.background(withIdentifier: "myIdentifier") let backgroundSession = URLSession(configuration: backgroundConfigObject, delegate: self, delegateQueue: nil) let retrieveTask = backgroundSession.dataTask(with: URL(string: "https://api.wedtec.net/cryptocoins/index.php?bitcoin&simple")!) retrieveTask.resume() } 当然,我的 ExtensionDelegate 实现了 URLSessionDataDelegate。 这总是会出错。 方法 func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error

2022-01-14 05:07:09    分类:技术分享    xcode   swift3   watchos-3

When WKExtension.scheduleBackgroundRefresh is supposed to call scheduledCompletion handler?

I'm trying to schedule background task with such line: WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate: Date(timeIntervalSinceNow: TimeInterval(5) * 60), userInfo: nil, scheduledCompletion: self.scheduledCompletion) where func scheduledCompletion(error: Error?) { if error == nil { print("successfully scheduled application background refresh") } else { print("error scheduling background refresh, error: \(error)") } } According to documentation: scheduledCompletion A block that is called by the system after the background app refresh task has completed. But by unknown reason it

2022-01-11 20:16:47    分类:问答    swift3   watchos-3   wkrefreshbackgroundtask

swift how to share data between Watch and iPhone on background

I have a functional application for sharing the data between iPhone and Watch (share text) and I want to make it work even when the watch is set on background (send data from iPhone to Watch when Watch is on background). I read a lot about how to make this but nothing seemd to be ok for my application. Please add the code to make application work as I said before. Or give me some source which fit with this app. Thank you! Code for iPhone: import UIKit import WatchConnectivity class ViewController: UIViewController, WCSessionDelegate { @IBOutlet weak var iPhoneLabel: UILabel! var session

2022-01-10 22:45:29    分类:问答    ios   swift   background   watchos-3   watchconnectivity

URLSessionTask runs always into an error

In my ExtensionDelegate I'm starting a URLSessionTask with following code: func scheduleNextURLSessionTask() { let backgroundConfigObject = URLSessionConfiguration.background(withIdentifier: "myIdentifier") let backgroundSession = URLSession(configuration: backgroundConfigObject, delegate: self, delegateQueue: nil) let retrieveTask = backgroundSession.dataTask(with: URL(string: "https://api.wedtec.net/cryptocoins/index.php?bitcoin&simple")!) retrieveTask.resume() } My ExtensionDelegate implements URLSessionDataDelegate, of course. This runs always into an error. Means func urlSession(_ session

2022-01-10 03:01:20    分类:问答    xcode   swift3   watchos-3

WatchOS 3 中的本地通知(Local Notification in WatchOS 3)

问题 我正在使用 WatchOS 3 测试版并尝试在手表上启动本地通知。 界面只是一个按钮,它调用下面代码中的“buttonPushed”方法。 该应用程序运行良好,但我从未收到通知。 应用程序结构是 Xcode 8 中 WatchKit 应用程序的默认结构。 此代码位于 WatchKit 扩展的 InterfaceController.swift 文件中 我错过了一些完全明显的东西吗? @IBAction func buttonPushed() { sendMyNotification() } func sendMyNotification(){ if #available(watchOSApplicationExtension 3.0, *) { let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in // Enable or disable features based on authorization. } let content = UNMutableNotificationContent() content.title = NSString

2021-12-21 16:47:40    分类:技术分享    ios   notifications   watchos-3

Local Notification in WatchOS 3

I'm using the WatchOS 3 beta and trying to initiate a local notification on the watch. The interface is just one button which calls the "buttonPushed" method in the code below. The app runs fine but I never get a notification. The app structure is the default from Xcode 8 for a WatchKit app. This code is in the InterfaceController.swift file of the WatchKit extension Am I missing something totally obvious? @IBAction func buttonPushed() { sendMyNotification() } func sendMyNotification(){ if #available(watchOSApplicationExtension 3.0, *) { let center = UNUserNotificationCenter.current() center

2021-12-12 21:40:36    分类:问答    ios   notifications   watchos-3