【发布时间】:2017-06-30 15:02:56
【问题描述】:
正如标题所述,我已经使用 Alamofire 设置了一个 backgroundURL。它在模拟器中就像一个魅力,但在我的设备上却没有。我确定我在这里遗漏了一些东西,因为我对 URL 没有那么丰富的经验。 这是我到目前为止的代码:
class NetworkManager {
static let shared = NetworkManager()
private lazy var backgroundManager: Alamofire.SessionManager = {
let bundleIdentifier = MyStruct.identifier
return Alamofire.SessionManager(configuration: URLSessionConfiguration.background(withIdentifier: bundleIdentifier))
}()
var backgroundCompletionHandler: (() -> Void)? {
get{
return backgroundManager.backgroundCompletionHandler
}
set{
backgroundManager.backgroundCompletionHandler = newValue
}
}
}
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
NetworkManager.shared.backgroundCompletionHandler = completionHandler
}
在我的视图控制器中:
func populateArrays(){
Alamofire.request("http://www.aps.anl.gov/Accelerator_Systems_Division/Accelerator_Operations_Physics/sddsStatus/mainStatus.sdds.gz").responseData { response in
switch response.result{
case .success:
print("Validation Successful")
case .failure(let error):
print(error.localizedDescription)
}
if let data = response.result.value{
【问题讨论】:
-
您在手机上运行时是否看到任何错误日志、控制台警告?
-
没有错误或任何东西。它只是停止与 url 通信。
-
您能在此处设置一个断点并确保您的 http 调用是从您的手机执行的吗?
-
应用打开时肯定会调用它。我可以看到它打印“验证成功”。但是当应用程序被最小化或手机被锁定时,它不会调用它。甚至 handleEventsForBackgroundURLSession 上的断点也不会触发,所以我知道它出于某种原因没有调用应用程序委托。
-
哦,你是说在后台运行请求?
标签: swift xcode url alamofire background-process