English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
0. Prepare for framework import
1. Drag the framework program into the project
2. Add iOS framework references
–SystemConfiguration.framework –MobileCoreServices.framework
3. Introduce
#import "AFNetworking.h"
4. Modify xxx-Prefix.pch file
#import <MobileCoreServices/MobileCoreServices.h> #import <SystemConfiguration/SystemConfiguration.h>
1.The AFN client initializes with a base URL and also instantiates an operation queue for subsequent multithreading processing
@interface ViewController () { // The AFN client initializes with a base URL and also instantiates an operation queue for subsequent multithreading processing AFHTTPClient *_httpClient; NSOperationQueue *_queue; } - (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL URLWithString:@"http://192.168.3.255/~apple/qingche"]; _httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; _queue = [[NSOperationQueue alloc] init]; }
2. Details of file upload operation implemented by AFN
#pragma mark - File upload - (IBAction)uploadImage { /* This section of code can be modified if necessary, and the position can be adjusted 1. Change upload.php to the address informed by the website developer 2. Change file to the field name informed by the website developer */ // 1. httpClient->url // 2. Upload request POST NSURLRequest *request = [_httpClient multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { // Generate the data body to be uploaded at this location // form corresponds to the form in the html file UIImage *image = [UIImage imageNamed:@"头像"1"]; NSData *data = UIImagePNGRepresentation(image); // In network development, when uploading files, it is not allowed to overwrite files or have duplicate filenames // To solve this problem, // You can use the current system time as the filename when uploading NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // Set time format formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *str = [formatter stringFromDate:[NSDate date]]; NSString *fileName = [NSString stringWithFormat:@"%@.png", str]; /* This method parameter 1. Binary data to be uploaded 2. Corresponds to the [file field "file"] in the [upload.php] file processing on the website 3. 要保存在服务器上的[文件名] 4. 上传文件的[mimeType] */ [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"image/png"]; }; // 3. operation包装的urlconnetion AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Upload completed"); failure:^(AFHTTPRequestOperation *} *operation, NSError NSLog(@"Upload failed", error) {->%@", error); }; //Execution [_httpClient.operationQueue addOperation:op];
That's all for this article. I hope it will be helpful to everyone's learning and that everyone will support the呐喊 tutorial more.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)