Tải bản đầy đủ
Chapter 9. Working with Documents on iOS

Chapter 9. Working with Documents on iOS

Tải bản đầy đủ

7. Select the new view controller, open the Identity Inspector, and set its class to
DocumentViewController (see Figure 9-1). This connects the view controller in
the storyboard to the view controller class that we just created.

Figure 9-1. Setting the new view controller’s class to DocumentViewController
8. Hold down the Control key and drag from the document list view controller to
the new document view controller. A list of potential types of segues you can cre‐
ate will appear; click Show (see Figure 9-2).
A segue is a transition between one view (and view controller)
to another. Segues are used only with storyboards, not nibs.
Segues are triggered, typically, by user interaction, and end
when the new view controller is displayed. You construct the
segue in the interface builder, and then it’s either triggered
automatically or manually through code using the performSe
gueWithIdentifier method. We’ll be using this later in the

Figure 9-2. Creating the segue



Chapter 9: Working with Documents on iOS

9. Select the “Show segue to ‘Document View Controller’” item in the outline, and
go to the Attributes Inspector.
10. Set the Identifier of the segue to ShowDocument (Figure 9-3).

Figure 9-3. Configuring the segue
Next, we’ll set up the user interface for the document view controller.
1. Add a UITextView to the document view controller. We’ll use this to display the
text contents of a note document.
2. Resize it to fill the entire screen, and add the following constraints to it:
• Leading spacing to container’s leading margin = 0
• Trailing spacing to container’s trailing margin = 0
• Bottom spacing to bottom layout guide = 0
• Top spacing to top layout guide = 0
This will make the text view that we just added fill the majority of the screen.
3. Go to the Attributes Inspector, and change its mode from Plain to Attributed
(Figure 9-4). We’ll be displaying attributed text—text that has formatting
attributes—so we need to make sure that the text view we’re using knows how to
display that.

Adding a View to Display Notes



