Wednesday, June 11, 2014

"Open In" for sharing in iOS Apps

Most of the iOS App developers may be wondering as why the most used and loved feature in Android "Open with" is not available in iOS. The reason for this being the way an application is structured in iOS. Files are encapsulated in an application environment called as Sand Box. So a file "F" downloaded using an app "A" belongs to "A" sandbox and hence is not visible to other apps on the iDevice. So lets consider a scenario where I have downloaded a RTF text file and I wanted to open it Evernote or any other app in my iDevice as developing my own file reader might not be an option always. How to accomplish this ? Have you noticed that DropBox does provide an option "Open In" to open any file in dropbox with other apps installed on the iDevice to do the same ?
UIDocumetInteractionController is the solution to this. I was developing an application where in I ran into similar situation and initially was deceived by UIActivityController to do the same. After pulling my hairs for couple of hours I did notice that  UIDocumetInteractionController is the solution to this and not UIActivityController. So below is the code as how to do this using a UIDocumetInteractionController.

// Declare a document interaction controller property

@property (retainUIDocumentInteractionController *docController;

NSURL *URL = [NSURL fileURLWithPath:filePath];
if (URL) {

        // Initialize Document Interaction Controller
        self.docController = [UIDocumentInteractionController interactionControllerWithURL:URL];

        // Configure Document Interaction Controller
        [self.docController setDelegate:self];

        // Present Open In Menu
inView:self.view animated:YES];

When this piece of code is executed the resulting output would be something as below based on the apps that are compatible to open the file installed on your iDevice. 

No comments:

Post a Comment