我知道我不是第一个要问的人,我可能不会成为第一个要问的人,但是我还没有看到任何对我有帮助的东西,所以无论如何我都会问。我试图在Unity应用程序中使用一些Swift和Objective-C,但我设法做到了这一点。但是,当我尝试使用在后台获取我的位置时,我尝试使用CLLocationManagerDelegate就像这样:
Example.swift:
import Foundation
import CoreLocation
import CoreLocation.CLLocationManagerDelegate
import UIKit
@objc class Example : NSObject {
@objc static func StartTracking(){
var att: FindMeTask = FindMeTask();
att.StartTracking()
}
}
class FindMeTask: UIViewController, CLLocationManagerDelegate {
var bgtask: UIBackgroundTaskIdentifier = UIBackgroundTaskIdentifier.invalid;
var coord: Coor = Coor()
private lazy var locationManager: CLLocationManager = {
let manager = CLLocationManager()
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.delegate = self
manager.requestAlwaysAuthorization()
manager.allowsBackgroundLocationUpdates = true
return manager
}()
public func StartTracking(){
locationManager.delegate = self;
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations
locations: [CLLocation]){
guard let mostRecentLocation = locations.last else{
return
}
let lat : Double = (mostRecentLocation.coordinate.latitude)
let lon : Double = (mostRecentLocation.coordinate.longitude)
coord.SetCoord(aLat: lat, aLon: lon)
if(UIApplication.shared.applicationState == .active){
print("In foreground")
}else{
print("In background, location is %@", coord)
}
}
}
Example.mm
#import <Foundation/Foundation.h>
#import "unityswift-Swift.h"
#import <CoreLocation/CoreLocation.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
extern "C" {
void _startTracking(){
[Example StartTracking];
}
}
最后,我的UnitySwift-Bridging-Header.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "UnityInterface.h"
#import "CoreLocation/CoreLocation.h"
#import "CoreLocation/CLLocationManagerDelegate.h"
因此,使用该代码,它给了我这个错误:当它试图声明我的FindMeTask的@interface(@interface FindMeTask:UIViewController)时,我的名字-Swift.h中没有名为'CLLocationManagerDelegate'的类型或协议。 另外,也许会有所帮助,我无法扩展CLLocationManagerDelegate在.mm文件中创建的接口(例如:@interface test:CLLocationManagerDelegate)
我尝试了很多事情,但是到目前为止,还没有一个工作。如果有人知道什么并可以提供帮助,那就太好了!预先感谢!