Afnetworking is an easy-to-use third-party web development framework for IOS that makes it easy to get JSON data from a URL address.
When using it, I use the Package Manager Cocoapods do not understand:
Cocoapods Installation: http://www.cnblogs.com/foxting/p/4520758.html
Ruby Installation: http://www.cnblogs.com/foxting/p/4520829.html
1. Navigate to the root of the project using the CD command in the terminal, my current project name is fresh
Then enter in the terminal: VI podfile will open an edit page, in the editing page input, afnetworking do not enter the version number default to the latest version
Platform:ios, ' 8.0 '
Pod ' afnetworking '
Exit VI and save file
Under Command mode, click the ":" colon key to enter "last line mode", for example: w filename (enter "w filename" to save the article with the filename you specified): Wq (Input " wq", disk and Exit VI): q! (Enter q!, do not save the mandatory exit VI) Exit VI after saving the file, execute the following command to install the Podfilepod installation after a few minutes of waiting after the terminal has a similar following prompts, Open the directory where the project is located you will find a file that ends in. xworkspace, and then open the project with this file open projects have two home directories, such as: Because Afnetworking is written in Object-c, you first add a. h header file Then add a line to the file
#import <AFNetworking/AFNetworking.h>
The header file is then configured within the project's build settings
Press COMMAND+B to compile the project when you are finished configuring it.
Then add code within the Viewcontroller, such as the downward use of foreign free weather forecast JSON interface, originally wanted to call the domestic stock AIP, found that the failure should not be returned every time the content-type= "text/html" it does not accept it.
Override Func Viewdidload () { super.viewdidload () //Do any additional setup after loading the view, typically fro M a nib. var requestmanager = Afhttprequestoperationmanager () //let url = "http://apis.haoservice.com/lifeservice/stock/ HS " //let params = [" gid ":" sh601009 "," Key ":" 2d13447002d14f35a0f33f4dfa405c93 "] let url ="/http Api.openweathermap.org/data/2.5/weather "let params = [" lat ": 37.79," lon ": -122.41," CNT ": 0]
The closure is written like jquery, requestmanager.get (URL, parameters:params, success: {operation: afhttprequestoperation!,responseobject:anyobject!) -Void in var stock = Responseobject as? Nsdictionary }, failure: {(operation:afhttprequestoperation!,error:nserror!), Void in println ( Error) } ) }
Use afnetwroking to access network data in Swift