#IBAction func clickedViewData(_ sender: Any) {
let fileManager = FileManager.default
let documentoPath = (self.getDirectoryPath() as NSString).appendingPathComponent("zip_2MB.zip")
if fileManager.fileExists(atPath: documentoPath){
let documento = NSData(contentsOfFile: documentoPath)
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [documento!], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView=self.view
present(activityViewController, animated: true, completion: nil)
}
else {
print("document was not found")
}
}
i have one Zip file i want to share as a .zip directly..but cant get proper solution to share .zip file..when i share file its share as data file.. i read some documents but cant get proper way for share zip file in activity controller.. please help to solve this problem.
Thank You
let fileManager = FileManager.default
let documentoPath = ("self.getDirectoryPath()" as NSString).appendingPathComponent("zip_2MB.zip")
let documento = NSData(contentsOfFile: documentoPath)
var url = URL(fileURLWithPath: NSTemporaryDirectory() + ("My File.txt"))
documento?.write(to: url, atomically: false)
// create activity view controller
var activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
Related
I am trying to share an address through UIActivityViewController.
This is how I get the URL:
func vCardURL(from coordinate: CLLocationCoordinate2D, with name: String?) -> URL {
let vCardFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("vCard.loc.vcf")
let vCardString = [
"BEGIN:VCARD",
"VERSION:4.0",
"FN:\(name ?? "Shared Location")",
"item1.URL;type=pref:http://maps.apple.com/?ll=\(coordinate.latitude),\(coordinate.longitude)",
"item1.X-ABLabel:map url",
"END:VCARD"
].joined(separator: "\n")
do {
try vCardString.write(toFile: vCardFileURL.path, atomically: true, encoding: .utf8)
} catch let error {
print("Error, \(error.localizedDescription), saving vCard: \(vCardString) to file path: \(vCardFileURL.path).")
}
return vCardFileURL
}
func didTapShareButton() {
let coordinate = CLLocationCoordinate2D(latitude: 52.520007, longitude: 13.404954)
let url = self.vCardURL(from: coordinate, with: "Berlin")
let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil)
//Here it opens the UIActivityViewController
present(activityViewController, animated: true, completion: nil)
}
URL Output:
file:///Users/edoardodecal/Library/Developer/CoreSimulator/Devices/E1A8A47C-ABF7-4048-ACB1-0AC91E7E0B5B/data/Containers/Data/Application/F79E6A32-B2E7-4137-A75B-AFDE3294A1C2/tmp/vCard.loc.vcf
This is the result:
What I expect:
Any hints? Thanks
Forget all this stuff. The only thing you need is to share the Apple Maps URL with the coordinates... That's it! Tested on iOS 14 with UIKit and SwiftUI.
if let url = URL(string: "https://maps.apple.com?ll=\(latitude),\(longitude)") {
let activity = UIActivityViewController(activityItems: [url], applicationActivities: nil)
}
And then show the View Controller...
I have a WKWebView which displayed pdf. I want to share the file to another devices such as iPad, iPhone.... using the share button. I tried to display the pdf in preview so it will have the iOS share button the code below.
import UIKit
import WebKit
class ShowPDFView: UIViewController, UIDocumentInteractionControllerDelegate {
#IBAction func SharePDFFile(_ sender: Any) {
let fileName = "testPDF"
guard let urlPath = Bundle.main.url(forResource: fileName, withExtension: "pdf") else {return}
let controller = UIDocumentInteractionController(url: urlPath)
controller.delegate = self
controller.presentPreview(animated: true)
}
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController!) -> UIViewController! {
return self
}
func documentInteractionControllerViewForPreview(controller: UIDocumentInteractionController!) -> UIView! {
return self.view
}
func documentInteractionControllerRectForPreview(controller: UIDocumentInteractionController!) -> CGRect{
return self.view.frame
}
I got runtime error.
[MC] Reading from private effective user settings.
The preview does not loaded. Does anyone know why?
This function works for me. In Swift 4
#IBAction func SharePDFFile(_ sender: Any) {
let fm = FileManager.default
var pdfURL = (fm.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
pdfURL = pdfURL.appendingPathComponent("johnMilky.pdf") as URL
//Rename document name to "myFile.pdf"
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("johnMilkyFile.pdf") as NSURL
do {
let data = try Data(contentsOf: pdfURL)
try data.write(to: url as URL)
let activitycontroller = UIActivityViewController(activityItems: [url], applicationActivities: nil)
if activitycontroller.responds(to: #selector(getter: activitycontroller.completionWithItemsHandler))
{
activitycontroller.completionWithItemsHandler = {(type, isCompleted, items, error) in
if isCompleted
{
print("completed")
}
}
}
//activitycontroller.excludedActivityTypes = [UIActivity.ActivityType.airDrop]
activitycontroller.popoverPresentationController?.sourceView = self.view
self.present(activitycontroller, animated: true, completion: nil)
}
catch {
print(error)
}
}
I have a problem, I would like to sent an Image with E-Mail, Message and also AirDrop. Currently only E-Mail and Message works, when I try to sent it per AirDrop, the Device get an .data File, which I can't open. How I can fix this problem ?
#IBAction func sendItButton(_ sender: UIBarButtonItem) {
let imageShare = [ image! ]
let activityViewController = UIActivityViewController(activityItems: imageShare , applicationActivities: nil)
activityViewController.popoverPresentationController?.barButtonItem = sender
self.present(activityViewController, animated: true, completion: nil)
}
I have faced similar issue today, seems like there's a bug on UIActivityViewController if the UIImage is originated from a .jpeg image from bundle. I used the file URL of the image (instead of UIImage) as the activityItems and it worked fine.
private func urlForBundleFile(filename: String) -> URL? {
let components = filename.components(separatedBy: ".")
return Bundle.main.url(forResource: components[0], withExtension: components[1])
}
let imageURL = urlForBundleFile(filename: "someImage.jpeg")
let activityViewController = UIActivityViewController(activityItems: [imageURL] , applicationActivities: nil)
I have write this code to show pdf using UIDocumentInteractionController.But,I don't know how to search pdf at local directory and open in iOS 8 and below..Any help?
let filename = history.invoiceLongDate // 01223642
if !filename.isEmpty{
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docs = paths[0]
let pathURL = NSURL(fileURLWithPath: docs, isDirectory: true)
if #available(iOS 9.0, *) {
let fileURL = NSURL(fileURLWithPath: "\(filename)_my_invoice.pdf", isDirectory: false, relativeToURL: pathURL)
self.docController = UIDocumentInteractionController(URL: fileURL)
self.docController?.delegate = self
self.docController?.presentOptionsMenuFromRect(sender.frame, inView: self.view, animated: true)
} else {
// Fallback on earlier versions
// Any Help with that?
}
}
You can view PDF in iOS 8 by using webview. Try below code,
if let pdf = NSBundle.mainBundle().URLForResource("myPDF", withExtension: "pdf", subdirectory: nil, localization: nil) {
let req = NSURLRequest(URL: pdf)
let webView = UIWebView(frame: CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height))
webView.loadRequest(req)
self.view.addSubview(webView)
}
OR
if let baseUrl = NSURL.fileURLWithPath(pathURL) {
let fileURL = baseUrl.URLByAppendingPathComponent(NFConstants.NFCoreDataStringIdentifiers.CoreDataStoresPathComponent.rawValue)
}
Hope this will be helpful to you.
UIDocumentInteractionController is available with (iOS 3.2, *).
For Viewing PDF file:
var documentInteractionController: UIDocumentInteractionController!
#IBAction func openDocument(sender: UIButton) {
let URL: NSURL = NSBundle.mainBundle().URLForResource("pdf-sample", withExtension: "pdf")!
if (URL != "") {
// Initialize Document Interaction Controller
self.documentInteractionController = UIDocumentInteractionController(URL: URL)
// Configure Document Interaction Controller
self.documentInteractionController.delegate = self
// Present Open In Menu
self.documentInteractionController.presentOptionsMenuFromRect(sender.frame, inView: self.view, animated: true)
//presentOpenInMenuFromRect(button.frame, inView: self.view, animated: true)
}
}
// MARK: UIDocumentInteractionControllerDelegate
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
return self
}
How can i save my pdf stored in my iphone to ibooks with activityController?
My code is ` pageSize = CGSizeMake (850, 1100)
let fileName: NSString = "Cars.pdf"
let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory: AnyObject = path.objectAtIndex(0)
let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName as String)
self.generatePDFs(pdfPathWithFileName)
//lines written to get the document directory path for the generated pdf file.
if let documentsPath = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first?.path {
print(documentsPath)
let File = [documentDirectory]
let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: File, applicationActivities: nil)
presentViewController(activityViewController, animated: true, completion: nil)`
but it only shows the documentDirectory in a mail.. i need something to say.. hey there is a pdf file...