我已经调查了重要位置。 我想在不运行应用程序时获取位置信息。 https://developer.apple.com/documentation/corelocation/getting_the_user_s_location/using_the_significant-change_location_service
以下代码是获取位置信息的触发器。
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if launchOptions?[.location] != nil {
locationManager.startMonitoringSignificantLocationChanges()
}
return true
}
但是我遇到了一个奇怪的行为。
当应用程序通过重要位置事件启动时。
仅限 AppDelegate:launchOptions?[.location] 不为零
AppDelegate + SceneDelegate:launchOptions?[.location] 始终为 nil
我无法从 SceneDelegate 方法中获得类似的值。
如果我想使用与位置相关的launchOptions,是否只使用AppDelegate更好?
遗憾的是,当启用场景生命周期时,没有机会获得任何
launchOptions
。我用 HopperApp 做了一些探索,发现了这个:
int -[UIApplication _compellApplicationLaunchToCompleteUnconditionally]() {
[_UISceneLifecycleMultiplexer sharedInstance];
r0 = objc_claimAutoreleasedReturnValue();
[r0 completeApplicationLaunchWithFBSScene:0x0 transitionContext:0x0];
...
提供给应用程序委托的启动选项通常取自
transitionContext.payload
,正如您所看到的,它只是作为 nil 提供。
当您使用场景委托时,您应该使用
scene(:,willConnectTo session:,options:)
函数
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
// handle universal links
if let userActivity = connectionOptions.userActivities.first {
self.scene(scene, continue: userActivity)
} else {
// handle other urls (f.e. your custom schemes)
self.scene(scene, openURLContexts: connectionOptions.urlContexts)
}
}