简体   繁体   English

尝试在屏幕关闭时让 Apple Watch 计时器应用程序在后台运行

[英]Trying to get Apple Watch timer app to run in background while the screen is off

I am trying to build a timing app for the watch that vibrates every minute just as a way for the user to keep track of time.我正在尝试为手表构建一个计时应用程序,它每分钟振动一次,作为用户跟踪时间的一种方式。 I have the timer correctly working and vibrating, but once the screen on the watch goes off, it no longer vibrates.我让计时器正常工作并振动,但是一旦手表上的屏幕熄灭,它就不再振动。 Once I wake the screen back up, it goes back to normal.一旦我唤醒屏幕,它就会恢复正常。

Right now to send the vibration I am using this:现在发送我正在使用的振动:

WKInterfaceDevice.currentDevice().playHaptic(.Stop)

I know this might be some sort of design implementation to save battery life, but obviously the point of my app is to keep the track of time even when the screen is off.我知道这可能是为了节省电池寿命的某种设计实现,但显然我的应用程序的重点是即使在屏幕关闭时也能跟踪时间。 Is something like this possible?这样的事情可能吗?

To keep a watchOS app running after the screen turns off it is now possible to use the new WKExtendedRuntimeSession commands offered in watchOS 6.为了在屏幕关闭后保持 watchOS 应用程序运行,现在可以使用 watchOS 6 中提供的新WKExtendedRuntimeSession命令。

Inside your WatchKit Extension ExtensionDelegate file create an extended runtime session with the following command.在您的 WatchKit Extension ExtensionDelegate 文件中,使用以下命令创建一个扩展的运行时会话。

let session = WKExtendedRuntimeSession()

Within this delegate file create an applicationDidBecomeActive() function which watchOS will call when your app becomes active.在此委托文件中,创建一个 applicationDidBecomeActive() 函数,当您的应用程序处于活动状态时,watchOS 将调用该函数。 Inside this function start the ExtendedRuntime Session with the following commands.在此函数中,使用以下命令启动 ExtendedRuntime Session。

session.delegate = self   // self as session handler
session.start()  // start WKExtendedRuntimeSession

An extended runtime session must be started when the app is in the active state.当应用程序处于活动状态时,必须启动扩展运行时会话。

Note you also need to enable Background Modes within the WatchKit Extension target.请注意,您还需要在 WatchKit 扩展目标中启用后台模式 Select one of the Session Types that most closely fits the type of app you are creating.选择最适合您正在创建的应用程序类型会话类型之一。

No, it's not currently possible to provide haptic feedback while the screen is off , because your code isn't running when the watch is not awake.不, 目前无法在屏幕关闭时提供触觉反馈,因为当手表未唤醒时您的代码未运行。

As you imagined, this is due to a battery-saving measure which suspends your watch app when the watch goes to sleep.正如您想象的那样,这是由于节电措施会在手表进入睡眠状态时暂停您的手表应用程序。

For more information, the Apple Developer Forum has a long watchOS 2 thread about NSTimer in background .有关更多信息,Apple Developer Forum 有一个关于后台 NSTimer的很长的 watchOS 2 线程。

For watchOS 2, one solution would be to use a paired app on your phone which handles the background timer and uses local notifications.对于 watchOS 2,一种解决方案是在手机上使用配对的应用程序来处理后台计时器并使用本地通知。 When the phone is locked, the notification would get sent to your watch.当手机锁定时,通知会发送到您的手表。 You can also use Watch Connectivity to keep the watch updated with timer details once it is awake.您还可以使用 Watch Connectivity 使手表在唤醒后随时更新计时器的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM