我是Swift的新手。我正在制作一个发送多个HTTP请求并收到回复的应用。
我阅读了Swift背景教程,然后在模拟器中尝试并测试它没有响应。
我也通过alamofire
发送HTTP请求,我认为这没问题,因为alamofire
使用了NSURL
。
所以我认为我的appdelegate有一些不足之处。你能协助找出它无法正常工作的原因吗?
以下是我的应用委托代码的一部分供参考:
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)
}
}
答案 0 :(得分:-1)
swift的请求和响应。您可以使用AFNetwork或自己制作。
NSURLConnection,班级可以帮助你