【问题标题】:How do I successfully use Xamarin Forms Share Extension to share NSUserDefaults value?如何成功使用 Xamarin Forms Share Extension 共享 NSUserDefaults 值?
【发布时间】:2022-09-29 01:18:16
【问题描述】:

我正在尝试实施Xamarin 表单共享扩展允许我的应用程序从另一个应用程序接收 zip 文件。

我已成功设置应用组在 Apple Developer Account 中,我成功地能够看到文件的共享 url,但现在尝试通过以下方式将文件同步回 Container 应用程序时遇到问题NSUserDefaults.

这是我的带有日志语句的代码,以解决无法在 Xamarin Forms 中调试扩展应用程序的事实(据我所知):

internal class CodeBasedViewController : SLComposeServiceViewController
{
    public override async void DidSelectPost()
    {
        int count = 0;
        try
        {
            NSExtensionItem item = ExtensionContext.InputItems[0];

            foreach(NSItemProvider prov in item.Attachments)
            {
                Log(count++.ToString());
                if (prov.HasItemConformingTo(UTType.URL) == true)
                {
                    Log(\"url type ok\");

                    //Load item with Load Completion action
                    prov.LoadItem(UTType.URL, null, async (dataRaw, error) =>
                    {
                        Log(\"load url item ok\");

                        //dataRaw is \"file:///Users/appname/Library/Developer/CoreSimulator/Devices/..GUID1../data/Containers/Shared/AppGroup/...GUID2.../
                        string uri = dataRaw.ToString();
                        Log2(uri);

                        NSUserDefaults shared = new NSUserDefaults(
                                    \"group.com.companyname.appname.ShareExtension\",
                                     NSUserDefaultsType.SuiteName);

                        shared.SetString(uri, \"zipfile\");
                        if (shared.Synchronize())
                        {
                            //sync completes successfully and code comes here
                            Log(\"sync success\");

                        }
                        else
                            Log(\"sync failed\");
                            
                        
                        
                        //base.DidSelectPost();

                    });

                }
            }
        }
        catch (Exception e)
        {
            Log(\"ex: \" + e.Message);
        }

    }
}

我有几个问题或问题:

  1. LoadItem 完成操作大约需要 7 秒才能完成,这对于 6kb 文件来说似乎很长。
  2. 如果我取消注释 base.DidSelectPost(),扩展程序几乎会在单击后立即关闭邮政,让我怀疑我的完成操作代码被绕过了。
  3. 当我在 Container 应用程序激活时查看 NSUserDefaults 时,uri 值没有出现。 (我猜如果我可以在我的容器中获取这个 uri 值,我最终将可以访问 zip 文件。)
    public override void OnActivated(UIApplication uiApplication)
    {
        base.OnActivated(uiApplication);
    
        NSUserDefaults shared = new NSUserDefaults(
                    \"group.com.companyname.appname.ShareExtension\",
                     NSUserDefaultsType.SuiteName);
    
        //ok = true
        bool ok = shared.Synchronize();
    
        //this is null!!
        string uri = shared.StringForKey(\"zipfile\");
    
    }
    

    关于这里出了什么问题的任何想法?

    更新

    我已经进入我的主机 VM 并创建了两个 XCode 应用程序,它们镜像我的容器的包名称并在我的 Windows VS 机器上共享扩展应用程序。 XCode 应用程序还配置了 AppGroups。

    这仍然不起作用。我不确定我还能尝试什么。有任何想法吗?

    更新 02

    我发布我的信息列表文件在这里提供更多信息。

    谁能看到我在这里做错了什么:

    容器应用

    <?xml version=\"1.0\" encoding=\"UTF-8\"?>
    <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
    <plist version=\"1.0\">
    <dict>
        <key>UIDeviceFamily</key>
        <array>
            <integer>1</integer>
            <integer>2</integer>
        </array>
        <key>UISupportedInterfaceOrientations</key>
        <array>
            <string>UIInterfaceOrientationPortrait</string>
            <string>UIInterfaceOrientationLandscapeLeft</string>
            <string>UIInterfaceOrientationLandscapeRight</string>
        </array>
        <key>UISupportedInterfaceOrientations~ipad</key>
        <array>
            <string>UIInterfaceOrientationPortrait</string>
            <string>UIInterfaceOrientationPortraitUpsideDown</string>
            <string>UIInterfaceOrientationLandscapeLeft</string>
            <string>UIInterfaceOrientationLandscapeRight</string>
        </array>
        <key>MinimumOSVersion</key>
        <string>8.0</string>
        <key>CFBundleDisplayName</key>
        <string>MyApp</string>
        <key>CFBundleIdentifier</key>
        <string>com.companyname.MyApp</string>
        <key>CFBundleVersion</key>
        <string>1.0</string>
        <key>CFBundleName</key>
        <string>MyApp</string>
        <key>XSAppIconAssets</key>
        <string>Assets.xcassets/AppIcon.appiconset</string>
        <key>UILaunchStoryboardName</key>
        <string>LaunchScreen</string>
        <key>UIViewControllerBasedStatusBarAppearance</key>
        <false/>
        <key>NSExtensionPointIdentifier</key>
        <string>com.apple.share-services</string>
    </dict>
    </plist>
    

    共享应用

    <?xml version=\"1.0\" encoding=\"UTF-8\"?>
    <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
    <plist version=\"1.0\">
    <dict>
        <key>CFBundleDevelopmentRegion</key>
        <string>en</string>
        <key>CFBundleDisplayName</key>
        <string>MyApp ShareExtension</string>
        <key>CFBundleExecutable</key>
        <string>MyApp.iOS.ShareExtension</string>
        <key>CFBundleIdentifier</key>
        <string>com.companyname.MyApp.ShareExtension</string>
        <key>CFBundleInfoDictionaryVersion</key>
        <string>6.0</string>
        <key>CFBundleName</key>
        <string>com.your-company.MyApp.iOS.ShareExtension</string>
        <key>CFBundlePackageType</key>
        <string>XPC!</string>
        <key>CFBundleShortVersionString</key>
        <string>1.0</string>
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>CFBundleVersion</key>
        <string>1</string>
        <key>MinimumOSVersion</key>
        <string>8.0</string>
        <key>NSExtension</key>
        <dict>
            <key>NSExtensionAttributes</key>
            <dict>
                <key>NSExtensionActivationRule</key>
                <dict>
                    <key>NSExtensionActivationSupportsFileWithMaxCount</key>
                    <integer>1</integer>
                    <key>NSExtensionActivationSupportsImageWithMaxCount</key>
                    <integer>1</integer>
                    <key>NSExtensionActivationSupportsMovieWithMaxCount</key>
                    <integer>0</integer>
                    <key>NSExtensionActivationSupportsText</key>
                    <true/>
                    <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
                    <integer>1</integer>
                </dict>
            </dict>
            <key>NSExtensionPrincipalClass</key>
            <string>CodeBasedViewController</string>
            <key>NSExtensionPointIdentifier</key>
            <string>com.apple.share-services</string>
        </dict>
    </dict>
    </plist>
    
    
  • 您是否检查过\"group.com.companyname.appname.ShareExtension\" 是否与xcode 中的&lt;key&gt;NSExtensionPointIdentifier&lt;/key&gt; 相同?
  • 谢谢丽云的回复。我目前在远程 macOS VM 上使用 XCode 在 Windows 上运行。我的意思是我也需要在 XCode 上创建扩展吗?
  • 对不起我的错误,我的意思是你显示检查 \"group.com.companyname.appname.ShareExtension\" 与你的表单和 ios 部分的 info.plist 中的 &lt;key&gt;NSExtensionPointIdentifier&lt;/key&gt; 相同。
  • 你说对了一部分。我的容器应用程序中没有设置NSExtensionPointIdentifier信息列表.我用com.apple.share-services 的值把它放在那里(不是在权利.plist)。不幸的是,这仍然无助于传递NSUserDefaults 值。

标签: ios xamarin.forms nsuserdefaults ios8-share-extension


【解决方案1】:

我想出了答案。

除了上述设置我还需要添加权利.plist到我的项目。我认为这是默认完成的,只是作为项目的一部分,但我错了。

您还需要:

  1. 转到项目特性->iOS 捆绑签名->自定义权利
  2. 浏览并选择权利.plist文件

    NSUserDefaults现在应该在容器和扩展应用程序中都可见!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 2015-08-17
    相关资源
    最近更新 更多