簡體   English   中英

從未調用過 LocationManager 的 didEnterRegion

[英]didEnterRegion of LocationManager not called ever

我厭倦了發現這個問題,但無法理解為什么會發生這種情況,因為位置管理器的委托方法didEnterRegiondidExitRegion被調用過。 我在我的 iPhone 上對此進行了測試,但它不起作用。 請告訴我我錯過了什么。 我的源代碼是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    manager = [[CLLocationManager alloc] init];
    manager.delegate = self;
    dist = 100;
    coord = CLLocationCoordinate2DMake(24.004686, 74.554088);
    region=[[CLRegion alloc] initCircularRegionWithCenter:coord radius:dist identifier:@"emanuele"];
    [manager startMonitoringForRegion:region desiredAccuracy:10];
    return YES;
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
    NSLog(@"Enter Region.");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got it..." message:@"You have Entered the Location." delegate:nil cancelButtonTitle:@"OK"  otherButtonTitles: nil];
    [alert show];
//    [alert release];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"didExitRegion");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got it..." message:@"You have Exited the Location." delegate:nil cancelButtonTitle:@"OK"  otherButtonTitles: nil];
    [alert show];
//    [alert release];
}

任何人都可以弄清楚出了什么問題嗎? 我搜索了很多演示和源代碼,但其中大部分都以didUpdateToLocation ,在我的案例中調用了它,但我對didEnterdidExit有疑問。 我也實現了didFailWithError但它沒有做這件事。 請給出為什么不調用這兩種方法的任何線索。 或者給我任何調用這些方法的鏈接,我沒有找到這些方法的示例/源代碼。 任何幫助將不勝感激。

您的頭文件是否聲明為 CLLocationManagerDelegate?

一旦你設置了監控區域,你應該通過刪除該代碼元素進行測試,然后在啟動時檢查 UIApplicationLaunchOptionsLocationKey 鍵,如下所示:

 if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]){

        NSLog(@"UIApplicationLaunchOptionsLocationKey - fired");
        if(locman == nil){
            locman = [[CLLocationManager alloc]init];


        locman.delegate = self;
        locman.distanceFilter = kCLLocationAccuracyBest;
        }

    }

然后將調用位置管理器委托,並且 didEnter 和 didExit 應該正常觸發。

viewDidLoad中,添加以下代碼

self.beaconRegion.notifyOnEntry=YES;
self.beaconRegion.notifyOnExit=YES;
self.beaconRegion.notifyEntryStateOnDisplay=YES;
[self.locationManager startMonitoringForRegion:self.beaconRegion];
[self.locationManager requestStateForRegion:self.beaconRegion];

我希望這會解決你的問題。

只有在常規設置中啟用后台應用程序刷新后,我才能獲得 didEnterRegion 和 didExitRegion。

您是否也嘗試過didDetermineState didEnterRegiondidExitRegion僅在進入/退出時調用,但根據我的理解,如果您在該地區並且實際上沒有移動, didDetermineState會告訴您您當前是否在該地區。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM