对于 SceneDelegate,LaunchOptions 始终为零

发布于 2025-01-11 20:53:54 字数 1115 浏览 0 评论 0原文

我已经调查了重要位置。 我想在不运行应用程序时获取位置信息。 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] 不为 nil
AppDelegate + SceneDelegate:launchOptions?[.location]始终为零

我无法从SceneDelegate方法获得类似的值。

SceneDelegate 截图

如果我想使用与位置相关的 launchOptions,是否只使用 AppDelegate 更好?

I have investigated siginificant location.
I would like to get location information when not running app.
https://developer.apple.com/documentation/corelocation/getting_the_user_s_location/using_the_significant-change_location_service

The following code is an trigger for getting location information.

func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        if launchOptions?[.location] != nil {
            locationManager.startMonitoringSignificantLocationChanges()
        }
        return true
    }

But I encountered a strange behavior.
When an app launch by significant location event.

AppDelegate only: launchOptions?[.location] isn’t nil
AppDelegate + SceneDelegate: launchOptions?[.location] is always nil

I couldn’t get similar value from SceneDelegate method.

SceneDelegate screenshot

If I want to use launchOptions related to location, is it better to use AppDelegate only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱果果 2025-01-18 20:53:54

UIApplicationLaunchOptionsLocationKeylaunchOptions?[.location] 目前仅适用于应用程序委托生命周期(从 iOS 7.4 开始),即删除 SceneDelegate 类并删除场景清单时来自 Info.plist。

要获得场景生命周期,您需要自己破解信息,例如:

Info.plist

Principal class = MyApplication

YourApp-Bridging-Header.h

#import "MyApplication.h"

MyApplication.h< /strong>

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyApplication: UIApplication

@property (assign, nonatomic) BOOL significantLocationCausedLaunch;

@end

NS_ASSUME_NONNULL_END

MyApplication.m

#import "MyApplication.h"

@interface UIApplication() // private methods

-(void)_connectUISceneFromFBSScene:(id)scene transitionContext:(id)context;

@end


@implementation MyApplication

- (void)_connectUISceneFromFBSScene:(UIScene *)scene transitionContext:(id)context {
    NSDictionary *payload = [context performSelector:@selector(payload)];
    self.significantLocationCausedLaunch = [[payload valueForKey:@"CLLaunchOptionsLocation"] boolValue];
    [super _connectUISceneFromFBSScene:scene transitionContext:context];
}

@end

现在您可以在场景委托中检索它,如下所示:

   func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        
        if let myApp = UIApplication.shared as? MyApplication {
            if myApp.significantLocationCausedLaunch {
                print("significantLocationCausedLaunch")
            }
        }
        
        guard let _ = (scene as? UIWindowScene) else { return }
    }

我通过反馈报告了场景生命周期的此限制,以便将其作为 .backgroundTask(.significantLocation) 添加到 SwiftUIFB13699977 请随时在您的报告中引用。

UIApplicationLaunchOptionsLocationKey or launchOptions?[.location] is currently only available (as of iOS 7.4) with app delegate life-cycle that's when you remove your SceneDelegate class and remove the scene manifest from the Info.plist.

To get it with scene lifecycle you need to hack the info yourself, e.g. something like this:

Info.plist

Principal class = MyApplication

YourApp-Bridging-Header.h

#import "MyApplication.h"

MyApplication.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyApplication: UIApplication

@property (assign, nonatomic) BOOL significantLocationCausedLaunch;

@end

NS_ASSUME_NONNULL_END

MyApplication.m

#import "MyApplication.h"

@interface UIApplication() // private methods

-(void)_connectUISceneFromFBSScene:(id)scene transitionContext:(id)context;

@end


@implementation MyApplication

- (void)_connectUISceneFromFBSScene:(UIScene *)scene transitionContext:(id)context {
    NSDictionary *payload = [context performSelector:@selector(payload)];
    self.significantLocationCausedLaunch = [[payload valueForKey:@"CLLaunchOptionsLocation"] boolValue];
    [super _connectUISceneFromFBSScene:scene transitionContext:context];
}

@end

Now you can retrieve it in your scene delegate as follows:

   func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        
        if let myApp = UIApplication.shared as? MyApplication {
            if myApp.significantLocationCausedLaunch {
                print("significantLocationCausedLaunch")
            }
        }
        
        guard let _ = (scene as? UIWindowScene) else { return }
    }

I reported this limitation of scene life cycle via Feedback for addition to SwiftUI as a .backgroundTask(.significantLocation) as FB13699977 feel free to reference in your report.

梦纸 2025-01-18 20:53:54

当您使用场景委托时,您应该使用 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)
        }
    }

When you work with the scene delegate you should use scene(:,willConnectTo session:,options:) function instead

 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)
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文