I want to use an action extension to transfer a PDF file from Safari to my host application.
If the user opens a PDF file in Safari, the extension should copy the local file to my app.
Somehow the NSItemProvider only has Items conforming to the type 'public.url'.
This is the web-URL to the file, but not the local filepath that I want.
Can someone point me in the right direction here?
I came across the same scenario yesterday. What I did was
let url: URL = (result as? URL)!
let path: String = url.path
if path.hasSuffix(".pdf") {
//download the data from url and write to a pdf file
}
Related
I've searched and found some ways where I can get the video asset/file of the LivePhoto asset. But what those methods do are as follows:
They somehow get the resources(PHAssetResource) for the LivePhoto asset
Select the specific video resource(PHAssetResource) from previously found resources.(Ex: resource.type==PHAssetResourceTypePairedVideo)
Save the resource data to a new File(!)
Get the URL for the newly created File.
But, what I want to know, is there any way I can get the URL for the originally stored file URL, without creating a new File?
[For other types of photos/videos I'm able to get the URLs of the originally stored files. The problem is with the video of the LivePhoto asset.]
I have found a way to directly get the original URL of the file. There is no need to save the file contents in a new file. (Tested on iOS12+)
We first get the file URL of associated Image of the LivePhoto.
asset.requestContentEditingInput(with: nil) { (editingInput, info) in
if let input = editingInput, let imgUrl = input.fullSizeImageURL {
// use imgUrl
}
}
The Video file is stored in the same directory as the image file. So, we use the Image URL but with different extension to find the Video file of the LivePhoto. The video file extension is either .mov or .MOV.
if let videoPath = (imagePath.deletingPathExtension as NSString).appendingPathExtension("MOV") {
let videoUrl = URL(fileURLWithPath: videoPath)
// check if file exist and use
// If MOV(Uppercase) extension file doesn't exist check for mov(lowercase) extension similarly
}
I have an epub file in the project and I would like to open it with in the application i have tried using "EpubExtractor" pod but no use.
Now I am trying to achive that if i can use the path to view that file on the webview.
guard let resourceUrl = Bundle.main.url(forResource: "Sway",withExtension: "epub") else {return}
webView.loadFileURL(resourceUrl,allowingReadAccessTo: resourceUrl)
path I am getting.
file:///Users/apple/Library/Developer/CoreSimulator/Devices/4203B9E1-E3A4-4636-8750-E5340AAD51ED/data/Containers/Bundle/Application/F3BF662A-34AD-44C0-AFD9-E4BB7AD74551/EpubWebView.app/Sway.epub
When I am using the above code i am not able to view anything on the webview as it gives me an error "not able to load in webview"
ePub format is not supported by the WKWebView framework. You can search Apple or webkit.org to see the core features of WKWebView
I would like to share a file (gpx) via the Gmail client app on the iPhone.
The problem is that the created mail does not contain the "shared" .gpx file.
Code for creating the share request:
let itemProver = GPXItemProvider(itemInformation) // Subclass of UIActivityItemProvider
let shareController = UIActivityViewController(activityItems:[itemProver], applicationActivities: nil)
present(controller: shareController)
The code itself does work nevertheless because if I try to export the gpx file into the default mail client on the device, everything works fine.
Do I miss something which requires the GMail app to contain the file as the attachement?
This topic covers a similar problem. A third-party library is used there though, which is not an option in my case:
Send an iphone attachment through email programmatically
Thank you.
You can write the GPX file to a temporary directory and share the URL instead of your custom item.
let data = ...
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("test.gpx")
try data.write(to: url)
let shareController = UIActivityViewController(activityItems:[url], applicationActivities: nil)
I have defined a UTI for a custom document format. I can export files from my app and append them to text messages, email, etc. I can import the files into my app by tapping on the document icon in iMessage. By tapping on the document icon, I have the option to copy to my app. That triggers a call in my AppDelegate to handle the incoming file.
What's bugging me is that the url for the incoming file is:
file:///private/var/mobile/Containers/Data/Application/21377C94-1C3C-4766-A62A-0116B369140C/Documents/Inbox/...
Whereas, when saving documents to the .documents directory, I use this URL:
file:///var/mobile/Containers/Data/Application/21377C94-1C3C-4766-A62A-0116B369140C/Documents/...
The difference being the /private/ and /Inbox/ path components.
Question: how can I purge the /private/.../Inbox/ path of the files that were copied to my app from iMessage? I noticed this when testing my app and when I tapped on the same document icon in iMessage it started generating file copies with the same name but adding -1, then -2, then -3 to the file name of the document from iMessage. It appears that copies are building up in that /private/.../Inbox/ path.
Is that something that gets flushed on its own or can I access that directory to remove those files? It's also annoying because based upon the filename, it appears to be a different file thus allowing multiple copies of the same file to be imported with a slightly different file name.
Ok, this took a fair amount of digging, but I'll post my solution that seems to work thus far in case anyone runs across the same problem.
let fileManager = FileManager.default
// get the URL for the "Inbox"
let tmpDirURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("Inbox")
// get all the files in the "Inbox" directory
let anythingThere = try? fileManager.contentsOfDirectory(at: tmpDirURL, includingPropertiesForKeys: nil)
if anythingThere?.count ?? 0 > 0 {
for eachURL in anythingThere! {
// for each url pointing to a file in that directory, get its path extension
let pathExtension = eachURL.pathExtension
// test to see if it's a UTI that you're interested in deleting
// in my case, the three "OCC" strings are the relevant UTI extensions
if pathExtension == "OCCrcf" || pathExtension == "OCCrdi" || pathExtension == "OCCsrf" {
// attempt to delete the temporary file that was copied to the
// "Inbox" directory from importing via email, iMessage, etc.
try fileManager.removeItem(at: eachURL)
}
}
}
If anyone has a more elegant solution, please respond as well. Thanks.
I am very new to this IOS swift programming. So need some suggestion and help. I am creating an app in which I have to show different file coming from the server. Below is the kind of link from which my file data is coming.
http://demo.xxxx.com/dctm-rest/repositories/iol_ref2/objects/0900a18480383d14/content-media?format=pdf&modifier=&page=0
This above link is for pdf or it can be of any type (txt, doc, img etc) so I have tried so many things but I was unable to get a result.
Solutions What I have tried:-
With basic auth, I am requesting URL and getting contents from the file, so I was getting data from server and printing on console but when I tried to copy that contents to one file it says unable to copy says no such file or folder exist but I have properly defined the path location. Below is my code link which I have asked earlier.
How to read data from file placed in server through url link in ios swift (4)?
Someone suggested using Alamofire lib that also I have used but no luck. Below is the code which I have referred.
Alamofire: finished with error - code: -1001
So let me know what I am doing is correct or not or is there any new method or way I can try. I have tried web view too. So please help me with this.
Thanks in advance
I use code like this to download a file and store it locally.
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent(documentFilename)
return (fileURL, [.removePreviousFile])
}
Alamofire.download(url, to: destination).response { [unowned self] response in
// check download and call completion handler
}
What code did you use to download the file?