IOS File Download and open, iOS File Download open

Source: Internet
Author: User

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!

Related Article

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.