IOS-use AFNetworking3.0 + (latest AFN) and afnetworking3.0afn

Source: Internet
Author: User

IOS-use AFNetworking3.0 + (latest AFN) and afnetworking3.0afn
0. Preparations for importing the framework • 1. Drag the AFNetworking3.0 + framework program into the project • 2. Or use Cocopod to import AFNetworking3.0 + • 3. import # import "AFNetworking. h"1. UI preparation

//. Hfile # import <UIKit/UIKit. h> @ interface ViewController: UIViewController @ property (weak, nonatomic) IBOutlet UIImageView * imageView; // upload progress bar @ property (weak, nonatomic) IBOutlet UIProgressView * progressView; @ end

 

2. Details of File Upload using AFN
-(Void) uploadImage {/* if you need to modify this code segment, you can adjust the position 1. put upload. php is changed to the address provided by website developers. 2. change file to the field name notified by website developers * // AFN3.0 + the Handle Based on the blocked HTPPSession AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; NSDictionary * dict =@{ @ "username ": @ "Saup"}; // formData: Used to splice the data to be uploaded. A data body to be uploaded is generated at this location [manager POST: @ "http: // 192.168.1.111: 12345/upload. php "parameters: dict constructingBodyWithBlock: ^ (id <AFMultipartFormData> _ Nonnull formData) {UIImage * image = [UIImage imageNamed: @" moon "]; NSData * data = UIImagePNGRepresentation (image); // during network development, during file upload, the file cannot be overwritten, and the file name must be duplicated. // you need to solve this problem, // you can use the current system event as the file name NSDateFormatter * formatter = [[NSDateFormatter alloc] init] During upload; // you can set the time format formatter. dateFormat = @ "yyyyMMddHHmmss"; NSString * str = [formatter stringFromDate: [NSDate date]; NSString * fileName = [NSString stringWithFormat: @ "symbol @.png", str]; // upload/* This method parameter 1. [binary data] 2. the corresponding website [upload. [field "file"] 3. [file name] 4. [mimeType] */[formData appendPartWithFileData: data name: @ "file" fileName: fileName mimeType: @ "image/png"];} progress: ^ (NSProgress * _ Nonnull uploadProgress) {// upload progress // @ property int64_t totalUnitCount; total file size to be downloaded // @ property int64_t completedUnitCount; size of the downloaded file /// Add a listener for the Progress SS (@ "% f", 1.0 * uploadProgress. completedUnitCount/uploadProgress. totalUnitCount); // return to the main queue column to refresh the UI. The custom progress bar is dispatch_async (dispatch_get_main_queue (), ^ {self. progressView. progress = 1.0 * uploadProgress. completedUnitCount/uploadProgress. totalUnitCount;});} success: ^ (NSURLSessionDataTask * _ Nonnull task, id _ Nullable responseObject) {NSLog (@ "uploaded successfully % @", responseObject);} failure: ^ (NSURLSessionDataTask * _ Nullable task, NSError * _ Nonnull error) {NSLog (@ "Upload Failed % @", error);}];}
4. Check network status-optimize User Experience
-(Void) viewDidLoad {[super viewDidLoad]; // Network Monitoring handle AFNetworkReachabilityManager * manager = [AFNetworkReachabilityManager sharedManager]; // you need to monitor the network connection status, you must first call the startMonitoring method of the Singleton [manager startMonitoring]; [manager setReachabilityStatusChangeBlock: ^ (AFNetworkReachabilityStatus status) {// status: // response =-1, unknown // AFNetworkReachabilityStatusNotReachable = 0, not connected // required = 1, 3G // AFNetworkReachabilityStatusReachableViaWiFi = 2, wireless connection NSLog (@ "% d", status);}];}

 

· You can view the logs of earlier versions of AFNetworking3.0 or earlier:

IOS-use AFNetworking (AFN)-to upload files

 

Author: Clear Saup

Source: http://www.cnblogs.com/qingche/

The copyright of this article is shared by the author and the blog. You are welcome to repost it, but you must keep this statement and provide a connection to the original article on the article page.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.