Доступ пользователя текущего местоположения в пользовательских Sirikit намерений, iOS, Swift - PullRequest
0 голосов
/ 25 февраля 2019

Я создал собственное намерение Sirikit, в классе IntentHandler я не могу найти местоположение пользователя, где по умолчанию для местоположения задано значение «Всегда».пожалуйста, посмотрите на код.

    import Foundation
import CoreData
import CoreLocation
class PhotoOfTheDayIntentHandler: NSObject, PhotoOfTheDayIntentHandling {
let context = CoreDataStorage.mainQueueContext()
var counter : DistanceEntity?
var locationManger = CLLocationManager()
    func confirm(intent: PhotoOfTheDayIntent, completion: @escaping (PhotoOfTheDayIntentResponse) -> Void) {
        completion(PhotoOfTheDayIntentResponse(code: .ready, userActivity: nil))
}

func handle(intent: PhotoOfTheDayIntent, completion: @escaping (PhotoOfTheDayIntentResponse) -> Void) {
    self.context.performAndWait{ () -> Void in
        let counter = NSManagedObject.findAllForEntity("DistanceEntity", context: self.context)
        if (counter?.last != nil) {
            self.counter = (counter!.last as! DistanceEntity)
                let currentLocation: CLLocation = locationManger.location!
                let greenLocation = CLLocation(latitude:self.counter!.latitude, longitude: self.counter!.longitude)
                let distanceInMeters = currentLocation.distance(from: greenLocation) // result is in meters
                debugPrint("distanceInMeters",distanceInMeters)
                completion(PhotoOfTheDayIntentResponse.success(photoTitle: "\(distanceInMeters) Meter"))
            completion(PhotoOfTheDayIntentResponse.success(photoTitle: "\(self.counter!.distance) Meter"))
        }
    }
}
}

, если я прокомментирую диспетчер местоположений, это вылетит.

1 Ответ

0 голосов
/ 22 июня 2019

import CoreLocation class WhateverYouWant: NSObject{ let userLocation = UserLocationManager()

func handle(intent: DistanceOfGreenIntent, completion: @escaping (DistanceOfGreenIntentResponse) -> Void) {

    if(userLocation.locationManager.location == nil){
        userLocation.locationManager.requestAlwaysAuthorization()
        userLocation.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }


    if let currentLocation: CLLocation = userLocation.locationManager.location{
        self.context.performAndWait{ () -> Void in
           completion(DistanceOfGreenIntentResponse.success(distanceString:"Please Start a game on course to get distances"))
    }else{
        debugPrint("Please enable your location")
        completion(DistanceOfGreenIntentResponse.success(distanceString: "Please enable your location to get distances"))
    }}
}
...