• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在 iOS App 中使用位置服务时如何确定确切的应用重新启动原因?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 06:04:51 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在我的 iOS 应用中使用定位服务,它包括 SignificantLocationChangesGeofence

当用户移动一段距离时,iOS 正在唤醒我的应用。我在 AppDelegate 中使用“UIApplicationLaunchOptionsLocationKey”来识别应用启动,如下所示。

if (launchOptions[UIApplicationLaunchOptionsLocationKey]) {
    NSLog(@"App relaunched because of new location events.");
} else {
    NSLog(@"Normal app open");
}

但我无法确定是 SignificantLocationChanges 还是 Geofence

我们是否可以使用“UIApplicationLaunchOptionsLocationKey”来确定应用重新启动的确切原因。

我知道以下地理围栏的委托(delegate)方法:

- (void)locationManagerCLLocationManager *)manager didExitRegionCLCircularRegion *)region {
} 

但是由于某种原因,这个方法没有被触发。

我正在寻找方法来确定确切的应用重新启动原因(SLC 或地理围栏)。

有什么建议吗?

提前致谢。



Best Answer-推荐答案


我不完全确定,你在问什么,但我会尽力而为。 您是使用地理围栏还是使用核心位置区域监控?

如果您想知道您的应用是否被 didEnterRegion/didExitRegiondidUpdateLocations 唤醒 我想您可以让 locationManager 委托(delegate)方法使用以下方式显示本地通知:

-(void)showBackgroundNotificationNSString *) message {
    if (app.applicationState == UIApplicationStateBackground && [app currentUserNotificationSettings].types != UIUserNotificationTypeNone){
        UILocalNotification *note = [[UILocalNotification alloc] init];
        note.alertBody = message;
        note.soundName = UILocalNotificationDefaultSoundName;
        note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0];
        [app scheduleLocalNotification: note];
    }
}

通过在每个委托(delegate)函数中使用不同的字符串消息调用该函数。

如果我理解正确,情况是应用程序处于终止状态,即关闭。然后它得到一个位置更新并唤醒。然后发生了一些事情。

你得到 didEnterRegion 但没有 didExitregion 吗?问题可能是您没有按照应有的方式手动开启定位服务。

我在我的 AppDelegate.m 中使用它

- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions {
    //for wake from terminated on location updates
    if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
        GeoFence *fence = [GeoFence sharedInstance];
        [fence startMonitoringSignificantLocationChanges];
    }
}

我的地理围栏类是单例。除了 locationManager 委托(delegate)函数之外,还有两个重要的函数:

//singleton
+ (GeoFence*) sharedInstance {
    static GeoFence *_sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedInstance = [[GeoFence alloc] init];
    });
    return _sharedInstance;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        self.locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;

        //get authorization
        if ([locationManager respondsToSelectorselector(requestAlwaysAuthorization)]) {
            [locationManager requestAlwaysAuthorization];
        }
        [self startMonitoringSignificantLocationChanges];
        }

    }
    return self;
}

我真的希望你可以使用它。如果您需要进一步的帮助,请告诉我。 记得在头文件中声明 + (GeoFence*) sharedInstance 。

此外,请务必记住,区域监控不依赖于位置更新。文档对此并不清楚,但您可以关闭显着的ChangeLocationUpdates 并仍然获取您的区域边界事件。但那是另一回事了。

关于ios - 在 iOS App 中使用位置服务时如何确定确切的应用重新启动原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29225913/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap