我正在尝试从网址获取数据。我给出了成功和失败的两个案例。但总是给我失败的情况。我不知道为什么总是把失败的情况归还给我..提前谢谢。
在本课程中,我试图下载数据。
import UIKit
import Alamofire
class RegisterTwo {
private static var _businessName = [String]()
public static var businessName: [String] {
if _businessName.isEmpty == true {
_businessName = ["Line of business"]
}
return _businessName
}
func downloadLineOfBusiness(completed: @escaping DownloadComplete) {
let lineOfBusinessURL = URL(string: LINE_OF_BUSINESS_URL)!
Alamofire.request(lineOfBusinessURL).responseJSON { (response) in
let result = response.result
switch(result) {
case.success(_):
if let dict = result.value as? Dictionary<String, AnyObject> {
// results
if let results = dict["results"] as? [Dictionary<String, AnyObject>] , results.count > 0 {
// name
if let name = results[0]["name"] as? String {
RegisterTwo._businessName.append("\(name)")
}
if results.count > 1 {
for x in 1..<results.count {
if let name = results[x]["name"] as? String {
RegisterTwo._businessName.append("\(name)")
}
}
}
print(RegisterTwo._businessName)
}
// set the success value true
UserDefaults.standard.setIsSuccessLineOfBusiness(value: true)
}
break
case.failure(_):
// set the success value false
UserDefaults.standard.setIsSuccessLineOfBusiness(value: false)
print("Failure occured")
break
}
completed()
}
}
}
我在这里调用这个函数。
override func viewDidAppear(_ animated: Bool) {
checkInternet()
}
func checkInternet() {
if currentReachabilityStatus == .reachableViaWiFi {
print("User is connected to the internet via wifi.")
self.registerTwo.downloadLineOfBusiness {
if self.isSuccessLineOfBusiness() {
self.perform(#selector(SplashVC.showVC), with: nil, afterDelay: 2)
} else {
self.alertBox(title: "Internet Error", message: "\nConnection Problem. Please reopen the app.")
}
}
}else if currentReachabilityStatus == .reachableViaWWAN{
print("User is connected to the internet via WWAN.")
self.registerTwo.downloadLineOfBusiness {
if self.isSuccessLineOfBusiness() {
self.perform(#selector(SplashVC.showVC), with: nil, afterDelay: 2)
} else {
self.alertBox(title: "Internet Error", message: "\nConnection Problem. Please reopen the app.")
}
}
} else {
print("There is no internet connection")
alertBox(title: "Internet Error", message: "\nNo internet connection.")
}
}