0

我想让一个应用程序(非 Unity 项目)在后台运行并将一些数据写入 XML 文件。这是我已经拥有的代码。但是,对于我的 Unity 应用程序,我需要知道文件的位置,以便我可以阅读它。

有谁知道 iOS 自动将它创建的 XML 文件保存在哪里?

4

1 回答 1

1

这个问题有点老了,但我会在这里回答,以防有人需要指南。

现在在 iOS8.0+ 中使用新的App Group功能很可能做到这一点。此功能允许 2 个或更多应用程序在一个共享目录中会面。这是设置它的非常简短的步骤:

  1. 进入您的 Apple Developer 代理帐户,在您管理应用 ID 和证书的同一页面中,创建新的应用组。假设group.com.company.myapp
  2. 我们需要 2 个应用程序来共享数据,对吗?确保您准备好 2 个应用程序 ID,如果没有,请创建新的。在这里您可以启用 App Group 还可以创建 2 个应用 ID,比如说com.company.myapp.readercom.company.myapp.writer。(可以是任何名称,您实际上不必像这样嵌套名称)您可以暂时取消选中应用程序组的应用程序权利,因为 XCode 将为我们完成所有配置。
  3. 在 XCode 上创建新项目,每个项目一个,Single view app 是一个很好的项目模板。
  4. 在项目设置/功能选项卡上打开“应用程序组”功能。不要忘记选择要使用的应用程序组。
  5. 在这里,您必须编写一些 Objective-C 代码来获取该共享文件夹路径。(请参阅NSFileManager containerURLForSecurityApplicationGroupIdentifier)UI 的 viewDidLoad 函数是尝试您的代码片段的好地方。
    • Writer 应用程序可能会将此路径与文件名连接并尝试将一些文件写入该目录,您可以使用任何具有此路径的文件 API。
    • 阅读器应用程序执行相同的操作,但从中读取。
  6. 测试直到您可以让阅读器应用程序看到编写器应用程序的文件。(我将把所有工作留在这里作为练习,正如我所说的,这将是简短的)

具体到OP的场景:

  1. 好的,你想要 Unity3D 中的这个功能。看起来没有内置的统一 API 或任何资产商店,所以你必须编写自己的 iOS 原生插件。如果您不熟悉它,请阅读一些文档。统一端的 C# 存根可以是:

    using UnityEngine;
    using System.Collections;
    using System.Runtime.InteropServices;
    
    public static class AppGroupPlugin
    {
    
        /* Interface to native implementation */
    
        #region Native Link
        #if UNITY_EDITOR
    
        private static string _GetSharedFolderPath( string groupIdentifier )
        {
            // Handle dummy folder in editor mode
            string path = System.IO.Path.Combine( System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop), groupIdentifier );
            if( !System.IO.Directory.Exists( path ) )
                System.IO.Directory.CreateDirectory( path );
    
            return path;
        }
    
        #elif UNITY_IOS
    
        [DllImport ("__Internal")]
        private static extern string _GetSharedFolderPath( string groupIdentifier );
    
        #endif
        #endregion
    
        public static string GetSharedFolderPath( string groupIdentifier )
        {
            return _GetSharedFolderPath( groupIdentifier );
        }
    }
    
  2. 只需让 Objective-C 向您返回共享路径即可。我测试过,你得到这个路径后,你可以在任何System.IO.File操作上使用这个路径来读写文件,就好像它们是普通文件夹一样。这可以只是位于 Plugins/iOS 文件夹中的一个 .m 文件。

    char* MakeNSStringCopy (NSString* ns)
    {
        if (ns == nil)
            return NULL;
    
        const char* string = [ns UTF8String];
    
        char* res = (char*)malloc(strlen(string) + 1);
        strcpy(res, string);
        return res;
    }
    
    NSString* _GetSharedFolderPathInternal( NSString* groupIdentifier )
    {
        NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:groupIdentifier];
        if( containerURL != nil )
        {
            //NSLog( @"Path to share content is %@", [containerURL path] );
        }
        else
        {
            NSLog( @"Fail to call NSFileManager sharing" );
        }
    
        return [containerURL path];
    }
    
    // Unity script extern function shall call this function, interop NSString back to C-string, 
    // which then scripting engine will convert it to C# string to your script side.
    const char* _GetSharedFolderPath( const char* groupIdentifier )
    {
        NSString* baseurl = _GetSharedFolderPathInternal( CreateNSString( groupIdentifier ) );
        return MakeNSStringCopy( baseurl );
    }
    
  3. 从 Unity 导出 XCode 项目后,不要忘记再次打开“App Group”功能。
于 2016-08-17T11:34:19.770 回答