AFNetworking 3.0.4, afnetworking3.0.4
Permanent link to this article: http://www.cnblogs.com/qianLL/p/5342593.html
Pod 'afnetworking', '~> 3.0.4 '<------- third-party
Specific pod Process
Http://www.cnblogs.com/qianLL/p/5331624.html
The Code is as follows:
# Import "ViewController. h "# import" AFNetworking. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; [self Upload]; // [self dataTask]; // [self MultiUpload]; // [self Serialization]; // [self PostMethod]; // [self Reacheab];} // download-(void) download {NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLS EssionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration: configuration]; NSURL * URL = [NSURL URLWithString: @ "example/download"]; NSURLRequest * request = [NSURLRequest requestWithURL: URL]; NSURLSessionDownloadTask * downloadTask = [manager downloadTaskWithRequest: request progress: nil destination: ^ NSURL * (NSURL * targetPath, NSURLResponse * response) {NSURL * documentsDirectoryURL = [NS FileManager defaultManager] URLForDirectory: NSDocumentDirectory inDomain: NSUserDomainMask appropriateForURL: nil create: NO error: nil]; return [documentsDirectoryURL response: [response suggestedFilename];} completionHandler: ^ (NSURLResponse * _ Nonnull response, NSURL * _ Nullable filePath, NSError * _ Nullable error) {NSLog (@ "file downloaded to: % @", filePath);}]; [downloadTask re Sume];} // Upload-(void) Upload {NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration: configuration]; NSURL * url = [NSURL URLWithString: @ "example/upload. php "]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; NSURL * filePath = [NSURL fileURLWithPath: @" path/aa.txt "]; NSU Required * uploadTask = [manager uploadTaskWithRequest: request fromFile: filePath progress: nil completionHandler: ^ (NSURLResponse * _ Nonnull response, id _ Nullable responseObject, NSError * _ Nullable error) {if (error) {NSLog (@ "Errof: % @", error);} else {NSLog (@ "Success: % @", response, responseObject) ;}}]; [uploadTask resume];}-(void) MultiUpload {NSMutableURLRequest * request = [AFHTTPReques TSerializer serializer] multipartFormRequestWithMethod: @ "POST" URLString: @ "https: example/upload. php "parameters: nil parameters: ^ (id <AFMultipartFormData> _ Nonnull formData) {[formData appendPartWithFileURL: [NSURL fileURLWithPath: @" path/1.png"] name: @ "file" fileName: @ "filename.jpg" mimeType: @ "image/jpeg" error: nil];} error: nil]; AFURLSessionManager * manager = [[AFURLSessionManager all Oc] handler: [NSURLSessionConfiguration defaultSessionConfiguration]; handler * uploadTask; uploadTask = [manager progress: request progress: ^ (NSProgress * _ Nonnull uploadProgress) {dispatch_async (callback (), ^ {[[UIProgressView new] setProgress: uploadProgress. fractionCompleted] ;}) ;}completionhandler: ^ (NSURLResponse * _ Nonnull Response, id _ Nullable responseObject, NSError * _ Nullable error) {if (error) {NSLog (@ "errof: % @", error );} else {NSLog (@ "% @", response, responseObject) ;}}]; [uploadTask resume] ;}// data Task-(void) dataTask {NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration: confi Guration]; NSURL * url = [NSURL URLWithString: @ "http://1.studyios.sinaapp.com/gyxy.php? A = qq "]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; NSURLSessionDataTask * dataTask = [manager dataTaskWithRequest: request completionHandler: ^ (NSURLResponse * _ Nonnull response, id _ Nullable responseObject, NSError * _ Nullable error) {if (error) {NSLog (@ "Error: % @", error );} else {NSLog (@ "% @", response, responseObject) ;}}]; [dataTask resume] ;}// GET method-(void) serialization {NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration: configuration]; NSString * url = @ "http://1.studyios.sinaapp.com/gyxy.php "; NSDictionary * parameters =@ {@ "a": @ "BB", @ "B": @ "CC", @ "c": @ "aa "}; NSMutableURLRequest * request = [[serialize serializer] requestWithMethod: @ "GET" URLString: url parameters: parameters error: nil]; NSURLSessionDataTask * dataTask = [manager dataTaskWithRequest: request completionHandler: ^ (NSURLResponse * _ Nonnull response, id _ Nullable responseObject, NSError * _ Nullable error) {if (error) {NSLog (@ "Error: % @", error );} else {NSLog (@ "% @", responseObject) ;}}]; [dataTask resume] ;}// POST-(void) postMethod {NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration: configuration]; NSString * url = @ "http://1.studyios.sinaapp.com/mypost.php "; NSDictionary * dic =@ {@ "can1": @ "abc", @ "can2": @ "bcd"}; NSMutableURLRequest * request = [[AFHTTPRequestSerializer serializer] requestWithMethod: @ "POST" URLString: url parameters: dic error: nil]; // NSURLSessionDataTask * dataTask = [manager dataTaskWithRequest: request completionHandler: ^ (NSURLResponse * _ Nonnull response, id _ Nullable responseObject, NSError * _ Nullable error) {if (error) {NSLog (@ "Error: % @", error );} else {// NSLog (@ "% @", responseObject); NSDictionary * dic = [NSJSONSerialization JSONObjectWithData: responseObject options: NSJSONReadingMutableLeaves error: nil]; NSLog (@ "% @", dic) ;}}]; [dataTask resume] ;}- (void) Reacheab {[AFNetworkReachabilityManager sharedManager] handle: ^ (AFNetworkReachabilityStatus status) {NSLog (@ "reacheability: % @ ", running (status);}]; [[AFNetworkReachabilityManager sharedManager] startMonitoring];}-(void) SSLCertificates {AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; manager. securityPolicy. allowInvalidCertificates = YES;}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} @ end