Nsurlsession implementation Download (Cannot see download progress)
/**
* Download task: Cannot see download progress
*/
- (void) Downloadtask
{
//1.GetSessionObject
nsurlsession *session = [nsurlsessionsharedsession];
//2.Create a downloadTask
Nsurl *url = [Nsurlurlwithstring:@ "Http://localhost:8080/MJServer/resources/test.mp4"];
Nsurlsessiondownloadtask *task = [Session Downloadtaskwithurl: URLCompletionhandler:^(Nsurl*location, Nsurlresponse *response,Nserror *error) {
//Location: path to temporary files (downloaded files)
NSString *caches = [Nssearchpathfordirectoriesindomains(nscachesdirectory,Nsuserdomainmask,YES) Lastobject];
//Response.suggestedfilename : recommended file name, generally consistent with the server-side file name
NSString *file = [Cachesstringbyappendingpathcomponent: Response.Suggestedfilename];
// cut or copy a temporary fileCachesfolder
Nsfilemanager *mgr = [NsfilemanagerDefaultmanager];
//Atpath: file path before clipping
//Topath: the file path after clipping
[Mgr Moveitematpath: Location.PathTopath: FileError:Nil];
}];
//3.Start Task
[Task Resume];
}
Note: nsurlsessiondownloadtask After the download is complete, save the data in the TMP temp file in the sandbox, you need to cut or copy the temporary files Caches Folder.
Nsurlsession implementation Download (Cannot see download progress)