Search code examples
iosswiftswift2alamofire

Integrate Alamofire 2 Manually without using Framework


I recently upgraded to Swift 2 and XCode 7. I was using Alamofire just by copying Alamofire.swift in my project. But now I want to use Alamofire 2 and I downloaded and got latest Alamofire.swift.

I am getting a lot of errors in Alamofire.swift like:

Use of undeclared type 'Request'

error screenshot

Also Method was an enum of HttpMethods, but now its typeAlias for COpaquePointer error2 screenshot

I know that it can be done through CocoaPods but I don't want to use frameworks. Whats the best way to integrate it now?

P.S.: I want to support iOS 8+


Solution

  • You must download all .swift files from Alamofire repository. Between Alamofire 1 and 2 there are lots of things that changed. For example Request and responseJSON block returns three things, NSURLRequest?, NSHTTPURLResponse?,Result<AnyObject> struct which has both the json data as json.value and json.error as ErrorType

    Following code example shows how to use it with Xcode 7 and Swift 2

    var defaultCfg: NSURLSessionConfiguration  {
        let cfg = NSURLSessionConfiguration.defaultSessionConfiguration()
        cfg.HTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
        cfg.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicy.Always
        cfg.URLCache = nil
        cfg.HTTPAdditionalHeaders = ["User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:40.0) Gecko/20100101 Firefox/40.0", "Accept-Language":"en-US,en;q=0.5"]
        return cfg
    }
    self.mngr = Manager(configuration: cfg)
        self.mngr.request(.GET, url,parameters:parameters)
            .responseJSON { (req, res, json) in
                if(json.error != nil) {
                  //  NSLog("Error: \(error)")
                    failure(res, json.value, someerroryouset)
                    return
    
                }
                else {
                    let jsond = JSON(json.value!)