AFN upload file method, AFN monitoring network status, reachability monitoring network status

Source: Internet
Author: User

AFN How to upload files

{

//1. Create a manager

Afhttprequestoperationmanager *mgr = [Afhttprequestoperationmanager manager];

//2. Send Request

[Mgr POST:@ "http://localhost/upload/upload.php" parameters:nil constructingbodywithblock:^ ( ID<AFMultipartFormData> formData) {

//FormData: Set the parameters required to upload the file , two ways to upload :

//<1> upload via URL of local file

{

NSString *fromfile = @ "/users/likaining/desktop/meinv.jpg";

Nsurl *url = [Nsurl urlwithstring:@ "file:///Users/likaining/Desktop/meinv.jpg"];

//URL: file path to upload files

//Name: The file name received by the server .

//FileName: The name of the file saved in the server

///MimeType: file type

[FormData appendpartwithfileurl:url Name:@ "UserFile" fileName:@ "MEINV" mimeType:@ "Image/jpg" error:NULL];

}

//<2> upload binary data via file

{

NSData *data = [NSData datawithcontentsoffile:zipfile];

[FormData appendpartwithfiledata:data Name:@ "UserFile" fileName:@ "Meinv.zip" mimeType:@ "gzip" ];

}

} success:^ (Afhttprequestoperation *operation, ID responseobject) {

// callback after successful upload

NSLog (@ "%@", responseobject);

} failure:^ (Afhttprequestoperation *operation, Nserror *error) {

// callback after upload failed

NSLog (@ " failed ");

}];

}

/*-------------------------------------- Monitor network status -----------------------------------------* /

1.AFN monitoring network status 2. Understanding reachability Monitoring network status

{

1. AFN monitoring network status

{

// Create network Status Manager

Afnetworkreachabilitymanager *mgr = [Afnetworkreachabilitymanager Sharedmanager];

// monitor changes in network status

[Mgr setreachabilitystatuschangeblock:^ (afnetworkreachabilitystatus status) {

// Call this block when the network status changes

switch (status) {

case Afnetworkreachabilitystatusreachableviawifi:

NSLog (@ "WiFi network ");

break;

case Afnetworkreachabilitystatusreachableviawwan:

NSLog (@ " cellular network ");

break;

case afnetworkreachabilitystatusnotreachable:

NSLog (@ " no network ");

break;

case Afnetworkreachabilitystatusunknown:

NSLog (@ " Unknown network ");

break;

default:

break;

}

}];

// start monitoring

[Mgr Startmonitoring];

}

2.reachability monitoring network status

{

// register to notify the viewer, when the network status changes , receive notifications !

[[Nsnotificationcenter Defaultcenter] Addobserver:self selector:@selector( internetstatuschanged) Name:kreachabilitychangednotification object:nil];

// when the controller is destroyed , remove the Notify Viewer .

-(void) Dealloc

{

[[Nsnotificationcenter Defaultcenter] Removeobserver:self];

}

// depending on the current network status , make a different response .

-(void) internetstatuschanged

{

NSLog (@ " network status changed ");

if ([reachability reachabilityforlocalwifi].currentreachabilitystatus = = Reachableviawifi) {

NSLog (@ "WiFi network ");

}

if ([reachability reachabilityforinternetconnection].currentreachabilitystatus = = Reachableviawwan) {

NSLog (@ " cellular network ");

}

if ([reachability reachabilityforinternetconnection].currentreachabilitystatus = = notreachable)

{

NSLog (@ " no network ");

}

}

// Create reachability objects and start monitoring changes in network status

-(void) Monitorinternetstatus

{

reachability *czreachability = [reachability reachabilityforinternetconnection];

[Czreachability Startnotifier];

self. reachability = czreachability;

}

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event

{

[self monitorinternetstatus];

}

}

}

AFN upload file method, AFN monitoring network status, reachability monitoring network status

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.