2017-07-28 33 views
0

我試圖實現它需要檢測設備是否位於目標區域(geofence)內部的功能。 如果它位於區域內部,它應該執行一些任務,而不管它處於前景還是背景。iOS區域監控在設備重啓後沒有明顯的位置變化

我能弄清楚大部分零件並實施。

然而,挑戰在於用戶重新啓動設備,而該設備已經位於該區域內部並且不會進行任何重大位置更改。

在這種情況下,iOS似乎沒有檢測到它是內部還是外部。因此,我的應用程序不會執行任何操作。

那麼有沒有什麼辦法讓iOS自動檢測它是否在地理圍欄內或重新啓動後沒有明顯的位置變化?

回答

0

首先,你需要調用requestState(for:)你想諮詢區,請注意這將使異步諮詢其答覆,然後將locationManager(_:didDetermineState:for:)locationManager(_:didEnterRegion:)locationManager(_:didExitRegion:)),趕上所以,如果你諮詢區的陣列考慮這帳戶。

var locationManager = CLLocationManager() 
// Do all proper setting for locationManager 
... 
// Here you make the asynchronous request for the state of a region 
locationManager.requestState(for: CLRegion()) // Your region 

func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) { 
    switch state { 
     case .inside: 
     // It's inside the region 
     case .outside: 
     // It's outside 
     default: 
     // It's unknown 
    } 
} 

綜上所述,requestState(for:)是強迫顯著變化,而無需移動裝置,使所有其他代表將被稱爲很好,但你的興趣點是locationManager(_:didDetermineState:for:)

源相當於:https://developer.apple.com/documentation/corelocation/cllocationmanager/1423804-requeststate

+0

感謝您的回覆,但只有在iOS設備檢測到重大位置更改時纔會調用此回調函數。 – androisojavaswift

+0

@androisojavaswift現在嘗試我的新方法,讓我知道。 –

+0

感謝您的詳細信息。你是對的。您建議的方法運作良好,我也使用與您的方法相同的方法。但是這種方法在重啓設備後不再工作,因爲iOS不會在後臺自動啓動應用程序並觸發requestState(用於:)。 所以我需要弄清楚如何在設備重啓後在後臺觸發requestState(用於:),但不需要用戶重新啓動應用程序。 – androisojavaswift

相關問題