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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值