1

In my app, I designed a new encrypted type data as attachment to the mail. When I receive the same type of attachment(filename.filetype) from another user, I want the attachment from mail to open in my app. I went through the action extensions tutorials. But, what is missing is, how can I open that particular type of attachment using my swift app. I get the answers in Obj-C as well as the previous versions of iOS. I am seeking an answer in iOS8, Swift to handle the file.

Here is my info.plist

<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.data</string>
        </array>
        <key>UTTypeDescription</key>
        <string>pvt file</string>
        <key>UTTypeIdentifier</key>
        <string>com.pryvateBeta.pvt</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <string>pvt</string>
        </dict>
    </dict>
</array>
<key>CFBundleDocumentsType</key>
<array>
    <dict>
        <key>CFBundleTypeIconFiles</key>
        <array/>
        <key>CFBundleTypeName</key>
        <string>pvt file</string>
        <key>CFBundleTypeRole</key>
        <string>Viewer</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.pryvateBeta.pvt</string>
        </array>
    </dict>
</array>

Here is my AppDelegate

func application(application: UIApplication, openURL Url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
    let encrypteddata = NSData(contentsOfURL: Url)

    return true}
Mohammed Janish
  • 207
  • 4
  • 17

1 Answers1

4

First of all you need to declare the file type your app will handle in your apps Info.plist.

For instance the configuration shown below declares that the app is able to open .lumenconfig files which are basically XML. See this for more info about the declarations.

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <!-- The name of this file type. -->
        <string>Lumen Configuration</string>
        <key>CFBundleTypeIconFiles</key>
        <!-- The name of this file type. -->
        <array/>
        <key>LSItemContentTypes</key>
        <!-- The different type identifiers that are handled 
             as this type of file. -->
        <array>
            <!-- This is a custom type I declare below -->
            <string>at.zujab.lumen.lumenconfig</string>
        </array>
    </dict>
</array>

If you use a custom type like I do in the above example you also need to declare that type. See this for more information about declaring your own type

<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <!-- How these files are structured -->
        <array>
            <string>public.xml</string>
        </array>
        <key>UTTypeIdentifier</key>
        <!-- This is the identifier for the custom type -->
        <string>at.zujab.lumen.lumenconfig</string> 
        <key>UTTypeDescription</key>
        <!-- How your app calls these files. -->
        <string>Lumen Configuration File</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
             <!-- The extension of the files of this type -->
            <string>lumenconfig</string>
        </dict>
    </dict>
</array>

Then in your app delegate implement a handler to handle the file:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    //....

    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
        //url contains a URL to the file your app shall open

        //In my EXAMPLE I would want to read the file as a dictionary
        let dictionary = NSDictionary(contentsOfURL: url)

    }

}
idmean
  • 14,540
  • 9
  • 54
  • 83
  • Is this the info.plist of the action Extension? – Mohammed Janish Aug 10 '15 at 09:19
  • @MohammedJanish No. You do not need an extension to open files in your app. This is the Info.plist of the app itself. – idmean Aug 10 '15 at 09:21
  • public.xml UTTypeIdentifier at.zujab.lumen.lumenconfig UTTypeDescription Lumen Configuration File What does these lines stand for? – Mohammed Janish Aug 10 '15 at 09:52
  • What is the url to be given in the AppDelegate? Is it the url of the mail App? – Mohammed Janish Aug 10 '15 at 09:54
  • @MohammedJanish There I declare my own type. I’m using a custom type called `lumenconfig` which iOS does not know. The URL, of course, points to the file (when you tap the email attachment and choose open in). – idmean Aug 10 '15 at 10:09
  • How can I get that url? And how can we set the action for the file which is downloaded – Mohammed Janish Aug 10 '15 at 11:38
  • Are you unable to read Swift? `url` contains the URL. – idmean Aug 10 '15 at 11:51
  • Sorry, I didn't see that. There are no errors, but still i can't open the attachment. I referred some more tutorials. I think my info.plist is okay. Can't find the bug – Mohammed Janish Aug 10 '15 at 12:33
  • @MohammedJanish Without seeing your plist I also won’t – the chance are higher if you add your plist into your question by [editing](http://stackoverflow.com/posts/31915406/edit) it. – idmean Aug 10 '15 at 12:44
  • @MohammedJanish This picture is pretty useless, please select the first row, hit cmd+c and cmd+v it into your question **and remove all redundant information not related to your problem**. But what I can see is that you have no values for "Conforms to UTI". – idmean Aug 10 '15 at 13:01
  • What should I do to handle that file in view controller?? – Mohammed Janish Aug 11 '15 at 04:45
  • @MohammedJanish You’ll somehow need to push a view controller into your navigation controller. – idmean Aug 11 '15 at 08:21
  • I have already six view controllers in my project. i want to open the file in one of these. – Mohammed Janish Aug 12 '15 at 08:58
  • Even though I need some more steps to complete this, You did ur part and i'm ticking ur answer. Thank you. – Mohammed Janish Aug 13 '15 at 11:04
  • I am getting an NSData file from mail. Where should i mention this in plist. @idmean – Mohammed Janish Aug 18 '15 at 09:48
  • @MohammedJanish You do not have to declare the files structure if it does not conform to any known structure. So if it is a custom binary format you don't need to do anything. – idmean Aug 19 '15 at 11:12
  • i got the solution. Ur help is appreciated. – Mohammed Janish Aug 19 '15 at 11:24