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