Aug 26 2019
, Swift 5, iOS 13, Xcode 11Swift 5, iOS 13, Xcode 11
Learn how to create a PDF, work with Core Text and Core Graphics and share the created document by building an app that displays the user’s input on a flyer that can be shared with other iOS apps.
By Bill Morefield.
Open FlyerBuilderViewController.swift and replace the contents of shareAction() with:
// 1guardlet title = flyerTextEntry.text,
let body = bodyTextView.text,
let image = imagePreview.image,
let contact = contactTextView.text
else {
// 2let alert =UIAlertController(
title: "All Information Not Provided",
message: "You must supply all information to create a flyer.",
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
return
}
// 3let pdfCreator =PDFCreator(
title: title,
body: body,
image: image,
contact: contact
)
let pdfData = pdfCreator.createFlyer()
let vc =UIActivityViewController(
activityItems: [pdfData],
applicationActivities: []
)
present(vc, animated: true, completion: nil)
Here you:
First, ensure the user added all information for the flyer.
If not, you create an informational message, display it to the user and return.
If the user has added all the information for the flyer, you create the PDF. You then create a UIActivityViewController to provide and display the shared object's data.
Build and run the app, enter all information, then tap Share at the bottom-right side of the app. You'll see options to share the PDF file. Unfortunately, the simulator doesn't provide many useful options, so you may want to run the app on a real device.
One Final Touch
That check before sharing the PDF is a nice user interface feature. So now you'll add a check to make sure all information is there when the user requests a preview as well.
Open FlyerBuilderViewController.swift file and, above prepare(for:sender), add this new method to determine if the segue should occur:
overridefuncshouldPerformSegue(withIdentifieridentifier: String,
sender: Any?) -> Bool {
iflet_= flyerTextEntry.text,
let_= bodyTextView.text,
let_= imagePreview.image,
let_= contactTextView.text {
returntrue
}
let alert =UIAlertController(
title: "All Information Not Provided",
message: "You must supply all information to create a flyer.",
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
returnfalse
}
This code checks that the user has entered something in each text field and has selected an image. If so, it allows the segue. If not, it displays an error message and stops the segue.
Where to Go From Here?
You can download the final project using the Download Materials button at the top or bottom of this page.
In this tutorial, you've built an app that creates a PDF file using PDFKit. The user's input produces a flyer that you can share with other iOS apps. In the process, you learned to create a PDF, work with Core Text and Core Graphics and share the created document.
We hope you found this tutorial helpful. If you have any questions or comments, please join the forum discussion below.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.