Today is a brief talk on how to use the more popular Afnetworking request framework in Swift Engineering.
Since afnetworking currently does not have a swift version, our project uses Cocoapod to install afnetworking.
This is my prodfile file, the use of cocoapod is the same as the use of OC is the same time, in order to facilitate the project, we randomly create a class, select the language OC, We will find that the project will come out with a swiftstudy-bridging-header.h file that deletes the created OC class, introduced here
OK, you can use afnetworking in the swift file.
- Platform:ios, ' 7.0 '
- Pod ' sdwebimage ', ' ~> 3.7.1 '
- Pod ' cocoahttpserver ', ' ~> 2.3 '
- Pod ' routinghttpserver ', ' ~> 1.0.0 '
- Pod ' masonry ', ' ~> 0.6.1 '
- Pod ' asihttprequest ', ' ~> 1.8.2 '
- Pod ' Mbprogresshud ', ' ~> 0.9.1 '
- Pod ' afnetworking ', ' ~> 2.5.4 '
When using OC for project development, we made a simple external encapsulation of afnetworking, and today we will implement it with Swift.
Use the new version afnetworking know it has a afhttpsessionmanager, where we inherit Afhttpsessionmanager use Singleton mode to return an instance of the class,
To do a network operation the single example in Swift is very different from OC syntax, and swift syntax is not much explained here, please refer to the SWIFT programming Language, let's take a look at the code
- //
- Requestclient.swift
- Swiftstudy
- //
- Created by Yang Wende on 15/8/19.
- Copyright (c) 2015 Yang Wende. All rights reserved.
- //
- import uikit
- class requestclient: afhttpsessionmanager {
li>
- class var sharedinstance : requestclient {
- struct Static {
- static var oncetoken: dispatch_once_t = 0
- static var instance:requestclient? = nil
- }
-
- dispatch_once (&Static.onceToken, { () -> Void in
- //string Fill in the appropriate baseurl to
- var url:nsurl = nsurl (string: "")!
- static.instance = requestclient (Baseurl: url)
- }) &NBSP
- //Returns an instance of this class
- return static.instance!
-
- }
- }
With the example of requestclient, we can post, get, and upload image files to the package.
Use afnetworking in OC to use block for data transfer and so on, in swift we use closures for data transmission
This is my file directory.
Swiftstudy------------Engineering Catalogue
Appdelegate.swift
Myviewcontroller.swift
Viewcontroller.swift
Network------------Network related
Requestclient.swift
Requestapi.swift
CUSTOMVC------------VC Encapsulation,
Costomnavigationvc.swift
Baseviewcontroller.swift
Here we mainly take the Chinese Weather Network API test A put request, look at the requestapi.swift.
- override func viewdidload () {
- super.viewdidload ()
-
- self.view.backgroundcolor = uicolor.browncolor ()
- var image:uiimage The
- //navigationbar left button customization, reference file baseviewcontroller.swift
- Self.leftbutton (nil, hliimage: nil, title: "Back", size: cgsize (width: 38, height: 30), action: "ButtonClick", target: self)
-
- var dic:dictionary= ["A": "A", "B": "B", "C": "C"];
-
-
- //post test
- Requestapi . POST ("List", body: dic, succeed: succeed, failed:failed)
- //get test
- var geturl:string = "http://m.weather.com.cn/atad/101190101.html"  
- Requestapi.get (GETURL, BODY: NIL, SUCCEED: SUCCEED, FAILED:&NBsp;failed)
-
- //File upload
- // do any additional setup& nbsp;after loading the view.
- }
- func succeed (Task: nsurlsessiondatatask!,responseobject:anyobject!) ->void{
- println ("oh my god succeeded + (Responseobject)")
- }
-
- func failed (task:nsurlsessiondatatask!,error:nserror!) ->void{
- println ("oh shit failed")
- }
In Viewcontroller we get the data by calling Requestapi's class method gets, and we define two methods succeed and failed, and look at the parameters in the method and the Afhttpsessionmanager
The parameter of the return data is the same, so that we get the data is the server to return all the data, call Equestapi get method will speak a function pointer passed past, the network request after the success of the function pointer will return the data.
Then we can do other work.
Use of the new version of Afnetworking will often encounter 3084 1011 and 1016 errors, the occurrence of these errors is generally a data parsing error, change the corresponding data types within the afnetworking can also be defined according to the actual situation.
- /**
- RequestClient.sharedInstance.requestSerializer = Afjsonrequestserializer ()
- RequestClient.sharedInstance.responseSerializer = Afjsonresponseserializer ()
- RequestClient.sharedInstance.requestSerializer.setValue ("application/json,text/html", Forhttpheaderfield: " Accept ")
- RequestClient.sharedInstance.requestSerializer.setValue ("Application/json; Charset=utf-8 ", Forhttpheaderfield:" Content-type ")
- */
To sum up, afnetworking is already using the most network framework, the further simple outer encapsulation of afnetworking, we can avoid the network request when a lot of initialization work, directly use a single example can, this improves
Efficiency, but also save a lot of code, at least a look at their own looks more comfortable, by using Swift to encapsulate afnetworking, but also systematically familiar with Swift's basic syntax, data types, as well as a single case, closures, etc. to use,
For Swift beginners, the actual learning effect is still more obvious, with these now you can use Swift to write a simple embarrassing hundred or news, and so on what that, as long as their own forward, it will not be the last.