zoukankan      html  css  js  c++  java
  • 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

      

  • 相关阅读:
    linux命令
    常用正则表达式总结
    List集合对象根据字段排序
    IO字 节流/字符流 读取/写入文件
    Jquery广告浮动效果小案例
    拿到添加对象的id号方法
    Jquery省市区三级联动案例
    JAVA集合迭代遍历和特性介绍
    Listener监听器使用小案例
    java中用过滤器解决字符编码问题
  • 原文地址:https://www.cnblogs.com/sallet/p/5302961.html
Copyright © 2011-2022 走看看