Reputation: 2315
I have a UIWebView
that loads a PDF using Basic Auth like so
NSURL* url = [[NSURL alloc] initWithString:_urlString];
NSString *authStr = [NSString stringWithFormat:@"%@:%@", usr, pwd];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength]];
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url];
[mutableRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
[_webView loadRequest:mutableRequest];
Is there a way for me to easily save this PDF file onto disk? I've tried this but failed:
NSData *fileData = [[NSData alloc] initWithContentsOfURL:_webView.request.URL];
My URL looks like this: www.domain.com/files/foo.pdf
Upvotes: 2
Views: 2517
Reputation: 1225
Another Approach:- You can download the PDF file Data and save it in Temp Directory or Your Prefered Directory. then use that saved directory to open the file in WebView.
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else {
return
}
//MARK:- Now Saving the Document Data into Directory. I am using the temp directory you can change it to yours.
let tempDirectory = URL(fileURLWithPath: NSTemporaryDirectory())
self.tmpURL = tempDirectory.appendingPathComponent(response?.suggestedFilename ?? "fileName.png")
do {
try data.write(to: self.tmpURL! )
} catch {
print(error)
}
DispatchQueue.main.async {
//MARK:- Instead of using MIME type and NSData .now you can use the file directory to open the file in WEBView
self.webView.loadFileURL(self.tmpURL!, allowingReadAccessTo: self.tmpURL!)
}
}
Upvotes: 0
Reputation: 1225
For Swift :
let webView = UIWebView()
let url = URL(string: "")!
let urlRequest : URLRequest = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: urlRequest , completionHandler: { (data, urlResponse, error) in
if (data != nil){
DispatchQueue.main.async {
webView.load(data!, mimeType: "application/pdf", textEncodingName: "UTF-8", baseURL: url)
}
}
})
task.resume()
Upvotes: 1
Reputation: 26036
-initWithContentsOfURL:
method of NSData
does a simple HTTP GET, but you needed to set some Authorization params that's why it fails.
To avoid downloading twice the data, you could use NSURLSession
to download it, save it, and use the -loadData:MIMEType:textEncodingName:baseURL:
of UIWebView
to load it.
NSMutableURLRequest *mutableRequest = //Your Custom request with URL and Headers
[[[NSURLSession sharedSession] dataTaskWithRequest:mutableRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
{
if (data)
{
//SaveDataIntoDisk
dispatch_async(dispatch_get_main_queue(), ^(){
[_webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil];
});
}
}] resume];
Upvotes: 2