2

Hi so before I ask my question I would like to apologise because I am new with app development so sorry if any questions are really stupid.

So I am making an app using the Instagram API and this is my first time implementing it into Xcode and to be honest i'm not quite sure how it really works but I have managed to struggle my way through gaining the code that Instagram provides into Swift using a Web View but now I need to be able to run the terminal command:

curl -F 'client_id=CLIENT_ID' \
-F 'client_secret=CLIENT_SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
-F 'code=CODE' \
https://api.instagram.com/oauth/access_token

but is it possible for that to be able to process on an iPhone?

What i'm sort of trying to ask is whether or not you can run terminal commands on iPhone Apps and if so, how am I able to do this? What is the Swift or Objective-C code for this?

Thanks!

Vasily Bodnarchuk
  • 24,482
  • 9
  • 132
  • 127
OvRdose
  • 115
  • 1
  • 1
  • 7

2 Answers2

3

You need to send simple http request. Try this code:

Swift 3

    let redirectURI = "https://www.instagram.com/"
    let clientID = "{YOUR_CLIENT_ID}"
    let clientSecret = "{YOUR_CLIENT_SECRET}"
    let code = "{RECEIVED_CODE}"

    let urlString = "https://api.instagram.com/oauth/access_token"
    let url = NSURL(string: urlString)!
    let paramString  = "client_id=\(clientID)&client_secret=\(clientSecret)&grant_type=authorization_code&redirect_uri=\(redirectURI)&code=\(code)&scope=basic+public_content"

    let request = NSMutableURLRequest(url: url as URL)
    request.httpMethod = "POST"
    request.httpBody = paramString.data(using: String.Encoding.utf8)!

    let task =  URLSession.shared.dataTask(with: request as URLRequest)  { (data, response, error) in
        do {
            if let jsonData = data {
                if let jsonDataDict  = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.allowFragments) as? [String: AnyObject] {
                    NSLog("Received data:\n\(jsonDataDict))")
                }
            }
        } catch let err as NSError {
            print(err.debugDescription)
        }
    }

    task.resume()

Swift 2

    let redirectURI = "https://www.instagram.com/"
    let clientID = "{YOUR_CLIENT_ID}"
    let clientSecret = "{YOUR_CLIENT_SECRET}"
    let code = "{RECEIVED_CODE}"

    let urlString = "https://api.instagram.com/oauth/access_token"
    let url = NSURL(string: urlString)!
    let paramString  = "client_id=\(clientID)&client_secret=\(clientSecret)&grant_type=authorization_code&redirect_uri=\(redirectURI)&code=\(code)&scope=basic+public_content"

    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST"
    request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)!

    let task =  NSURLSession.sharedSession().dataTaskWithRequest(request)  { (data, response, error) in
        do {
            if let jsonData = data {
                if let jsonDataDict  = try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments) as? [String: AnyObject] {
                    NSLog("Received data:\n\(jsonDataDict))")
                }
            }
            } catch let err as NSError {
            print(err.debugDescription)
        }
    }

    task.resume()
Vasily Bodnarchuk
  • 24,482
  • 9
  • 132
  • 127
0

Terminal is an application on a desktop / laptop.

cURL is a tool / library on a desktop / laptop.



Can an iPhone install and use these?

No.



Can we write swift code that achieves the same thing cURL does?

Yes. The cURL command is firing of a request to a web service provided by instagram.

You will need to do quite a bit of google'ing and research to learn more about web requests and web services in general. Then looking into how yo do this in swift, such as using NSURLSession, or using an SDK.

Simon McLoughlin
  • 8,293
  • 5
  • 32
  • 56