iOS development afnetworking for data transfer and file upload

Source: Internet
Author: User

//Transfer data

1Afhttprequestoperationmanager *manager =[Afhttprequestoperationmanager manager];2Manager.responseserializer =[Afhttpresponseserializer serializer];3Manager.requestserializer =[Afhttprequestserializer serializer];4 5 //2. Set Login Parameters6Nsdictionary *dict = @{@"username": Username,@"Password":p Assword,@"Birthday": Birthday,@"Gender": Gender,@"Mobile": Mobile,@"Avstar": Avster};7 //3. Request8[Manager POST:@"Http://127.0.0.1/HelloServer/index.php/users/insert"Parameters:dict success: ^ (Afhttprequestoperation *operation,IDresponseobject) {9 TenNSString *result =[[NSString alloc] Initwithdata:responseobject encoding:nsutf8stringencoding]; OneNSLog (@"POST--%@,%@", result, [Nsthread CurrentThread]);//automatically return to main thread AUIWindow *window =[[UIApplication sharedapplication] Keywindow]; -Window.rootviewcontroller =[Appdelegate Tabbarcontroller]; - the} failure: ^ (afhttprequestoperation *operation, Nserror *error) { -NSLog (@"%@", error); -}];

//File Upload

1NSData *data =uiimagepngrepresentation (image);2NSDateFormatter *formatter =[[NSDateFormatter alloc] init];3Formatter.dateformat =@"YYYYMMDDHHMMSS";4NSString *str =[Formatter stringfromdate:[nsdate Date]];5NSString *filename = [NSString stringWithFormat:@"%@.png", str];6NSLog (@"filename:%@", fileName);7Afhttprequestoperationmanager *manager =[Afhttprequestoperationmanager manager];8Manager.responseserializer =[Afhttpresponseserializer serializer];9Manager.requestserializer =[Afhttprequestserializer serializer];Ten OneNsdictionary *parameters = @{@"Foo":@"Bar"}; A[Manager POST:@"http://localhost/HelloServer/index.php/index/upload/"Parameters:parameters constructingbodywithblock:^ (ID<AFMultipartFormData>formData) { -[FormData appendpartwithfiledata:data Name:@"file"Filename:filename MimeType:@"Image/png"]; -} success:^ (Afhttprequestoperation *operation,IDresponseobject) { theNSLog (@"Success:%@", responseobject); -} failure:^ (Afhttprequestoperation *operation, Nserror *error) { -NSLog (@"Error:%@", error); -}];

iOS development afnetworking for data transfer and file upload

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.