How to save document to "Files" App in swift [duplicate] - swift

This question already has answers here:
How to write a file to a folder located at Apple's Files App in Swift
(2 answers)
Closed 3 years ago.
I just created a PDF document in my app and I want to save the document on the device in a way that the "Files" app can access the saved document.
When you download a PDF document in Safari there is an option to save the document to the "Files" app, which is what I'm trying to do.
I Googled for several hours but wasn't able to find any useful methods.

If you have access to your PDFdata then you could present the user with a UIActivityViewController where they can then save it to Files, or share it with any of the other available options:
let activityViewController = UIActivityViewController(activityItems: ["Name To Present to User", pdfData], applicationActivities: nil)
present(activityViewController, animated: true, completion: nil)

Minimum of iOS 11
The Files app was introduced in iOS 11. You will not be presented with the option to save to Files when you are on a version less than iOS 11. With that being said, documents can be downloaded to a variety of different apps depending on the settings available on the user's phone and the options you allow.
#available(iOS 11.0, *)
func savePDF() {
guard let documentData = document.dataRepresentation() else { return }
let activityController = UIActivityViewController(activityItems: [documentData], applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
}
This is an example of a way to save a document. You can't force it to be downloaded and show up in their Files app. You have to present a UIActivityViewController that shows options on what they can do with that document.
If you want to exclude any options shown on the UIActivityViewController, then implement excludedActivityTypes like so.
activityController.excludedActivityTypes = [.airDrop, .postToTwitter]

Try UIDocumentInteractionController:
let doc = UIDocumentInteractionController(url: url)
doc.presentOptionsMenu(from:in:animated:)
There's an option named save to Files.

Related

how can i let a user pick a local document?

I'm supposed to test PSPDFKit in a way that lets the user open a locally saved pdf file.
For this i require the files url(e.g. path, i guess). How can i let the user pick a local document?
Can anybody help me out?
I'm using storyboard and swift.
You can present a PDF document in PSPDFKit using the PDFViewController like so:
// Create the `Document`.
let fileURL = Bundle.main.url(forResource: "Document", withExtension: "pdf")!
let document = Document(url: fileURL)
// Create the PDF view controller.
let pdfController = PDFViewController(document: document)
// Present the PDF view controller within a `UINavigationController` to enable the toolbar.
present(UINavigationController(rootViewController: pdfController), animated: true)
For more details, take a look at the following guide here.
Also, to present a document that your end-user has selected, consider using a document picker or a file browser. Please refer to DocumentPickerSidebarExample.swift from Catalog and SwiftUI Document Browser for runnable example projects.
In the future, please reach out to our support portal at pspdfkit.com/support/request/ - we're happy to provide support there for our commercial SDK.

Unable to open MS Word and PDF docs with SFSafariViewController in latest iOS 13.0

We are trying to use SFSafariViewController to browse a Word document from our iOS Mobile app using Swift as it gives ReaderView and Font Increase/Decrease within the App. Everything works as expected on the Simulator with 12.2 ios version, however when we published the changes to TestFlight and started using the app on a real phone ios 13.0 version the SFSafariViewController shows the FileName and says "Open in 'Dropbox' 'More... '"
https://i.stack.imgur.com/R595d.jpg
we want to disable/remove that option to choose different apps to open the word/pdf docs and display the content directly within the app. Would greatly appreciate if someone can help remove this option and directly display the documents like below.
https://i.stack.imgur.com/EhFec.png
Below is the code. Can someone please help how to resolve this issue?
func openGuidelineSafari(passedURL: String) {
let allowedCharacterSet = CharacterSet(charactersIn: " ").inverted
let escapedString = passedURL.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet)
let url = URL (string: (escapedString!))
let config = SFSafariViewController.Configuration()
config.entersReaderIfAvailable = true
let vc = SFSafariViewController(url:url!, configuration: config)
vc.delegate = self
present(vc, animated: true)
}

Sharing PDF - blank from Mail.app but OK over Airdrop

I'm sharing a PDF using the built in Share activity action sheet using the following code:
if let pdf = pdfData {
let vc = UIActivityViewController(activityItems: [pdf], applicationActivities: [])
if (vc.responds(to: #selector(getter: popoverPresentationController))) {
vc.popoverPresentationController?.sourceView = btnShare;
}
self.present(vc, animated: true)
} else {
showTextAlertWithImage...
...
}
When the sharing popover is displayed, tapping on Airdrop and sharing to a nearby computer the PDF everything works as expected.
But if I choose Mail from the sharing list the PDF is blank. Does anyone please know why?
It's the same PDF that's being shared in both situations. If it matters, the mail account is an Outlook account.
Sharing the PDF through the Gmail app also produces the correct PDF, however the attachment does not include the '.pdf' file extension.
Check how to add Content Type to your activity item you are sharing.

how to add a custom entry in UIActivityViewController

When i open an attachment from iPhone or iPad mail application, there is a popup that can transfert the file to a third party application.
This iOS popup is called UIActivityViewController.
I wand to add my own iOS application (written in swift) in this popup and i do not know how to do...
Thanks
I believe you are asking about two separate things.
UIActivityViewController:
Here's my code in my app.
let vc = UIActivityViewController(activityItems: [shareImage], applicationActivities: [])
vc.excludedActivityTypes = [
UIActivityType.airDrop,
UIActivityType.assignToContact,
UIActivityType.addToReadingList,
//UIActivityType.copyToPasteboard,
//UIActivityType.mail,
//UIActivityType.message,
//UIActivityType.openInIBooks,
//UIActivityType.postToFacebook,
//UIActivityType.postToFlickr,
UIActivityType.postToTencentWeibo,
//UIActivityType.postToTwitter,
UIActivityType.postToVimeo,
UIActivityType.postToWeibo,
UIActivityType.print,
//UIActivityType.saveToCameraRoll
]
present(vc,
animated: true,
completion: nil)
vc.popoverPresentationController?.sourceView = self.view
vc.completionWithItemsHandler = {(activity, success, items, error) in
}
Pay attention to the excludedActiviyTypes code. My app is an image effect app, so I commented out the things I wish to have in the popup - Weibo, print, assignToContact, ReadingList, Vimeo, and TencentWeibo. Here's the thing - I kept the full list of activities for quick reference. (I've never found it on ADC and end up getting it by using code complete.)
UIActivityType is an extended struct, so if you want to go the route of using it this way I think you'll have to (1) subclass UIActivityViewController, (2) write your own extension to UIActivityType, and (3) provide any/all code needed to use a service that isn't listed above.
A Document Provider is what I think you are looking for. Either that, or Universal - sometimes called Deep - Linking. (The links are to Apple documentation for using each.)
I've obviously used UIActivityiewController, but haven't (yet) had any need to these other two.
If you have questions about setting up a UIActivityViewController, post them in the comments. If someone else has a way to "extend" it in any way other than I described, let me know and I'll remove this answer.

Is it possible to show the app UI portion of iOS sharing extension into a page control in SWIFT?

I am using the following code to share text using iOS sharing extension (UIActivityViewController).
let vc = UIActivityViewController(activityItems: ["File URL"], applicationActivities: [])
presentViewController(vc, animated: true, completion: nil)
It shows a popup to share using text sharing applications like following:
But, I want the sharing app portion in my view into a page control like below:
How can I do this? If there is any alternative way to achieve this requirement, kindly suggest.