Web development tools for iOS development

Source: Internet
Author: User

Web development tools for iOS development

Doing mobile development often involves several modules:1, network detection 2, network request get and POST request 3, File upload 4, File download 5, breakpoint continuation

Now share these one by one with you and welcome to study and discuss this example using the Afnetworking framework

Network detection:


#pragma mark-reachability Management (IOS 6-7)


//network snooping (used to detect if the network can be linked.) This method is best placed in Appdelegate, allowing the program to open and start detecting the network .
-(void) Reachabilitymanager
{
Turn on network snooping
[Manager.reachabilitymanager startmonitoring];

Monitor network changes
[Manager.reachabilitymanager setreachabilitystatuschangeblock:^ (afnetworkreachabilitystatus status) {
Switch (status) {

When the network is unavailable (no network or request delay)
Case Afnetworkreachabilitystatusnotreachable:
Break

When it comes to cellular data networks and WiFi for mobile phones
Case Afnetworkreachabilitystatusreachableviawifi:
Case Afnetworkreachabilitystatusreachableviawwan:
Break

Other information
Default
Break
}
}];

Stop Network Monitoring (if you need to always detect network status, you can not stop to keep it running)
[Manager.reachabilitymanager stopmonitoring];
}


GET Request data:

#pragma mark-get Request (IOS 6-7)


GET request
-(void) Methodget
{
void request
if (manager) {
Manager = nil;
}

Create request
Manager = [Afhttprequestoperationmanager manager];

The parser for setting the request is Afhttpresponseserializer (for parsing data nsdata directly), and the default is Afjsonresponseserializer (for parsing JSON)
Manager.responseserializer = [Afhttpresponseserializer serializer];

Send a GET request
[Manager get:@ "interface Address" Parameters:nil success:^ (afhttprequestoperation *operation, id responseobject) {

Request succeeded (when parser is Afjsonresponseserializer)
NSLog (@ "Success:%@", responseobject);

Request succeeded (when parser is Afhttpresponseserializer)
NSString *jsonstring = [[NSString alloc] Initwithdata:responseobject encoding:nsutf8stringencoding];
NSLog (@ "success:%@", jsonstring);

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

Request failed
NSLog (@ "error:%@", error);
}];

}

POST request:

#pragma mark-post Request (IOS 6-7)


POST request
-(void) methodpost
{
void request
if (manager) {
Manager = nil;
}

Adding parameters
Nsdictionary *parameters = @{@ "Key": @ "Object",
@ "Key": @ "Object"};

Create request
Manager = [Afhttprequestoperationmanager manager];

The parser for setting the request is Afhttpresponseserializer (for parsing data nsdata directly), and the default is Afjsonresponseserializer (for parsing JSON)
Manager.responseserializer = [Afhttpresponseserializer serializer];

Send a POST request
[Manager post:@ "interface Address" Parameters:parameters success:^ (afhttprequestoperation *operation, id responseobject) {

Request succeeded (when parser is Afjsonresponseserializer)
NSLog (@ "Success:%@", responseobject);

Request succeeded (when parser is Afhttpresponseserializer)
NSString *jsonstring = [[NSString alloc] Initwithdata:responseobject encoding:nsutf8stringencoding];
NSLog (@ "success:%@", jsonstring);

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

Request failed
NSLog (@ "error:%@", error);
}];
}

Upload:
#pragma mark-upload Request (IOS 6-7)


Upload (upload as a form, take a picture for example)
-(void) methodupload
{
void request
if (manager) {
Manager = nil;
}

Adding parameters
Nsdictionary *parameters = @{@ "Key": @ "Object",
@ "Key": @ "Object"};

Create request
Manager = [Afhttprequestoperationmanager manager];

The parser for setting the request is Afhttpresponseserializer (for parsing data nsdata directly), and the default is Afjsonresponseserializer (for parsing JSON)
Manager.responseserializer = [Afhttpresponseserializer serializer];

Send a POST request to add a file that needs to be sent here as a picture
[Manager post:@ "interface Address" Parameters:parameters constructingbodywithblock:^ (id<afmultipartformdata> formData) {

Add a picture and compress it (0.0 is the maximum compression rate, and 1.0 is the minimum compression rate)
NSData *imagedata = uiimagejpegrepresentation ([UIImage imagenamed:@ "picture name (note suffix name)"], 1.0);

Add a file to upload, here is a picture
[FormData appendpartwithfiledata:imagedata name:@ "Server put picture parameter name (Key)" filename:@ "Picture name" mimetype:@ "file type (here is the picture format, such as image /jpeg) "];

} success:^ (afhttprequestoperation *operation, id responseobject) {

Request succeeded (when parser is Afjsonresponseserializer)
NSLog (@ "Success:%@", responseobject);

Request succeeded (when parser is Afhttpresponseserializer)
NSString *jsonstring = [[NSString alloc] Initwithdata:responseobject encoding:nsutf8stringencoding];
NSLog (@ "success:%@", jsonstring);

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

Request failed
NSLog (@ "error:%@", error);
}];

}

