我使用 swiftyJSON 从 api url 消费 OData。这里 api url 与 VPN 连接。
api url 看起来像http://192.xxx.xx.xx:8000/sap/opu/odata/sap/Z_SRV/PRListSetSet ?$format=json
当我在模拟器中运行时,我可以从 odata api url 获取数据,但是在设备中运行时,没有从 odata api url 接收到数据。由于没有 vpn 连接到移动设备。如何以编程方式对我的 VPN 进行硬编码以在移动设备中接收数据?
这是我从 OData api url 获取数据的方式:
typealias ServiceResponse = (JSON,Error?) -> Void
class PrListApiManager: NSObject {
static let sharedInstance = PrListApiManager()
let baseURL = apiUrlConstant.prListOdataUrl
func getPrList(onCompletion:@escaping (JSON) -> Void) {
let route = baseURL
makeHTTPGetRequest(path: route) { (json: JSON, error: Error?) in
onCompletion(json as JSON)
}
}
// MARK: perform a GET Request
private func makeHTTPGetRequest(path: String, onCompletion: @escaping ServiceResponse) {
let user = ApiloginConstant.user
let password = ApiloginConstant.password
let loginString = "\(user):\(password)"
guard let loginData = loginString.data(using: String.Encoding.utf8) else {
return
}
let base64LoginString = loginData.base64EncodedString()
print("base 64 login :\(base64LoginString)")
let headers = ["Authorization": "Basic \(base64LoginString)"]
// using URL and request getting a json
let request = URLRequest(url: NSURL(string: path)! as URL)
let config = URLSessionConfiguration.default
config.httpAdditionalHeaders = headers
let session = URLSession.init(configuration: config)
session.dataTask(with: request) { (data:Data?, response: URLResponse?, error:Error?) in
if let jsonData = data { // if data has a data and success
do {
let json: JSON = try JSON(data: jsonData)
onCompletion(json,nil)
print("json data:\(json)")
}catch {// error
onCompletion(JSON(),error)
}
} else { // if the data is nil
onCompletion(JSON(),error)
}
}.resume()
}