Saving, Printing and Emailing data from a ScrollView

Hi, I am at the final hurdle in my first app and as I am a novice coder I hope someone can steer me in the right direction with a solution to a problem:-

My app is a medical ‘decision making tool’ for clinicians that involves the medic entering various client medical data via pickers, sliders and switches. This data is then collated, processed and presented in an on-screen ScrollView report. This report displays the declared client data plus recommendations as to the action that should be carried out by the clinician based on the data submitted. (The ScrollView actually contains all possible clinical actions but the non-relevant text is ‘hidden’ dependant on the entered client data).

So far so good, the app is working in terms of the results displayed on the iPhone or iPad screen. However, legislation requires medics to store records of client consultations and so my last step is to try to print or save or email the report from within the app.

I have added a Toolbar to the foot of the ScrollView with the usual export symbol as a BarButton. What I would like to happen is when the medic taps this button , the displayed report is converted to a PDF which could be stored using the usual users apps such as iBooks, Save to Local Files or E-Mailed to the medic’s clinic address.

However, I do not know how to do this! I have read that I may need to use ‘Core Data’ or somehow convert the UIView to a PDF.

This is a big ask but I am hoping an experienced Code Crew Community member can help and advise me on this.

Kind regards,
Rhys

Hi Rhys! Sounds like a great app!

CoreData is for storing data locally on a phone. I’d begin googling “how to create a PDF” and you’ll need to know how to use the “share sheet” which is what pops up when you click export and can then save/email the file

Hi @Rhys

There are several tutorials about building PDFs floating around. It seems like it is a little more advanced coding, see what you think.

Below is some basic code for exporting a comma delimited txt file the can be open in any spread sheet. I found starting with a csv file to get basic export going is a good first step.

Good luck.


//get the data from the array, in my case either  from the list array or the task array, I have  Bool - t that I check to see which one I am exporting.

// create a file
        let fileName = fName
        let path = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileName)!

        //add a title text
        var csvText = title

//        for loop to build the array
        if t {
            for res in tasks! {
                let newLine =
                "\(res.taskName ?? ""),\(res.important), \(res.complete)\n"
                csvText.append(contentsOf: newLine)
            }
        } else {
            for res in list! {
                let newLine =
                "\(res.name ?? "" ),\(res.share)\n"
                csvText.append(contentsOf: newLine)
            }
        }

        // save data to csv file
        do {
            try csvText.write(to: path, atomically: true, encoding: String.Encoding.utf8)
        } catch {

            //alert the user the file did not save
            Alerts.showAlert(title: "Attention", message: "Sorry, the shared file failed to save.", vc: self)
        }

        // share VC
        let vc = UIActivityViewController(activityItems: [path], applicationActivities: [])

        //for ipad
        if let popoverController = vc.popoverPresentationController {
            popoverController.sourceView = self.view
            popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
            popoverController.permittedArrowDirections = []
        }

        //Present the VC
        present(vc, animated: true, completion: nil)

Blessings,
—Mark

Hi Mikaela,

Thank you for replying to my post.
I have found a few references to creating PDF’s on Google and Stack Overflow. Unfortunately, these are quite old and use previous versions of Xcode. The Apple Xcode PDF library has been upgraded and even though I have tried mimicking the methods outlined in these resources I end up with “red errors” all over my code! As I am very new to programming and coding, so far I have not been able to unravel these errors. :sob:

I will study Apple’s documentation again and keep trying - I will let you know how I get on!

Thanks,
Rhys

Hi Mark,

Thank you very much for replying to my post.

You are right - it seems that exporting data from within an app requires code which is currently beyond my knowledge. I have tried using a few PDF creation methods listed on Google/Stack Overflow without success.

However, I must admit I had not thought of exporting the data as a CSV file! Thanks for this great idea and the code you posted.

I will try this and let you know how I get on.

Take care of yourself during the current horrible Pandemic,
Rhys