Swift网络请求库Alamofire

Swift网络请求库Alamofire,第1张

概述Alamofire由cnoon大神编写的基于swift的网络请求库 Github下载地址 [TOC] 运行限制: iOS 8.0+ / Mac OS X 10.9+ / tvOS 9.0+ / watchOS 2.0+ Xcode 7.3+ CocoaPods安装: 1.下载CocoaPods $ gem install cocoapods CocoaPods 0.39.0+ is require

Alamofire由cnoon大神编写的基于swift的网络请求库
Github下载地址
[TOC]

运行限制: iOS 8.0+ / Mac OS X 10.9+ / tvOS 9.0+ / watchOS 2.0+ Xcode 7.3+ CocoaPods安装:

1.下载CocoaPods

$ gem install cocoapods

CocoaPods 0.39.0+ is required to build Alamofire 3.0.0+.
2.修改Podfile文件:

source 'https://github.com/CocoaPods/Specs.git'platform :ios,'9.0'use_frameworks!pod 'Alamofire','~> 3.3'

3.下载:

$ pod install

使用Alamofire 发出请求

import Alamofire
Alamofire.request(.GET,url)

响应处理
Alamofire.request(.GET,url,parameters: ["key": "value"])         .responseJsON { response in             print(response.request)               print(response.response)              print(response.data)                print(response.result)              if let JsON = response.result.value {                 print("JsON: \(JsON)")             }         }
响应JsON处理
Alamofire.request(.GET,url)         .responseJsON { response in             deBUGPrint(response)         }
http方法
public enum Method: String { case OPTIONS,GET,head,POST,PUT,PATCH,DELETE,TRACE,CONNECT }
上传文件
let fileURL = NSBundle.mainBundle().URLForResource("Default",withExtension: "png")Alamofire.upload(.POST,file: fileURL)
上传进度
Alamofire.upload(.POST,file: fileURL)         .progress { bytesWritten,totalBytesWritten,totalBytesExpectedToWrite in             print(totalBytesWritten)             // This closure is NOT called on the main queue for performance             // reasons. To update your ui,dispatch to the main queue.             dispatch_async(dispatch_get_main_queue()) {                 print("Total bytes written on main queue: \(totalBytesWritten)")             }         }         .responseJsON { response in             deBUGPrint(response)         }
下载
Alamofire.download(.GET,url) { temporaryURL,response in    let fileManager = NSfileManager.defaultManager()    let directoryURL = fileManager.URLsForDirectory(.documentDirectory,inDomains: .UserDomainMask)[0]    let pathComponent = response.suggestedfilename    return directoryURL.URLByAppendingPathComponent(pathComponent!)}
http头部
Alamofire.request(.GET,headers: ["key":"value"])         .responseJsON { response in             deBUGPrint(response)         }
总结

以上是内存溢出为你收集整理的Swift网络请求库Alamofire全部内容,希望文章能够帮你解决Swift网络请求库Alamofire所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1076890.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存