2015-02-11 70 views
1

我可以使用CoreLocation框架獲取我的當前位置。我想註冊我的當前位置,以便我想知道誰已經進入和退出該區域。爲此,我使用蘋果記錄的方法註冊了一個區域,該區域爲- (void)registerRegionWithCircularOverlay:(MKCircle*)overlay andIdentifier:(NSString*)identifier { }。我的問題是什麼時候這個方法被調用?使用registerRegionWithCircularOverlay方法註冊區域

文檔鏈接:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html

任何幫助嗎?

+0

我認爲這是一個自定義的方法,你會得到其他方法來觸發這個方法的最後一行。你在didEnterRegion時檢查它。 – Larme 2015-02-11 13:33:01

回答

1

您必須注意訪問用戶設備位置的權限。因此該應用程序能夠跟蹤用戶的位置。

您可以通過以下做到這一點:

  1. 中的.plist文件添加一個關鍵NSLocationWhenInUseUsageDescription。這是鑰匙的類型值爲String。您必須注意不要在此處使用String以外的其他類型。

enter image description here

  • 向用戶訪問的用戶位置之前,添加下面一行。

    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.delegate = self; 
    [self.locationManager requestWhenInUseAuthorization]; 
    
  • 下面的代碼將開始監視區:

    // Tell location manager to start monitoring for the region 
    [self.locationManager startMonitoringForRegion:self.myBeaconRegion]; 
    
  • 而且覆蓋下面的方法,以確保區域發現:

    - (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region 
    { 
        // We entered a region! 
        NSLog(@"Entered in region"); 
    } 
    
    -(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region 
    { 
        // Exited the region 
        NSLog(@"Exited from region"); 
    } 
    
    +0

    我已經做了上面提到的所有檢查授權許可,Plist等權限描述我試圖註冊一個區域與上述方法,並遵循所有內部的蘋果指引,如檢查isMonitoringAvailable和授權,但我應該在哪個coreLocation委託事件在發佈問題之前調用此方法我在考慮在某些位置更改時調用 – codeIgnitor 2015-02-11 14:01:13

    +0

    您可以完全做到這一點,我的意思是在委託方法'didEnterRegion:'中註冊用戶位置。在這裏你必須檢查用戶不能註冊兩次。 – Kampai 2015-02-11 14:05:08