[IOS] File Download Note, ios note
File downloads are dividedNSURLConnectionAndNSURLSessionTwo types, the former has a long history of hate. It is relatively difficult to use. The latter is new and some additional features are added.
1. Download NSURLConnection
TIPS:
1. When NSURLConnection is downloaded and NSData is written to a file, data does not occupy much memory. (even if the file is large) 2. A little bit is being transferred. the disk cache is used. instead of memory cache. 3. Learn how to add a proxy to NSURLConnection. [ConsetDelegateQueue: [[NSOperationQueuealloc] init]
4. NSURLResponse records the url, mineType, exceptedContentLength, suggestedFileName, and other attributes. These attributes are required for download.
Follow these steps to track the download percentage (the URLConnection method ):
# Import "XNDownload. h "typedef void (^ ProgressBlock) (float percent); @ interface XNDownload () <NSURLConnectionDataDelegate> @ property (nonatomic, strong) NSMutableData * dataM; // file path saved in the sandbox @ property (nonatomic, strong) NSString * cachePath; // total file length @ property (nonatomic, assign) long fileLength; // The length of the downloaded file @ property (nonatomic, assign) long currentLength; // callback block code @ property (nonatomic, copy) Progre SsBlock progress; @ end @ implementation XNDownload-(NSMutableData *) dataM {if (! _ DataM) {_ dataM = [NSMutableData data];} return _ dataM;}-(void) downloadWithURL :( NSURL *) url progress :( void (^) (float )) progress {// 0. record block code self. progress = progress; // 1. request GET NSURLRequest * request = [NSURLRequest requestWithURL: url]; // 2. connection NSURLConnection * connection = [NSURLConnection connectionWithRequest: request delegate: self]; // enable connection to support multiple threads and specify the working queue of the proxy. // when NSURLConnection is running, the running cycle is not responsible for listening for the specific execution of the proxy [connection setDelegateQueue: [[NSOperationQueue alloc] init]; // 3. start connection [connection start];} # pragma mark-proxy method // 1. the server receives the response from the server. After the server executes the request, it returns the data-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) to the client *) response {NSLog (@ "% @ % lld", response. suggestedFilename, response. expectedContentLength); // 1. saved cache path NSString * cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject]; self. cachePath = [cachePath stringByAppendingPathComponent: response. suggestedFilename]; // 2. total file length self. fileLength = response. expectedContentLength; // 3. the length of the currently downloaded file self. currentLength = 0; // clear data [self. dataM setData: nil];} // 2. receive data from the server-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {// concatenate data [self. dataM appendData: data]; // increases the length of the currently downloaded file according to the data Length. currentLength + = data. length; float progress = (float) self. currentLength/self. fileLength; // determines whether block code if (self. progress) {[[NSOperationQueue mainQueue] addOperationWithBlock: ^ {// force run loop to execute an update [[[nsunloop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date. progress (progress) ;}}}// 3. complete receiving-(void) connectionDidFinishLoading :( NSURLConnection *) connection {NSLog (@ "% s % @", _ func __, [NSThread currentThread]); // write dataM to the cache directory of the sandbox // write data. The underlying implementation of NSURLConnection is to cache data using disks [self. dataM writeToFile: self. cachePath atomically: YES];} // 4. error-(void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error {NSLog (@ "% @", error. localizedDescription);} @ end
Ii. Download NSURLSession
NSURLSession can be used for resumable upload and download suspension.
1. session provides asynchronous download with multiple threads enabled. 2. Download pause and resume: (the method in the session proxy) * Get An NSData variable to save the download. when paused, the download task is cleared. * resume: Send the paused data to the session to continue the download and clear the previous data. 3. The task must be resume before execution.
# Import "XNViewController. h "@ interface XNViewController () <NSURLSessionDownloadDelegate> // download network session @ property (nonatomic, strong) NSURLSession * session; // download task @ property (nonatomic, strong) NSURLSessionDownloadTask * downloadTask; // The binary data that is resumed @ property (nonatomic, strong) NSData * resumeData; @ end @ implementation XNViewController-(NSURLSession *) session {if (! _ Session) {NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration]; _ session = [NSURLSession sessionWithConfiguration: config delegate: self delegateQueue: nil];} return _ session;}-(void) viewDidLoad {[super viewDidLoad]; [self downloadFile];} // pause the download task-(IBAction) pause {// if the download task does not exist, directly return if (self. downloadTask = nil) return; // pause the task (the resumeData in the block code is the binary data being downloaded) // When stopping a download task, you need to save the data [self. downloadTask cancelByProducingResumeData: ^ (NSData * resumeData) {self. resumeData = resumeData; // clear and release the current download task self. downloadTask = nil;}] ;}- (IBAction) resume {// is the data to be resumed? If (self. resumeData = nil) return; // creates the resume download task self. downloadTask = [self. session downloadTaskWithResumeData: self. resumeData]; [self. downloadTask resume]; // clears the resume data of the previous record. resumeData = nil;} // If the cache directory is used in development, you must provide a function to "Clear cache "! /** Download file */-(void) downloadFile {NSString * urlStr = @ "http: // localhost/master set. rmvb "; urlStr = [urlStr failed: NSUTF8StringEncoding]; NSURL * url = [NSURL URLWithString: urlStr]; // (1) proxy & Direct start/2. start the download task self. downloadTask = [self. session downloadTaskWithURL: url]; [self. downloadTask resume] ;}# pragma mark-download proxy method-(void) URLSession :( NSURLSession *) session downloadT Ask :( NSURLSessionDownloadTask *) downloadTask didFinishDownloadingToURL :( NSURL *) location {NSLog (@ "%@%@", location, [NSThread currentThread]);}/** bytesWritten: totalBytesWritten: Number of downloaded bytes Bytes: Total download size */-(void) URLSession :( NSURLSession *) session downloadTask :( NSURLSessionDownloadTask *) downloadTask didWriteData :( int64_t) bytesWritten totalBytesWritten :( int64_t) TalBytesWritten progress :( int64_t) progress {float progress = (float) totalBytesWritten/progress; [[NSOperationQueue mainQueue] addOperationWithBlock: ^ {// Update progress UI operations in the main thread .... }];}/** Resume proxy method */-(void) URLSession :( NSURLSession *) session downloadTask :( callback *) downloadTask didResumeAtOffset :( int64_t) fileOffset expectedTotalBytes :( int64_t) expectedTotalBytes {NSLog (@ "offset: % lld", fileOffset);} @ end
Source: http://blog.csdn.net/xn4545945
Downloaded IOS System File
After unzipping the system image, there are generally 2-3 installation tools. You can try them one by one. If none of them work, change the system image.
Where can I download ios system files?
There are many websites that I believe you will like.
Www.ankty.com/