开发者

Own data format for the iPhone

开发者 https://www.devze.com 2022-12-23 11:11 出处:网络
I would like to create my own data format for an iPhone app. The files should be similar structured as e.g. Apple\'s iWork files (.pages). That means, I have a folder with some files in it:

I would like to create my own data format for an iPhone app. The files should be similar structured as e.g. Apple's iWork files (.pages). That means, I have a folder with some files in it:

The file 'Juicy.fruit' contains:
   Fruits
   ---> Apple.xml
   ---> Banana.xml
   ---> Pear.xml
   ---> PreviewPicture.png

This folder "Fruits" should be packed in a handy file 'Juicy.fruit'. Compression isn't necessary. How could I achieve this? I've discovered some open source ZIP-libraries. However, I would like to to build my own data format with the i开发者_JAVA技巧Phones built-in libs (if possible).

Best regards, Stefan


Okay, so there are three ways I am reading your question, here's my best guess on each one:

  1. You want your .fruit files to be associated with your app via Safari/SMS/some network connection (aka when someone wants to download files made for your app or made by your app).

In this case, you can register a protocol for your app, as discussed here:

iPhone file extension app association

  1. You want the iPhone to globally associate .fruit files with your app, in which case you want to look into Uniform Type Identifiers. Basically, you set up this association in your installer's info.plst file.

  2. You want to know how you can go from having a folder with files in it to that folder being a single file (package) with your .fruit extension.

If that's the case, there are many options out there and I don't see a purpose in rolling your own. Both Microsoft and Adobe simply use a standard zip compression method and use their own extension (instead of .zip). If you drop any office 2007 document, such as docx or Adobe's experimental .pdfxml file into an archive utility (I like 7z, but any descent one will do), you will get a folder with several xml files, just like you're describing for your situation. (This is also how Java's jar file type works, fyi). So unless you have a great reason to avoid standard compression methods (I vote gzip), I would follow the industry lead on this one.

I can definitly appreciate the urge to go DIY at every level possible, but you're basically asking (if it's #3) how you can create your own packaging algorithm, and after reading how some of the most basic compression methods work, I would leave that one alone. Plus I really doubt that Apple has built in libraries for doing something that most people will just use standard methods for.

One last note:

If you are really gunning to do it from scratch (still suggest not), since your files are all XML, you could just create a new XML file that will act as a wrapper of sorts, and have each file go into that wrapper file. But this would be really redundant when it came time to unwrap, as it would have to load the whole file every time. But it would be something like:

Juicy.fruit --
   <fruit-wrapper>
       <fruit>
          <apple>
              ... content from apple.xml
          </apple>
       </fruit>
       <fruit>
          <banana>
              ... content from banana.xml
          </banana>
       </fruit>
       <fruit>
          <pear>
              ... content from pear.xml
          </pear>
       </fruit>
       <picture>
              ...URL-encoded binary of preview picture
       </picture>
   </fruit-wrapper>     

But with this idea, you either have to choose to unpack it, and thus risk losing track of the files, overwriting some but not all, etc etc, or you always treat it like one big file, in which case, unlike with archives, you have to load all of the data each time to pull anything out, instead of just pulling the file you want from the archive.

But it could work, if you're determined.

Also, if you are interested, there is a transfer protocol intended specifically for XML over mobile called WBXML (Wap Binary XML). Not sure if it is still taken seriously, but if there is an iPhone library for it, you should research it.

0

精彩评论

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

关注公众号