2015-08-10 1 views
0

Dans mon application, j'ai créé une nouvelle donnée de type cryptée en pièce jointe au courrier. Lorsque je reçois le même type de pièce jointe (filename.filetype) d'un autre utilisateur, je souhaite que la pièce jointe du courrier s'ouvre dans mon application. Je suis allé à travers les tutoriels d'extensions d'action. Mais, ce qui manque est, comment puis-je ouvrir ce type particulier de pièce jointe en utilisant mon application rapide. J'obtiens les réponses dans Obj-C ainsi que dans les versions précédentes d'iOS. Je cherche une réponse dans iOS8, Swift pour gérer le fichier.Ouvrir une pièce jointe à partir d'un e-mail à l'aide de l'application ios 8 [swift]

Voici mon 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> 

Voici mon AppDelegate

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

    return true} 
+0

Je ne pouvais pas résoudre ce Obj-C. Je cherche une référence ou une réponse dans Swift. –

Répondre

4

Tout d'abord vous devez déclarer le type de fichier votre application se chargera dans vos applications Info.plist.

Par exemple, la configuration ci-dessous déclare que l'application est capable d'ouvrir .lumenconfig fichiers qui sont essentiellement 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> 

Si vous utilisez un type personnalisé comme je le fais dans l'exemple ci-dessus vous devez également déclarer ce 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> 

ensuite dans votre délégué application implémenter un gestionnaire pour gérer le fichier:

@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) 

    } 

} 
+0

Est-ce l'info.plist de l'action Extension? –

+0

@MohammedJanish Non. Vous n'avez pas besoin d'une extension pour ouvrir les fichiers dans votre application. C'est l'Info.plist de l'application elle-même. – idmean

+0

public.xml UTTypeIdentifier at.zujab.lumen.lumenconfig UTTypeDescription Fichier de configuration Lumen Que signifient ces lignes? –