iOS Development Web Chapter-File Download (one • unreasonable)

Source: Internet
Author: User

iOS Development Web Chapter-File Download (one • unreasonable)

One, small file download

If the file is small, the download method will be more

Directly with NSData's + (ID) Datawithcontentsofurl: (nsurl *) URL;

Philip? Nsurlconnection send an HTTP request to download

If you are downloading pictures, you can also use the Sdwebimage framework

Second, sand box

1. In the Finder, some of the system's files (repositories) are hidden and can display hidden files by running code in the terminal.

Show hidden system files:

Defaults write Com.apple.finder appleshowallfiles YES

2. Create an alias to view the app sandbox.

The recommended tool, Simpholders, can be used to view the sandbox.

Three, large file download example

hint : The project code, just for example, is not reasonable.

1 //2 //YYVIEWCONTROLLER.M3 //01-Downloading of files (unreasonable)4 //5 //Created by Apple on 14-6-30.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten  One @interfaceYyviewcontroller () A@property (nonatomic,strong) Nsmutabledata *FileData; --(ibaction) star; -  the @end -  - @implementationYyviewcontroller -  +- (void) Viewdidload - { + [Super Viewdidload]; A } at  --(ibaction) star { -     //Create a download path -      -Nsurl *url=[nsurl urlwithstring:@"Http://192.168.1.53:8080/MJServer/resources/videosres.zip"]; -      in     //Create a request -Nsurlrequest *request=[Nsurlrequest Requestwithurl:url]; to      +     //send a request (using proxy method) -Nsurlconnection *connt=[nsurlconnection Connectionwithrequest:requestDelegate: self]; the [connt start]; * } $ Panax Notoginseng #pragmaMark-nsurlconnectiondatadelegate Proxy method - /* the * When the response to the server is received (the server is connected) , the +  */ A-(void) Connection: (Nsurlconnection *) connection didreceiveresponse: (Nsurlresponse *) Response the { +     //Initialize Data -Self.filedata=[Nsmutabledata data]; $ } $  - /* - * Called when data is received from the server (may be called multiple times, only partial data is passed at a time) the  */ --(void) Connection: (Nsurlconnection *) connection didreceivedata: (NSData *) DataWuyi { the     //1.1 points to receive data.  -NSLog (@"receive data from the server! ---%d", data.length); Wu [Self.filedata Appenddata:data]; - } About  $ /* - * When the server's data is loaded, it will be called -  */ --(void) Connectiondidfinishloading: (Nsurlconnection *) Connection A { +     //Download Complete the     //large files do not put documents, you can put library\caches or TMP - //nsstring *fullpath=[caches stringbyappendingstring:@ "Video.zip"]; $      theNSString *caches =[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) lastobject]; theNSString *filepath = [caches stringbyappendingpathcomponent:@"Video.zip"]; the [Self.filedata Writetofile:filepath atomically:yes]; the      -NSLog (@"Download Complete"); in } the /* the * Request error (failure) when called (Request timeout \ network \ No net \, usually refers to client error) About  */ the-(void) Connection: (Nsurlconnection *) connection didfailwitherror: (Nserror *) Error the { the } +  - @end

Simulator:

Click on the screen to click on the download button to download the files on the local server.

Code Description:

In dealing with large file download problems, whether using synchronous or asynchronous download, is to get all the data at once, not advisable. Therefore, it is more appropriate to use the proxy way to handle.

This project, it took a long time to splice the data, and it took a long time to put it on the write system sandbox. And it consumes a lot of memory space. This approach is unreasonable, and a more appropriate approach should be to stitch data while writing to the hard disk.

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.