Understanding UIDocument in iOS 5: Clarifying Questions and Answers

Understanding UIDocument in iOS 5: Clarifying Questions and Answers

Introduction

The UIDocument class is a powerful tool for interacting with documents on an iPhone or iPad. In iOS 5, the UIDocument class provides a convenient way to save and retrieve files from the device’s file system. However, there are some important questions that developers need to understand when working with UIDocument. In this article, we will delve into three related questions: where does the typeName come from in the contentsForType:error: method, can you have multiple document types for an iOS app, and how does UIDocument work on the simulator.

Question 1: Where Does the typeName Come From?

The typeName parameter in the contentsForType:error: method is used to identify a specific type of document. When using a custom document type, you need to override the - (NSString *)fileType method to return your custom file type.

Implementing Custom Document Type

To implement a custom document type, you need to create a subclass of UIDocument and override the - (NSString *)fileType method. This method returns the file type that should be used when saving documents of this type.

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface CustomDocument : UIDocument

- (NSString *)fileType;

@end

@implementation CustomDocument

- (NSString *)fileType {
    return @"CustomDocumentType";
}

@end

In the above example, we have created a custom document type called “CustomDocumentType.” When using this custom document type in UIDocument, it will be used instead of the default file type.

Using the Custom Document Type

To use the custom document type, you need to specify the file type in the contentsForType:error: method. This method is responsible for loading and saving documents based on their types.

- (id)contentsForType:(NSString *)typeName error:(NSError **)outError {
    if ([typeName isEqualToString:@"CustomDocumentType"]) {
        // Load custom document data here
    } else {
        // Handle other document types
    }
    return nil;
}

In the above example, we have specified that the “CustomDocumentType” should be used to load and save documents. When using this custom document type, you can implement your own logic for loading and saving documents.

Question 2: Can You Have Multiple Document Types?

Yes, you can have multiple document types for an iOS app. To achieve this, you need to return different file types in the - (NSString *)fileType method.

Example with Two Document Types

@interface MyDocument : UIDocument {
    NSString *_documentType;
}

- (NSString *)fileType;

@end

@implementation MyDocument

- (NSString *)fileType {
    if (_documentType == @"DocType1") {
        return @"DocType1";
    } else {
        return @"DocType2";
    }
    _documentType = @"DocType1";
    return @"DocType1";
}

@end

In the above example, we have created a document class called MyDocument with two custom document types “DocType1” and “DocType2.” The - (NSString *)fileType method returns different file types based on the current state.

Question 3: Does UIDocument Work in the Simulator?

Yes, UIDocument works in the simulator. However, there is a catch - you need to make sure that your app has permission to write files to the device’s file system.

Ensuring Permission

To ensure that your app has permission to write files, you need to add the NSDocumentTypesPropertyListKey key to your app’s info.plist file.

<key>NSDocumentTypesPropertyList</key>
<array>
    <dict>
        <key>LSHandlerForPreservedStateFileTypes</key>
        <string>com.example.MyApp.PreservedState</string>
        <key>UTI</key>
        <string>com.example.MyApp.Document</string>
    </dict>
    <dict>
        <key>LSHandlerForPreservedStateFileTypes</key>
        <string>com.example.MyApp.PreservedState2</string>
        <key>UTI</key>
        <string>com.example.MyApp.Document2</string>
    </dict>
</array>

In the above example, we have added two custom document types to our app’s info.plist file. The NSDocumentTypesPropertyListKey key specifies that these document types should be used when saving and loading files.

Saving Documents in the Simulator

To save documents in the simulator, you need to use the UIDocumentPickerViewController class.

- (void)saveFileUsingDocumentPicker {
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithTypes:@[@"com.example.MyApp.Document"] modalPresentationStyle:UIModalPresentationPopover];
    [self presentViewController=documentPicker animated:YES completion:nil];
}

In the above example, we have created a document picker view controller that allows the user to select files from their device’s file system. When the user selects a file, you can load its contents using the contentsForType:error: method.

Conclusion

UIDocument is a powerful tool for interacting with documents on an iPhone or iPad. By understanding how to implement custom document types and work with multiple document types, you can create apps that provide users with a more intuitive experience when working with files. Additionally, by using the UIDocumentPickerViewController class, you can save documents in the simulator without needing to manually specify file paths.

Further Reading

For more information on how to use UIDocument, we recommend checking out Apple’s official documentation: [UIDocument](https://developer.apple.com/library/ios/documentation/UIKit Reference/ReferenceManual/FoundationDataTypes/Classes/UIDocument.html).

Additionally, you can find tutorials and examples of using UIDocument in the following resources:


Last modified on 2023-07-21