AlamoFireDemo

//
//  ViewController.swift
//  AlamFireDemo
//
//

import UIKit
import Alamofire

class ViewController: UIViewController {
    lazy var defManager : Manager = {
        //1、get addtional http header
        var defHeaders = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:]
        //2、set a NSURLSessionConfiguration
        let conf = NSURLSessionConfiguration.defaultSessionConfiguration()
        conf.HTTPAdditionalHeaders = defHeaders
        
        //3、geneter a manager
        let manager = Alamofire.Manager(configuration: conf)
        return manager
        
    }()
    // 后台下载
    lazy var backgroundManager :Manager = {
        
        var defHeaders = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:]
        
        let conf = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.example.app.backgroud")
        let manager = Alamofire.Manager(configuration: conf)
        return manager
    }()
    
    
    lazy var ephemeralManager :Manager = {
        
        var defHeaders = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:]
        
        let conf = NSURLSessionConfiguration.ephemeralSessionConfiguration()
        let manager = Alamofire.Manager(configuration: conf)
        return manager
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        Alamofire.request(.GET, "https://httpbin.org/get").response { (request, response, data, error) -> Void in
            print(request)
            print(response)
            print(data)
            print(data.dynamicType)
            print(error)
            print(error.dynamicType)
        }.responseString { (response) -> Void in
            print("String ==========")
            switch response.result {
            case .Success(let str):
                    print("(str.dynamicType)")
                    print("(str)")
            case .Failure(let error):
                    print("(error)")
            
            }
        }.responseJSON { (response) -> Void in
            print("JSON ==========")
            
            switch response.result {
            
            case .Success(let json):
                let dic = json as! Dictionary<String,AnyObject>
                
                let origin = dic["origin"] as! String
                let headers = dic["headers"] as! Dictionary<String,String>
                print("origin :(origin)")
                print("user-agent :(headers["User-Agent"])")
            case .Failure(let error):
                print(error)
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

1、创建AlamFireDemo 工程,关闭工程
2、进入到工程目录 执行 pod init 命令 生成 PodFile文件
3、vi PodFile编辑该文件

启用:platform :ios, '8.0'

use_frameworks!

在end之前粘贴 pod 'Alamofire','~> 3.0’,执行pod install
4、安装完成之后打开白色工程
open -a Xcode AlamoFireDemo.xcworkspace

  

原文地址:https://www.cnblogs.com/sallet/p/5302961.html