1

我正在尝试在最近开始使用 SwiftUI 的 iOS 应用中实现位置触发通知。

作为第一个实验步骤,我有一个按钮。当点击它时,它应该会创建一个通知,一旦我靠近某个位置就会触发。作为起点,我阅读了这一页。但在付诸实践时并不是一清二楚。

位置部分正在工作,但不是通知部分。虽然我以前使用过 CoreLocation 和 UserNotifications,但我以前没有使用位置触发通知的经验。

此后是我此时拥有的相关(不工作)代码。任何提示或建议,在正确的方向,使其工作将受到欢迎。

import SwiftUI
import MapKit

struct ContentView: View {
    ......
    func handleShareLocation() {
        print(#function)
        //TRIAL-CODE
        guard CLLocationManager.locationServicesEnabled() else {
            return
        }
        UNUserNotificationCenter.current().requestAuthorization(
            options: [.alert, .sound, .badge],
            completionHandler: { _,_ in //[weak self] granted, _ in
                /*guard granted else {
                    self?.delegate?.notificationPermissionDenied()
                    return
                }*/

                let notificationInfo = LocationNotificationInfo (notificationId: "nyc_promenade_notification_id",
                locationId: "nyc_promenade_location_id",
                radius: 500.0,
                latitude: 40.696503,
                longitude: -73.997809,
                title: "Welcome to the Brooklyn Promenade!",
                body: "Tap to see more information",
                data: ["location": "NYC Brooklyn Promenade"])

                self?.requestNotification(notificationInfo:     notificationInfo)
        })
        //TRIAL-CODE
    }


    var body: some View {
        VStack {
            Spacer()
            Button(action: {
                self.handleShareLocation()
            }) {
                Text("Create location notification")
                .padding()
            }
            Spacer()
        }
    }
}

此外,我也在使用这个类:

class LocationNotificationScheduler: NSObject {
     ........
}

来自我提到的文件。其中有两个不清楚的点是:

  1. LocationNotificationSchedulerDelegate来自哪里?
  2. 什么是通知计划

这两点导致错误消息,例如:

Use of undeclared type 'LocationNotificationSchedulerDelegate'

如果需要更多信息,请告诉我。

4

0 回答 0