簡體   English   中英

當用戶進入/退出iOS中的位置區域時,在后台執行任務(代碼執行/網絡調用)

[英]Perform task (code execution/ network call) in background when user enters/exits a location region in iOS

系統:在我的應用程序中,我正在使用地理圍欄(監視區域)。 每當用戶進入或退出監視區域或區域時,如果該應用程序在后台甚至終止,該應用程序都會顯示本地通知。 這工作得很好。 該應用程序能夠顯示本地通知。

現在,我還需要通過HTTP POST調用向應用程序的后端服務器提交此信息(如果用戶位於受監視區域之內或之外)。

問題: App在LocationManager的委托方法中進行API調用,但有時可以,有時卻不起作用。 如果應用未處於前台狀態,則似乎代碼執行會隨機停止。

代碼樣例

// MARK: - Location Manager Delegate
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    if region is CLCircularRegion {
        showLocalNotification(forRegion: region)// Works
        updateUserEntryAPICall(region: region) // Sometimes works
    }
}

func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
    if region is CLCircularRegion {
        showLocalNotification(forRegion: region) // Works
        updateUserExitAPICall(region: region) // sometime works
    }
}

我沒有在iOS中打開后台模式功能。 該應用仍然可以顯示本地通知。 我需要將其打開才能進行網絡通話嗎?

在此處輸入圖片說明

請幫忙。

看起來您正在使用常規URLSession發出API請求。 您需要確保正在處理后台運行的會話上執行此操作,例如,通過如下初始化:

let session = URLSession(configuration: .background(withIdentifier: "foo"))

暫無
暫無

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

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