0
votes

I am developing iOS app (with Xamarin). I try to handle pdf files with my app and access theirs content. I added my app to context menu of pdf files ("Open in..."), like here https://i.stack.imgur.com/yowSw.png

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <string>PDF</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.adobe.pdf</string>
        </array>
    </dict>
</array>

When I click "Open in MY APP", my app opens and I try to access byte array content of file.

In SceneDelegate, I added

    [Export("scene:openURLContexts:")]
    public void OpenUrlContexts(UIScene scene, NSSet<UIOpenUrlContext> urlContexts)
    {
        var path = urlContexts.AnyObject.Path;
        var fileBytes = File.ReadAllBytes(path);
    }

Path looks like: file:///private/var/mobile/Containers/Shared/AppGroup/XXX/File%20Provider%20Storage/sample.pdf

Unfortunately, reading throws an exception: System.UnauthorizedAccessException: 'Access to the path "/private/var/mobile/Containers/Shared/AppGroup/XXX/File Provider Storage/sample.pdf" is denied.'

How can I access byte content of file opened in that way?

1
Did you share the file between apps by using App Group or Share Extension? - Lucas Zhang

1 Answers

0
votes

Add the following code in Info.plist

<key>UTExportedTypeDeclarations</key>
    <array>
        <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.data</string>
                <string>public.composite-content</string>
            </array>
            <key>UTTypeIdentifier</key>
            <string>com.adobe.pdf</string>
            <key>UTTypeDescription</key>
            <string>PDF</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.mime-type</key>
                <string>application/pdf</string>
                <key>public.filename-extension</key>
                <array>
                    <string>pdf</string>
                </array>
            </dict>
        </dict>

        <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.data</string>
            </array>
            <key>UTTypeIdentifier</key>
            <string>com.microsoft.excel.xls</string>
            <key>UTTypeDescription</key>
            <string>Excel Document</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.mime-type</key>
                <string>application/vnd.ms-excel</string>
                <key>public.filename-extension</key>
                <array>
                    <string>xls</string>
                </array>
            </dict>
        </dict>
    </array>

If it still doesn't work , you could share your sample so that I can test it on my side .