【问题标题】:iOS - "Open in...", problem with accessing fileiOS - “打开方式...”,访问文件有问题
【发布时间】:2020-08-20 01:17:51
【问题描述】:

我正在开发 iOS 应用程序(使用 Xamarin)。我尝试用我的应用程序处理 pdf 文件并访问他们的内容。我将我的应用程序添加到 pdf 文件的上下文菜单(“打开方式...”),就像这里 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>

当我点击“在我的应用程序中打开”时,我的应用程序打开并尝试访问文件的字节数组内容。

在 SceneDelegate 中,我添加了

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

路径看起来像: file:///private/var/mobile/Containers/Shared/AppGroup/XXX/File%20Provider%20Storage/sample.pdf

不幸的是,读取会引发异常: System.UnauthorizedAccessException: '访问路径“/private/var/mobile/Containers/Shared/AppGroup/XXX/File Provider Storage/sample.pdf”被拒绝。'

如何访问以这种方式打开的文件的字节内容?

【问题讨论】:

  • 您是否使用应用组或共享扩展在应用之间共享文件?

标签: ios iphone xamarin xamarin.ios


【解决方案1】:

在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>

如果还是不行,你可以分享你的样品,让我测试一下。

【讨论】:

    猜你喜欢
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多