gpt4 book ai didi

ios - 如何实现 iOS 后台获取?

转载 作者:行者123 更新时间:2023-11-28 09:27:13 24 4
gpt4 key购买 nike

我是 Swift 的新手。我正在制作一个发送多个 HTTP 请求并接收响应的应用。

我看了Swift后台教程,然后在模拟器上试了试没有反应。

我还通过 alamofire 发送 HTTP 请求,我认为这没有问题,因为 alamofire 使用 NSURL

所以我认为我的 appdelegate 有一些不足。您能协助确定它不起作用的原因吗?

这是我的应用委托(delegate)代码的一部分以供引用:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(
UIApplicationBackgroundFetchIntervalMinimum)

return true
}

func application(application: UIApplication, completionHandler: (UIBackgroundFetchResult) -> Void) {

if let AddressViewController = window?.rootViewController as? AddressViewController {
AddressViewController.fetchAndUpdate() {
completionHandler(.NewData)
}
} else {
completionHandler(.Failed)
}
}

func fetchAndUpdate(completionHandler: (() -> Void)!) {

AddressList.map({ (oneLineAddress) in
dataController.fetchTags(oneLineAddress) { (error, newPocket) in
self.dataController.updateData(oneLineAddress, newPocket: newPocket)
}
})
}

func fetchTags(address:OneLineAddress, completionHandler: (NSError?, newPocket:Set<String>) -> Void) {

var newPocket:Set<String> = []

guard let targetAddress:OneLineAddress = address else {
return
}
Alamofire.request(.GET, targetAddress.url).responseString {
responseString in
guard responseString.result.error == nil else {
completionHandler(responseString.result.error!, newPocket: [])
NSLog("%@", responseString.result.error!)
return
}
guard let htmlAsString = responseString.result.value else {
let error = Error.errorWithCode(.StringSerializationFailed, failureReason: "Could not get HTML as String")
NSLog("%@", error)
completionHandler(error, newPocket: [])
return
}

let doc = HTMLDocument(string: htmlAsString)

let anchors = doc.nodesMatchingSelector("a")

for row in anchors {
if let anchorElement = row as? HTMLElement {
if self.isAnchor(anchorElement) {
if let title = self.matchKeyword(anchorElement, address: targetAddress) {
newPocket.insert(title)
}
}
}
}
completionHandler(nil, newPocket: newPocket)
}
}

最佳答案

通过 swift 请求和响应。您可以使用 AFNetwork 或自己制作。

NSURLConnection,这个类可以帮助你

关于ios - 如何实现 iOS 后台获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35534353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com