开发者

Associating sqlite3 db to an iPhone app

开发者 https://www.devze.com 2023-01-04 17:39 出处:网络
I\'m trying to associate an SQLite3 database file with our app so that it\'s easy to open backed up database from an email. The following however does not seem to work as Mail still doesn\'t recognize

I'm trying to associate an SQLite3 database file with our app so that it's easy to open backed up database from an email. The following however does not seem to work as Mail still doesn't recognizes the file (on an iPad and iPhone 4):

<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeConformsTo</key>
        <array>
            <string>public.database</string>
            <string>public.data</string>                    
        </array>
        <key>UTTypeDescription</key>
        <string>App Database File</string>
        <key>UTTypeIdentifier</key>
        <string>com.company.App.db</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <string>db</string>
            <key>public.mi开发者_Go百科me-type</key>
            <string>application/x-sqlite3</string>            
        </dict>
    </dict>
</array>

 <key>CFBundleDocumentTypes</key>
 <dict>
  <key>CFBundleTypeName</key>
  <string>App Database</string>
  <key>CFBundleTypeIconFiles</key>
  <array>
   <string>Icon-Small.png</string>
   <string>Icon.png</string>
  </array>
    <key>CFBundleTypeRole</key>
        <string>Editor</string>

  <key>LSItemContentTypes</key>
  <array>
   <string>com.company.App.db</string>
  </array>
  <key>LSHandlerRank</key>
  <string>Alternate</string>
 </dict>
</dict>

Any idea what I'm doing wrong?


For completeness and possibly my own reference later here is a bit of the further details that got it working for me:

Declaring Document Types your app supports (eg sqlite3 databases)

<key>UTExportedTypeDeclarations</key>
    <array>
        <dict>
            <key>UTTypeIdentifier</key>
            <string>com.company.sqlite3.database</string>
            <key>UTTypeReferenceURL</key>
            <string>http://www.company.com/</string>
            <key>UTTypeDescription</key>
            <string>MyCompany SQLite Database</string>
            <key>UTTypeIconFile</key>
            <array>
                <string>Icon-Small.png</string>
                <string>Icon.png</string>
            </array>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.database</string>
                <string>public.data</string>
            </array>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.filename-extension</key>
                <array>
                    <string>sqlite</string>
                </array>
                <key>public.mime-type</key>
                <array>
                    <string>application/x-sqlite3</string>
                    <string>application/octet-stream</string>
                </array>

            </dict>
        </dict>
    </array>
<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <string>MyCompany SQLite Database</string>
        <key>CFBundleTypeIconFiles</key>
        <array>
            <string>Icon-Small.png</string>
            <string>Icon.png</string>
        </array>

        <key>CFBundleTypeExtensions</key>
        <array>
            <string>sqlite</string>
        </array>

        <key>CFBundleTypeMIMETypes</key>
        <array>
            <string>application/x-sqlite3</string>
            <string>application/octet-stream</string>
        </array>

        <key>LSHandlerRank</key>
        <string>Alternate</string>

        <key>LSItemContentTypes</key>
        <array>
            <string>com.company.sqlite3.database</string>
        </array>

        <key>NSPersistentStoreTypeKey</key>
        <string>SQLite</string>

    </dict>
</array>

Copy paste the above XML into your Info.plist file.

Setting the 'Store Type' to 'SQLite' didn't wasn't the killer fix for me.

My previous post here mentioned a rather incorrect way of getting it to work which accepts all files and didn't properly export the type.

Also if your app is emailing off these files as attachments make sure it matches the MIME type you set it to capture. The application/octet-stream is not important it is just that older versions of our app are emailing out the databases with that MIME type.

eg,

[controller addAttachmentData:[NSData dataWithContentsOfFile:dbPath] mimeType:@"application/x-sqlite3" fileName:filename];

I sure hope anyone else that tries to find out how to get their app to support opening sqlite3 database backups finds this helpful.


Okay I've sorted this out. If you instead use 'target's info' panel and add your document type there (and then select 'SQLite' as the type') it just works. Obviously provided you've exported the type as above.


As per Apple's documentation, CFBundleDocumentTypes is an array, not a dict.

You have:

<key>CFBundleDocumentTypes</key>
<dict>

so the <dict> needs to be in an <array> element, like so:

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <string>App Database</string>
        <!-- ... -->
    </dict>
</array>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号