How do I associate file types with an iPhone application?

后端 未结 4 1834

On the subject of associating your iPhone app with file types.

In this informative question I learned that apps could be associated with custom URL protocols.

4条回答
  •  星月不相逢
    2020-11-21 08:21

    BIG WARNING: Make ONE HUNDRED PERCENT sure that your extension is not already tied to some mime type.

    We used the extension '.icz' for our custom files for, basically, ever, and Safari just never would let you open them saying "Safari cannot open this file." no matter what we did or tried with the UT stuff above.

    Eventually I realized that there are some UT* C functions you can use to explore various things, and while .icz gives the right answer (our app):

    In app did load at top, just do this...

    NSString * UTI = (NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, 
                                                                       (CFStringRef)@"icz", 
                                                                       NULL);
    CFURLRef ur =UTTypeCopyDeclaringBundleURL(UTI);
    

    and put break after that line and see what UTI and ur are -- in our case, it was our identifier as we wanted), and the bundle url (ur) was pointing to our app's folder.

    But the MIME type that Dropbox gives us back for our link, which you can check by doing e.g.

    $ curl -D headers THEURLGOESHERE > /dev/null
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 27393  100 27393    0     0  24983      0  0:00:01  0:00:01 --:--:-- 28926
    $ cat headers
    HTTP/1.1 200 OK
    accept-ranges: bytes
    cache-control: max-age=0
    content-disposition: attachment; filename="123.icz"
    Content-Type: text/calendar
    Date: Fri, 24 May 2013 17:41:28 GMT
    etag: 872926d
    pragma: public
    Server: nginx
    x-dropbox-request-id: 13bd327248d90fde
    X-RequestId: bf9adc56934eff0bfb68a01d526eba1f
    x-server-response-time: 379
    Content-Length: 27393
    Connection: keep-alive
    

    The Content-Type is what we want. Dropbox claims this is a text/calendar entry. Great. But in my case, I've ALREADY TRIED PUTTING text/calendar into my app's mime types, and it still doesn't work. Instead, when I try to get the UTI and bundle url for the text/calendar mimetype,

    NSString * UTI = (NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType,
                                                                       (CFStringRef)@"text/calendar", 
                                                                       NULL);
    
    CFURLRef ur =UTTypeCopyDeclaringBundleURL(UTI);
    

    I see "com.apple.ical.ics" as the UTI and ".../MobileCoreTypes.bundle/" as the bundle URL. Not our app, but Apple. So I try putting com.apple.ical.ics into the LSItemContentTypes alongside my own, and into UTConformsTo in the export, but no go.

    So basically, if Apple thinks they want to at some point handle some form of file type (that could be created 10 years after your app is live, mind you), you will have to change extension cause they'll simply not let you handle the file type.

提交回复
热议问题