IOS File Download and open, iOS File Download open
A recent project needs to download an online report, so after completing the process, I am taking a look at the ideas to achieve my needs.
Let's just talk about the code.
First, obtain the link to the network file to determine whether the segment needs to be downloaded again or directly opened.
# Pragma mark Download Report
/// Step 1
// Whether to download or open the file
-(Void) downloadPDF :( NSString *) downloadUrl {
NSArray * array = [downloadUrl componentsSeparatedByString: @ "/"]; // array of multiple elements separated from character/
NSString * file = [array lastObject];
If ([self isFileExist: file]) {
// Obtain the file path under Documents
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * path = [paths objectAtIndex: 0];
NSString * pathString = [path stringByAppendingFormat: @ "/% @", file];
NSLog (@ "path: % @", pathString );
[Self loadDocument: pathString];
} Else {
// Download again
[Self downloadFile: downloadUrl];
}
}
# Pragma mark: Step 2: Determine whether this file exists in the sandbox
-(BOOL) isFileExist :( NSString *) fileName
{
// Obtain the file path under Documents
NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * path = [paths objectAtIndex: 0];
NSString * filePath = [path stringByAppendingPathComponent: fileName];
NSFileManager * fileManager = [NSFileManager defaultManager];
BOOL result = [fileManager fileExistsAtPath: filePath];
NSLog (@ "this file already exists: % @", result? @ "Yes": @ "does not exist ");
Return result;
}
/// // Step 3
// Download PDF
-(Void) downloadFile :( NSString *) downLoadUrl {
_ Weak typeof (self) weakSelf = self;
[Self hudTipWillShow: YES];
[DataService downloadTaskWithURL: downLoadUrl completion: ^ (id result ){
NSLog (@ "% @", result );
NSProgress * downloadProgress = result;
If (weakSelf. HUD ){
WeakSelf. HUD. progress = downloadProgress. fractionCompleted;
_ HUD. labelText = [NSString stringWithFormat: @ "% 2.f% %", downloadProgress. fractionCompleted * 100];
}
} FilesPath: ^ (id filesPath ){
[_ RePortDwn setBackgroundImage: [UIImage imageNamed: @ "downLoad"] forState: UIControlStateNormal];
// NSLog (@ "% @", filesPath );
NSURL * urlString = filesPath;
NSString * string = [urlString absoluteString];
NSArray * array = [string componentsSeparatedByString: @ "/"]; // an array of two elements separated from character
NSString * file = [array lastObject];
NSLog (@ "filePathName =: % @", file );
[WeakSelf hudTipWillShow: NO];
}];
}
/// // Step 4
// Display downloaded files with webview
-(Void) loadDocument :( NSString *) documentName
{
UIWebView * webView = [[UIWebView alloc] initWithFrame: CGRectMake (0, 64, kSCREEN_WIDTH, kSCREEN_HEIGHT)];
[Self. view addSubview: webView];
NSURL * url = [NSURL fileURLWithPath: documentName];
NSURLRequest * request = [NSURLRequest requestWithURL: url];
[WebView loadRequest: request];
}
Finally, you can call the method in step 1 directly.
There is no resumable upload in this case, and it will be updated later!