# Alamofire Tutorial: Getting Started

**URL:** <https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899>\
**Category:** 🎓 Courses & Articles\
**Created:** [March 1, 2016, 2:00pm UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899 "2016-03-01T14:00:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![astralbodies](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/astralbodies/32/109036_2.png) [@astralbodies](https://forums.kodeco.com/u/astralbodies)\
**Post date:** [March 1, 2016, 2:00pm UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/1 "2016-03-01T14:00:17Z")

</div>

In this Alamofire tutorial, you'll learn how to build an iOS app with Swift that sends & receives data to a remote API via HTTP using the Alamofire library.

* * *
 This is a companion discussion topic for the original entry at [http://www.raywenderlich.com/121540/alamofire-tutorial-getting-started](http://www.raywenderlich.com/121540/alamofire-tutorial-getting-started)

---

<div class="post-metadata">

**Author:** ![zarzonis](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zarzonis/32/94791_2.png) [@zarzonis](https://forums.kodeco.com/u/zarzonis)\
**Post date:** [March 1, 2016, 9:22pm UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/2 "2016-03-01T21:22:14Z")

</div>

Nice tutorial! Just what i needed to get started with Alamofire. Thank you very much!

---

<div class="post-metadata">

**Author:** ![astralbodies](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/astralbodies/32/109036_2.png) [@astralbodies](https://forums.kodeco.com/u/astralbodies)\
**Post date:** [March 1, 2016, 9:37pm UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/3 "2016-03-01T21:37:33Z")

</div>

You’re definitely welcome! Thanks for the feedback 🙂

---

<div class="post-metadata">

**Author:** ![huralnyk](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/huralnyk/32/81966_2.png) [@huralnyk](https://forums.kodeco.com/u/huralnyk)\
**Post date:** [March 3, 2016, 11:23am UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/4 "2016-03-03T11:23:18Z")

</div>

Super tutorial. Thanks a lot! Especially for part about creating API Router.

---

<div class="post-metadata">

**Author:** ![a\_kant](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/a_kant/32/110017_2.png) [@a\_kant](https://forums.kodeco.com/u/a_kant)\
**Post date:** [March 8, 2016, 7:47pm UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/5 "2016-03-08T19:47:14Z")

</div>

Very good tutorial.  
Thanks a lot.  
One question: how I can use language for tags from this project?

I added in ImaggaRouter.swift code:

case Language(String)  
…  
case .Language(“ru”): //this error  
return (“/language”, .GET) // —//—//—

---

<div class="post-metadata">

**Author:** ![astralbodies](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/astralbodies/32/109036_2.png) [@astralbodies](https://forums.kodeco.com/u/astralbodies)\
**Post date:** [March 8, 2016, 8:05pm UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/6 "2016-03-08T20:05:20Z")

</div>

@a_kant Probably something like this:

```
public enum ImaggaRouter: URLRequestConvertible {
  static let baseURLPath = "http://api.imagga.com/v1"
  //TODO: Replace xxx with your auth token found at https://imagga.com/profile/dashboard
  static let authenticationToken = "Basic xxx"
  
  case Content()
  case Tags(String, String)
  case Colors(String)

  public var URLRequest: NSMutableURLRequest {
    let result: (path: String, method: Alamofire.Method, parameters: [String: AnyObject]) = {
      switch self {
      case .Content():
        return ("/content", .POST, [String: AnyObject]())
      case .Tags(let contentID, let language):
        let params = ["content" : contentID, "language" : language]
        return ("/tagging", .GET, params)
      case .Colors(let contentID):
        let params = ["content" : contentID, "extract_object_colors" : NSNumber(int: 0)]
        return ("/colors", .GET, params)
      }
    }()

    let URL = NSURL(string: ImaggaRouter.baseURLPath)!
    let URLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(result.path))
    URLRequest.HTTPMethod = result.method.rawValue
    URLRequest.setValue(ImaggaRouter.authenticationToken, forHTTPHeaderField: "Authorization")
    URLRequest.timeoutInterval = NSTimeInterval(10 * 1000)

    let encoding = Alamofire.ParameterEncoding.URL

    return encoding.encode(URLRequest, parameters: result.parameters).0
  }

}

```

---

<div class="post-metadata">

**Author:** ![a\_kant](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/a_kant/32/110017_2.png) [@a\_kant](https://forums.kodeco.com/u/a_kant)\
**Post date:** [March 8, 2016, 8:26pm UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/7 "2016-03-08T20:26:02Z")

</div>

Tomorrow I try it. And inform you.  
Thank you.

---

<div class="post-metadata">

**Author:** ![a\_kant](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/a_kant/32/110017_2.png) [@a\_kant](https://forums.kodeco.com/u/a_kant)\
**Post date:** [March 9, 2016, 4:06am UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/8 "2016-03-09T04:06:36Z")

</div>

It’s work fine.  
Thanks a lot.

P.S.  
I added code:  
func downloadTags(contentID: String, completion: ([String]) → Void) {  
Alamofire.request(ImaggaRouter.Tags(contentID, **“ru”** ))  
.responseJSON { }

---

<div class="post-metadata">

**Author:** ![isuru](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/isuru/32/104343_2.png) [@isuru](https://forums.kodeco.com/u/isuru)\
**Post date:** [March 12, 2016, 4:45pm UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/9 "2016-03-12T16:45:23Z")

</div>

Thanks for the tutorial. I’ve been working with Alamofire since it’s beginning. But creating the router was new to me. I will surely make use of that in my future projects.

---

<div class="post-metadata">

**Author:** ![dakeshi](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@dakeshi](https://forums.kodeco.com/u/dakeshi)\
**Post date:** [March 22, 2016, 2:58am UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/10 "2016-03-22T02:58:28Z")

</div>

It’s great tutorial to learn about Alamofire.  
Thank you.

---

<div class="post-metadata">

**Author:** ![abo3atef](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/abo3atef/32/16062_2.png) [@abo3atef](https://forums.kodeco.com/u/abo3atef)\
**Post date:** [April 2, 2016, 11:51pm UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/11 "2016-04-02T23:51:21Z")

</div>

Thanks so much for this very Nice Tutorial.  
Actually I revised a lot of Swift skills because of this tutorials and write downed them in Text file.  
I can send it to you if you want, just send me your email.  
my email is: [ahmad.3atef@gmail.com](mailto:ahmad.3atef@gmail.com)

I’ll use Router concept in my next swift code, specially in Business Logic Layer as I was using it in Objective C.  
😉

Thanks So much

---

<div class="post-metadata">

**Author:** ![mahmud](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mahmud/32/6725_2.png) [@mahmud](https://forums.kodeco.com/u/mahmud)\
**Post date:** [April 18, 2016, 10:40am UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/12 "2016-04-18T10:40:35Z")

</div>

For some beginners like me could you please add a delay i found it helpful before performing the segue  
i used the code below it helped me to better understand the interactions

```
  self.delay(2.0) {
    self.performSegueWithIdentifier("ShowResults", sender: self)
    }

```

in imagePickerController(\_:didFinishPickingMediaWithInfo info:) in the 4th part  
:]

---

<div class="post-metadata">

**Author:** ![rothausen](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rothausen/32/61722_2.png) [@rothausen](https://forums.kodeco.com/u/rothausen)\
**Post date:** [May 10, 2016, 9:41am UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/13 "2016-05-10T09:41:23Z")

</div>

Hi, this might be a bit of topic, but here goes:  
I have developed an app where an admin user can controll the app content (text, pictures, events etc) from a backend/CMS system. However the transfer of the data does not take place untill the user opens the app (every time the app is opened it goes to the backend to look for updates). I would like to push the data immediately so the user do not have to wait for the download to take place when the app is opened. So my question is how: Can I force the data to the app immediately after a backend update? I think it could be implemented with a special type of push messages that will trigger the app to search the backend for updates, but I would like to hear you if you have other ideas for how this functinality can be implemented?  
Thanks 🙂  
Claus

---

<div class="post-metadata">

**Author:** ![astralbodies](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/astralbodies/32/109036_2.png) [@astralbodies](https://forums.kodeco.com/u/astralbodies)\
**Post date:** [May 10, 2016, 12:46pm UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/14 "2016-05-10T12:46:26Z")

</div>

Hi Claus - Yeah that’s a little off topic but I don’t mind giving you some direction. You are correct, background push notifications with fetch is the right way to go. You can also configure your app to do a scheduled background fetch but the interval is not guaranteed by iOS and is dependent on a lot of conditions you have no control over. I’d look into implementing a push notification with background fetching. Do remember that the user can shut off background fetch for your app so make sure you don’t make the app completely contingent on this feature.

---

<div class="post-metadata">

**Author:** ![dershowitz011](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/dershowitz011/32/33190_2.png) [@dershowitz011](https://forums.kodeco.com/u/dershowitz011)\
**Post date:** [May 27, 2016, 2:13pm UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/16 "2016-05-27T14:13:40Z")

</div>

How do you get the ACTUAL server json file using alamofire. Something like

> {  
> “responseHeader”: {  
> “status”: 0,  
> “QTime”: 61,  
> “params”: {  
> “q”: “firstName:Shaantnu”,  
> “indent”: “true”,  
> “wt”: “json”,  
> “\_”: “1464028787952”  
> }  
> },  
> “response”: {  
> “numFound”: 1,  
> “start”: 0,  
> “docs”: [{  
> “caseTypes”: [  
> “Pre-Litigation Mediation”,  
> “Civil Laws”,  
> “Family Dispute”,  
> “Divorce”,  
> “Marriage Laws”,  
> “Dowry Issues”,  
> “Women Help & Safety”,  
> “HUF or Joint Family Law”,  
> “Muslim Laws”  
> ],

and not

> {  
> response = {  
> docs = (  
> {  
> “_version_” = 15352wqwd18967678812160;  
> active = (  
> 1  
> );  
> email = (  
> “[me@me.com](mailto:me@me.com)”  
> );  
> firstName = (  
> Nehlo  
> );  
> id = NehloTestUser1464098899306;  
> lastName = (  
> Test  
> );  
> mode = (  
> User  
> );  
> password = (  
> wear21zero  
> );  
> }  
> );

---

<div class="post-metadata">

**Author:** ![huynh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/huynh/32/103909_2.png) [@huynh](https://forums.kodeco.com/u/huynh)\
**Post date:** [August 16, 2016, 10:38am UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/18 "2016-08-16T10:38:43Z")

</div>

why I don’t see [unowned self] or [weak self] in this closure? This app will leak memory right, Ray.

> self.downloadTags(firstFileID) { tags in  
> self.downloadColors(firstFileID) { colors in  
> completion(tags: tags, colors: colors)  
> }  
> }

---

<div class="post-metadata">

**Author:** ![astralbodies](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/astralbodies/32/109036_2.png) [@astralbodies](https://forums.kodeco.com/u/astralbodies)\
**Post date:** [August 27, 2016, 7:35pm UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/19 "2016-08-27T19:35:54Z")

</div>

I can update the code to include `[weak self]` - it looks like at some point `[unowned self]` is used in the uploader which could crash if self is nil. The chance of a leak happening in the example is pretty minimal but I’ll update it for those using the example code as a basis for their own projects.

---

<div class="post-metadata">

**Author:** ![astralbodies](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/astralbodies/32/109036_2.png) [@astralbodies](https://forums.kodeco.com/u/astralbodies)\
**Post date:** [August 27, 2016, 7:37pm UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/20 "2016-08-27T19:37:37Z")

</div>

Actually I’m not really sure a retain cycle is possible here given the block is local to the function itself.

---

<div class="post-metadata">

**Author:** ![afinque](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/afinque/32/110937_2.png) [@afinque](https://forums.kodeco.com/u/afinque)\
**Post date:** [August 31, 2016, 3:39am UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/21 "2016-08-31T03:39:38Z")

</div>

Hi, Great tutorial so far (I’m almost done)!

I had some trouble with the tutorial, though, probably because I lean a little newb. The location of where to put this code wasn’t immediately clear:

**Next, add the following code to upload.responseJSON:**

I think it would be clearer to say: Next, add the following code after “upload.responseJSON { response in” or similar verbiage.

---

<div class="post-metadata">

**Author:** ![afinque](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/afinque/32/110937_2.png) [@afinque](https://forums.kodeco.com/u/afinque)\
**Post date:** [August 31, 2016, 3:52am UTC](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899/22 "2016-08-31T03:52:41Z")

</div>

It’s also not clear to me where this location is:

**Next, go back to uploadImage(\_:progress:completion:) and replace the call to the completion handler in the success condition with the following:**

[Next page](https://forums.kodeco.com/t/alamofire-tutorial-getting-started/3899.md?page=2)