Download:

#pragma mark-download Request (IOS 6-7)


Download
-(void) MethodDownload
{
Download progress bar
Uiprogressview *downprogressview = [[Uiprogressview alloc] initwithprogressviewstyle:uiprogressviewstyledefault];
Downprogressview.center = Cgpointmake (self.view.center.x, 220);
downprogressview.progress = 0;
Downprogressview.progresstintcolor = [Uicolor Bluecolor];
Downprogressview.tracktintcolor = [Uicolor Graycolor];
[Self.view Addsubview:downprogressview];

Set the location where the files are stored (this demo saves the files in the Documents folder in the iphone sandbox.) For information on how to obtain the file path, please search by yourself.
Method One
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *cachesdirectory = [Paths objectatindex:0];
NSString *filepath = [cachesdirectory stringbyappendingpathcomponent:@ "filename"];
Method Two
NSString *filepath = [nsstring stringwithformat:@ "%@/documents/file name (note suffix)", nshomedirectory ()];

Path to print File save
NSLog (@ "%@", FilePath);

Create Request Management
Operation = [[Afhttprequestoperation alloc] initwithrequest:[nsurlrequest requestwithurl:[nsurl URLWithString:@ ""] ;

Add download request (get the server's output stream)
Operation.outputstream = [Nsoutputstream outputstreamtofileatpath:filepath append:no];

Set Download progress bar
[Operation setdownloadprogressblock:^ (Nsuinteger bytesread, Long long totalbytesread, long long Totalbytesexpectedtoread) {

Show Download Progress
CGFloat progress = ((float) totalbytesread)/totalbytesexpectedtoread;
[Downprogressview setprogress:progress Animated:yes];
}];

Request Management Judgment Request result
[Operation setcompletionblockwithsuccess:^ (afhttprequestoperation *operation, id responseobject) {

Request succeeded
NSLog (@ "Finish and Download to:%@", FilePath);

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

Request failed
NSLog (@ "error:%@", error);
}];

}

Breakpoint Continuation:

#pragma mark-download Management (IOS 6-7)


Start Download (breakpoint continuation)
-(void) Downloadstart
{
[Self methoddownload];
[Operation start];
}


Pause Download (breakpoint continuation)
-(void) Downloadpause
{
[Operation Pause];
}


Continue download (Breakpoint continuation)
-(void) Downloadresume
{
[Operation Resume];

}

IOS7 feature specific uploads and downloads:



#pragma mark-upload Request (IOS 7 only)


Upload (iOS7 dedicated)
-(void) methodUploadFor7
{
void request
if (SessionManager) {
SessionManager = nil;
}

Create request (IOS7 private)
SessionManager = [[Afurlsessionmanager alloc] Initwithsessionconfiguration:[nsurlsessionconfiguration Defaultsessionconfiguration]];

Add a Request interface
Nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:@ "upload address"]];

Add the uploaded file
Nsurl *filepath = [nsurl fileurlwithpath:@ "local file address"];

Send an upload request
Nsurlsessionuploadtask *uploadtask = [SessionManager uploadtaskwithrequest:request fromFile:filePath Progress:nil completionhandler:^ (nsurlresponse *response, id responseobject, Nserror *error) {
if (Error) {

Request failed
NSLog (@ "error:%@", error);

} else {

Request succeeded
NSLog (@ "Success:%@%@", response, Responseobject);
}
}];

Start uploading
[Uploadtask resume];
}


#pragma mark-download Request (IOS 7 only)


Download (iOS7 dedicated)
-(void) methodDownloadFor7
{
void request
if (SessionManager) {
SessionManager = nil;
}

Create request (IOS7 private)
SessionManager = [[Afurlsessionmanager alloc] Initwithsessionconfiguration:[nsurlsessionconfiguration Defaultsessionconfiguration]];

Add a Request interface
Nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:@ ""]];

Send a download request
Nsurlsessiondownloadtask *downloadtask = [SessionManager downloadtaskwithrequest:request progress:nil destination:^ Nsurl * (Nsurl *targetpath, Nsurlresponse *response) {

Set the location where the files are stored (this demo saves the files in the Documents folder in the iphone sandbox.) For information on how to obtain the file path, please search by yourself.
Nsurl *filepath = [Nsurl fileurlwithpath:[nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSUserDomainMask , YES) Firstobject]];

return [FilePath Urlbyappendingpathcomponent:[response Suggestedfilename]];

} completionhandler:^ (Nsurlresponse *response, Nsurl *filepath, Nserror *error) {

Download complete
NSLog (@ "Finish and Download to:%@", FilePath);
}];

Start download
[Downloadtask resume];

}

Source: http://download.csdn.net/detail/wangliang198901/7935199




Web development tools for iOS development

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.