gpt4 book ai didi

iphone - 核心位置 didEnterRegion 无效

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:55 25 4
gpt4 key购买 nike

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

仅当对应于实现 CLLocationManagerDelegate 的类的 UIView 处于事件状态时才有效。

如果我更改 View ,它不会触发 didEnterRegion。谁能帮帮我?

我的代码是这样的

- (void)enableRegionMonitoring {
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
CLLocationCoordinate2D myMonLocation = CLLocationCoordinate2DMake(10.766699790955, 76.650101525879);
CLRegion *myRegion = [[CLRegion alloc]
initCircularRegionWithCenter:myMonLocation
radius:100
identifier:@"MyLoc"];
//NSLog(@"reg=%@",myRegion);
// Start monitoring for our CLRegion using best accuracy
[locationManager startMonitoringForRegion:myRegion
desiredAccuracy:kCLLocationAccuracyBest];
}



- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"Entered Region");

NSDate *nowx=[NSDate date];


UILocalNotification *localNotification=[[UILocalNotification alloc]init];
if (!localNotification)
return;
NSDictionary *data = [NSDictionary dictionaryWithObject:@"qw" forKey:@"mykey"];
[localNotification setUserInfo:data];

[localNotification setFireDate:nowx];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
NSMutableString *message=[[NSMutableString alloc]init];
message = @"Local Not Triggered By didEnterRegion";
[localNotification setAlertBody:[nowx description]];
[localNotification setAlertAction:@"Open App"];
[localNotification setHasAction:YES];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

最佳答案

从您的代码来看,我猜您正在使用 ARC,这取决于您的 Controller / View 层次结构,当您切换到不同的 View 时,您的 View 和 Controller 可能会被释放,发生这种情况时,locationManager 也会被释放。

只需将整个 CLLocationManager 代码移动到您的 AppDelegate 并让 AppDelegate 成为 CLLocationManager 委托(delegate)。在您现在调用“enableRegionMonitoring”的地方,您可以在 AppDelegate 上调用它。即使 ViewController 不再可见,它也会保持事件状态。

关于iphone - 核心位置 didEnterRegion 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12907642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com