3

I have consuming OData from api url using swiftyJSON. Here api url is connected with VPN.

And api url looks like http://192.xxx.xx.xx:8000/sap/opu/odata/sap/Z_SRV/PRListSetSet?$format=json

When i run in simulator, i can able get data from odata api url but while running in device, no data received from odata api url. Since no vpn is connected to mobile device. how can i hard code my VPN programmactically to receive data in mobile?

here is how i'm getting data from 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()
}
PvUIDev
  • 95
  • 1
  • 9
  • 38

1 Answers1

0

Connecting to a VPN is probably not your best bet. If your app is intended for public release, this can be a massive security concern and may overload your servers. The reason behind this is because I believe you cannot route only some of the traffic through a VPN. Either everything from the device goes through, or nothing does.

If you really need a way to reach your servers, consider using a proxy that only exposes that specific service out to the internet.

If you still want to go the VPN route, here's an answer that explains how to set it up: https://stackoverflow.com/a/39183930/4663856

  • its in house project not for public.. Thanks for answering.. i'm new to this how can i use this in my project.. any sample code.. – PvUIDev Sep 09 '19 at 07:16
  • @ParameswaranV can you simply connect to your vpn directly from your device? Other than that, you can also share your macbook connection with your device, using the lightening port or blue tooth. This way when you make an api request from your device, it will go through the macbook, then the vpn. no? – Alex Sep 15 '19 at 10:01