iOS开发SwiftUI

如何让Apple Watch App 保持Active状态

参考:https://stackoverflow.com/questions/47896493/can-i-keep-a-watch-app-running-in-background

在Apple Watch App 开发中,有时候我们希望Apple Watch App能一直运行在前台。但是开发时,如果不做什么特别处理的话,会发现App在一段时间就会进入后台,并且表盘会显示时间。那么有什么方法能让App一直保持活跃呢,我们可以利用Background Gps。

App可以一直运行在前台
退到表盘时,还能看到App运行在后台
  • Apple Watch中开启Background Location updates
  • Info.plist中添加 Privacy

Privacy – Location Always and When In Use Usage Description

Privacy – Location Always Usage Description

Privacy – Location When In Use Usage Description

  • 在 ExtensionDelegate.swift 中启动GPS 定位。

import WatchKit
import CoreLocation

class ExtensionDelegate: NSObject, WKExtensionDelegate, CLLocationManagerDelegate {
    var locationManager: CLLocationManager?

    func applicationDidFinishLaunching() {

        self.locationManager = CLLocationManager()
        self.locationManager!.delegate = self
        self.locationManager!.distanceFilter = 1
        self.locationManager!.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager!.requestAlwaysAuthorization()
        // 这一行代码非常重要!!!
        self.locationManager!.allowsBackgroundLocationUpdates = true
        self.locationManager!.startUpdatingLocation()
    }

    func applicationDidBecomeActive() {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillResignActive() {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, etc.
    }

    func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
        // Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
        for task in backgroundTasks {
            // Use a switch statement to check the task type
            switch task {
            case let backgroundTask as WKApplicationRefreshBackgroundTask:
                // Be sure to complete the background task once you’re done.
                backgroundTask.setTaskCompletedWithSnapshot(false)
            case let snapshotTask as WKSnapshotRefreshBackgroundTask:
                // Snapshot tasks have a unique completion call, make sure to set your expiration date
                snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)
            case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:
                // Be sure to complete the connectivity task once you’re done.
                connectivityTask.setTaskCompletedWithSnapshot(false)
            case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
                // Be sure to complete the URL session task once you’re done.
                urlSessionTask.setTaskCompletedWithSnapshot(false)
            case let relevantShortcutTask as WKRelevantShortcutRefreshBackgroundTask:
                // Be sure to complete the relevant-shortcut task once you're done.
                relevantShortcutTask.setTaskCompletedWithSnapshot(false)
            case let intentDidRunTask as WKIntentDidRunRefreshBackgroundTask:
                // Be sure to complete the intent-did-run task once you're done.
                intentDidRunTask.setTaskCompletedWithSnapshot(false)
            default:
                // make sure to complete unhandled task types
                task.setTaskCompletedWithSnapshot(false)
            }
        }
    }

}

extension ExtensionDelegate: WCSessionDelegate {


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let currentLocation = locations.last! as CLLocation
        

    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

        switch status.rawValue {

        case 0:
            print("Not determined")
            NotificationCenter.default.post(name: .gpsInfo, object: "Not determined", userInfo: nil)

        case 1:
            print("Restricted Access")
            NotificationCenter.default.post(name: .gpsInfo, object: "Restricted Access", userInfo: nil)

        case 2:
            print("Denied")
            NotificationCenter.default.post(name: .gpsInfo, object: "Denied", userInfo: nil)

        case 3:
            print("Authorized Always")
            self.locationManager!.requestLocation()
            NotificationCenter.default.post(name: .gpsInfo, object: "Authorized Always", userInfo: nil)

            break

        case 4:

            print("Authorization When in use")
            self.locationManager!.requestLocation()

        default:
            break
        }

    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {


    }
}