Figure 9-4. Setting the mode of the text view
4. Open DocumentViewController.swift.
5. Add the following code to implement the textView, document, and documentURL
@IBOutlet weak var textView : UITextView!
private var document : Document?
// The location of the document we're showing
var documentURL:NSURL? {
// When it's set, create a new document object for us to open
didSet {
if let url = documentURL {
self.document = Document(fileURL:url)

The textView property will be used to connect this code to the text view that
shows the document’s text, while the document property holds the Document
object that’s currently open. The documentURL property stores the location of the
document that the view controller is currently displaying; importantly, when it’s
set, it creates and prepares the document property with a Document object to use.
6. Open Main.storyboard and open DocumentViewController.swift in the Assistant.
Connect the text view to the textView outlet.
For a quick reminder on how to connect views to outlets, see
“Connecting the Code” on page 22.

7. Implement viewWillAppear to open the document and load information from it:
override func viewWillAppear(animated: Bool) {
// Ensure that we actually have a document
guard let document = self.document else {
NSLog("No document to display!")



Chapter 9: Working with Documents on iOS

// If this document is not already open, open it
if document.documentState.contains(UIDocumentState.Closed) {
document.openWithCompletionHandler { (success) -> Void in
if success == true {
self.textView?.attributedText = document.text
// We can't open it! Show an alert!
let alertTitle = "Error"
let alertMessage = "Failed to open document"
let alert = UIAlertController(title: alertTitle,
message: alertMessage,
preferredStyle: UIAlertControllerStyle.Alert)
// Add a button that returns to the previous screen
alert.addAction(UIAlertAction(title: "Close",
style: .Default, handler: { (action) -> Void in
// Show the alert
animated: true,
completion: nil)

The code for opening documents is verbose but pretty straightforward. We first
check to ensure that the view controller actually has a Document to open; if it
doesn’t, it tells the navigation controller to return to the document list.
Next, it asks if the document is currently closed. If it is, we can open it by calling

openWithCompletionHandler. This attempts to open the document and takes a

closure that gets informed whether it was successfully opened or not. If opening
succeeds, the Document’s properties now contain the data that we need, like its
text; as a result, we can grab the note’s text and display it in the textView.
If opening the document fails, we need to tell the user about it. To handle this, we
create and display a UIAlertController.

Adding a View to Display Notes



Next, we’ll make tapping on a file in the document list view controller open that
1. Open DocumentListViewController.swift.
2. Implement the didSelectItemAtIndexPath to trigger the segue to the document:
override func collectionView(collectionView: UICollectionView,
didSelectItemAtIndexPath indexPath: NSIndexPath) {
// Did we select a cell that has an item that is openable?
let selectedItem = availableFiles[indexPath.row]
if itemIsOpenable(selectedItem) {
sender: selectedItem)

The didSelectItemAtIndexPath method is called when the user taps on any
item in the collection view, and receives as parameters the collection view that
contained the item, plus an NSIndexPath that represents the position of the item
in question. NSIndexPath objects are really just containers for two numbers: the
section and the row. Collection views can be broken up into multiple sections,
and each section can contain multiple rows.
In order to access the correct document, we need to figure out the URL repre‐
sented the item the user just selected. Because we only have a single section in
this collection view, we can just use the row property to get the URL from the
availableFiles list. If that URL is openable (which we test by using the itemIsO
penable method), we ask the system to perform the ShowDocument segue, passing
in the URL.
We can also access the row property in NSIndexPath using the item
property. They both represent the same value.

When we ask the system to perform a segue, the view controller at the other end of
the segue will be created and displayed. Before it’s shown, however, we’re given a
chance to prepare it with the right information that it will need. In this case, the Docu
mentViewController at the other end of the segue will need to receive the correct
NSURL so that it can open the document.


| Chapter 9: Working with Documents on iOS

1. Open DocumentListViewController.swift.
2. Implement prepareForSegue to prepare the next view controller:
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?) {
// If the segue is "ShowDocument" and the destination view controller
// is a DocumentViewController...
if segue.identifier == "ShowDocument",
let documentVC = segue.destinationViewController
as? DocumentViewController
// If it's a URL we can open...
if let url = sender as? NSURL {
// Provide the url to the view controller
documentVC.documentURL = url
} else {
// It's something else, oh no!
fatalError("ShowDocument segue was called with an " +
"invalid sender of type \(sender.dynamicType)")

The prepareForSegue method is called whenever the view controller is about to
show another view controller, via a segue. It receives as its parameters the segue
itself, represented by a UIStoryboardSegue object, as well as whatever object was
responsible for triggering the segue. In the case of the ShowDocument segue, the
sender is an NSURL, because we passed that in as the sender parameter to the
performSegueWithIdentifier method in didSelectItemAtIndexPath.
To get the view controller that we’re about to transition to, we ask the segue for its
destinationViewController property and ask Swift to try to give it to us as a
DocumentViewController. Next, we double-check the type of the sender and
make sure that it’s an NSURL. Finally, we give the view controller the URL.
Now’s a great time to build and run the app. You should now be able to tap on docu‐
ment thumbnails and segue to the editing screen, and get a “back” button to return to
the document list, which is provided automatically by the navigation controller. Edits
can be made, though they can’t be saved yet. But, still! There’s some good progress
happening here.
Finally, we also want to open documents that we’ve just created. We’ll do this by creat‐
ing a method called openDocumentWithPath, which will receive a String that con‐

Adding a View to Display Notes



tains a path. It will prepare an NSURL, and then call performSegueWithIdentifier,
passing the URL as the sender.
We’ll be using this method from multiple different places later in
this book, so we’re putting it in a method.

1. Implement the openDocumentWithPath method, which takes a path and attempts
to open it:
func openDocumentWithPath(path : String) {
// Build a file URL from this path
let url = NSURL(fileURLWithPath: path)
// Open this document
self.performSegueWithIdentifier("ShowDocument", sender: url)

Next, when a document is created, we’ll want the app to immediately open it for
2. Add the calls to openDocumentWithPath to the createDocument method:
func createDocument() {
// Create a unique name for this new document
// by adding a random number
let documentName = "Document \(arc4random()).note"
// Work out where we're going to store it temporarily
let documentDestinationURL = DocumentListViewController
// Create the document and try to save it locally
let newDocument = Document(fileURL:documentDestinationURL)
forSaveOperation: .ForCreating) { (success) -> Void in
if (DocumentListViewController.iCloudAvailable) {



If we have the ability to use iCloud...
If we successfully created it, attempt to
move it to iCloud
success == true, let ubiquitousDestinationURL =

Chapter 9: Working with Documents on iOS

.URLByAppendingPathComponent(documentName) {
// Perform the move to iCloud in the background
NSOperationQueue().addOperationWithBlock { () -> Void in
do {
try NSFileManager.defaultManager()
itemAtURL: documentDestinationURL,
destinationURL: ubiquitousDestinationURL)
.addOperationWithBlock { () -> Void in

// Open the document
if let path = ubiquitousDestinationURL.path {
} catch let error as NSError {
NSLog("Error storing document in iCloud! " +
} else {
// We can't save it to iCloud, so it stays
// in local storage.


// Just open it locally
if let path = documentDestinationURL.path {

We’re now able to open documents, but not much else. Next, we’ll add the ability to
actually edit the document.

Adding a View to Display Notes



Editing and Saving Documents
The last critical feature of this app is to let the user make changes to the document.
When you’re using the UIDocument system, your documents are automatically saved
when the user leaves your application or when you close the document. You don’t
need to manually save changes—the system will automatically take care of it for you.
To signal to iOS that the user is done with the document, we’ll close the document
when the user leaves the DocumentViewController.
This means that, if the document was modified, the system will call contentsForType
and ask the Document class to provide an NSFileWrapper containing the document’s
contents, which will be saved to disk.
However, the system has to know that changes were applied in the first place, so, in
order to tell the document that changes were made, we need to use the updateChange
Count method when the user makes a change to the text field. To find out that bit of
information, we need to ask the text view to let the view controller know when a
change is made.
1. Open DocumentViewController.swift.
2. Make DocumentViewController conform to UITextViewDelegate by adding
UITextViewDelegate to the class’s definition:
class DocumentViewController: UIViewController, UITextViewDelegate {

When an object conforms to the UITextViewDelegate protocol, it’s able to act as
the delegate for a text view. This means that it can be notified about events that
happen to the text view, such as the user making changes to the content of the
text view.
3. Implement the textViewDidChange method to store text in the document, and
update the document’s change count:
func textViewDidChange(textView: UITextView) {
document?.text = textView.attributedText

Even though it’s called the change “count,” you don’t really work
with a number of changes. Rather, the change count is internal to
the document system; your app doesn’t need to know what the
change count is, you just need to update it when the user modifies
the content of the document.



Chapter 9: Working with Documents on iOS

With this method in place, the view controller is able to respond to a text view chang‐
ing its content. We use this opportunity to update the Document’s text property, and
then call updateChangeCount to signal to the document that the user has made a
change to its content. This indicates to the UIDocument system that the document has
changes that need to be written to disk; when the system decides that it’s a good time
or when the document is closed, the changes will be saved.
Now that the document’s contents are updated, we need to tell the document system
to close the document when we leave the view controller.
1. Implement viewWillDisappear to close the document:
override func viewWillDisappear(animated: Bool) {

2. Open Main.storyboard.
3. Hold down the Control key, and drag from the text view to the document view
controller (Figure 9-5). Select “delegate” from the menu that appears.
Remember to drag to the view controller itself, not the view
that the view controller is managing. If you drag to the little
yellow circle icon above the view controller’s interface, you’ll
always be connecting to the right thing.

Figure 9-5. A drag in progress
4. Launch the app, open a document, make changes, close it, and reopen it—the
changes are still there!

In this chapter, we’ve added the ability to open notes and view their contents, as well
as the ability to actually edit and save the changes to notes. We did this by creating




some new view controllers and their UI in storyboards and connecting them with
In the next chapter, we’ll add support for file attachments and update the interface to
show a list of attachments.



Chapter 9: Working with Documents on iOS