c# - iOS 8 中的 IsolatedStorage 异常

标签 c# objective-c xcode unity3d ios8

我被要求修复一个在 Unity 中构建的导致 iOS 8 出现问题的 iPhone 游戏。该游戏在所有以前版本的 iOS 中都可以运行,但在 iOS 8 中它会加载所有启动画面,然后卡在以下 LoadGameData 函数上我在 Xcode 6 中遇到这个错误:

IsolatedStorageException: Could not find a part of the path "/private/var/mobile/Containers/Bundle/Application/D9F47ED4-40E1-420E-A5A8-836F52BC301C/Documents/GameSave3.dat".
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0 
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean isAsync, Boolean anonymous) [0x00000] in <filename unknown>:0 
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access) [0x00000] in <filename unknown>:0 
  at LoadSave.LoadGameData () [0x00000] in <filename unknown>:0 
  at MainLoop.Update () [0x00000] in <filename unknown>:0 

(Filename:  Line: -1)

这是包含 LoadGameData() 函数的 LoadSave.cs 文件:

using UnityEngine;
using System.Collections;
using System.IO;


public class LoadSave : MonoBehaviour {

    void Start () 
    {
        Debug.Log ("Start LoadSave");

        Globals.g_loadSave = this;  
    }

    void Update () {

    }

    void SetGameDataDefaults () 
    {
        for (int i = 0; i < (int)World.ActivityType.kNum; i++)
        {
            Globals.g_main.world.notification_day[i] = -1;
            Globals.g_main.world.notification_hour[i] = -1;
            Globals.g_main.world.notification_minute[i] = -1;
        }                   
    }

    string pathForDocumentsFile( string filename ) 
    { 
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            string path = Application.dataPath.Substring( 0, Application.dataPath.Length - 5 );
            path = path.Substring( 0, path.LastIndexOf( '/' ) );
            return Path.Combine( Path.Combine( path, "Documents" ), filename );
        }       
        else if(Application.platform == RuntimePlatform.Android)
        {
            string path = Application.persistentDataPath;   
            path = path.Substring(0, path.LastIndexOf( '/' ) ); 
            return Path.Combine (path, filename);
        }   
        else 
        {
            string path = Application.dataPath; 
            path = path.Substring(0, path.LastIndexOf( '/' ) );
            return Path.Combine (path, filename);
        }
    }   

    public void SaveGameData()
    {
        Utilities.Log("Write to GameSave File");                                            

        string path = this.pathForDocumentsFile("GameSave3.dat" );      
        FileStream file = new FileStream (path, FileMode.Open, FileAccess.Write);           
        this.WriteGameDataToFile(file);     
        file.Close();
    }

    public void LoadGameData()
    {
        string path = pathForDocumentsFile( "GameSave3.dat" );

        //if the file has not been made yet then set defaults and create it...
        if (!File.Exists(path))
        {
            Utilities.Log("Create GameSave File");                                  
            this.SetGameDataDefaults();
            FileStream newFile = new FileStream (path, FileMode.Create, FileAccess.Write);
            this.WriteGameDataToFile(newFile);
            newFile.Close();

            return;
        }

        //Otherwise just read it

        Utilities.Log("Read GameSave File");                        

        FileStream file = new FileStream (path, FileMode.Open, FileAccess.Read);        
        this.ReadGameDataFromFile(file);
        file.Close();
    }



    void ReadGameDataFromFile(FileStream filestream)
    {
        BinaryReader reader = new BinaryReader(filestream);

        for (int i = 0; i < World.kNumOpenPlayGamesRemembered; i++)
        {
            Globals.g_main.world.lastOpenPlayGames[i] = (GameType)reader.ReadInt32();
        }       

        Globals.g_main.world.openPlayRememberIndex = reader.ReadInt32();

        for (int i = 0; i < (int)World.ActivityType.kNum; i++)
        {
            Globals.g_main.world.numBadges[i] = reader.ReadInt32();
        }       

        //stored local notification info

        for (int i = 0; i < (int)World.ActivityType.kNum; i++)
        {
            Globals.g_main.world.notification_day[i] = reader.ReadInt32();
            Globals.g_main.world.notification_hour[i] = reader.ReadInt32();
            Globals.g_main.world.notification_minute[i] = reader.ReadInt32();
        }               

    }   


    void WriteGameDataToFile(FileStream filestream)
    {
        BinaryWriter writer = new BinaryWriter(filestream);

        for (int i = 0; i < World.kNumOpenPlayGamesRemembered; i++)
        {
            writer.Write((int)Globals.g_main.world.lastOpenPlayGames[i]);
        }       

        writer.Write(Globals.g_main.world.openPlayRememberIndex);

        for (int i = 0; i < (int)World.ActivityType.kNum; i++)
        {
            writer.Write((int)Globals.g_main.world.numBadges[i]);
        }       

        //stored local notification info

        for (int i = 0; i < (int)World.ActivityType.kNum; i++)
        {
            writer.Write((int)Globals.g_main.world.notification_day[i]);
            writer.Write((int)Globals.g_main.world.notification_hour[i]);
            writer.Write((int)Globals.g_main.world.notification_minute[i]);
        }       


    }
}

令我困惑的是,为什么它在除 iOS 8 以外的所有 iOS 版本中都能正常工作。知道问题出在哪里吗?谢谢。

最佳答案

我终于明白了。问题出在 pathForDocumentsFile() 函数中使用的 dataPath 方法。 iOS 的早期版本不需要路径是持久的,因为我将它用于下面的机器人。将 dataPath 方法替换为 persistentDataPath 方法修复了 iOS 8 中的问题。

if (Application.platform == RuntimePlatform.IPhonePlayer){
    string path = Application.persistentDataPath.Substring( 0, Application.persistentDataPath.Length - 5 );
    path = path.Substring( 0, path.LastIndexOf( '/' ) );
    return Path.Combine( Path.Combine( path, "Documents" ), filename );
}

关于c# - iOS 8 中的 IsolatedStorage 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26175906/

相关文章:

c# - 支持从 VSTO 加载项进行日志记录的最简单方法

ios - Firebase 持久化,清除 Firebase 缓存

iphone - 向现有 Xcode 项目本地化添加新语言

启动应用程序时出现 iOS 10.1 警告

c# - 在 .net 析构函数中重新创建对 'this' 的根引用是否合法?

c# - 与任务中的对象通信

javascript - 参数 'href' 应该代表 facebook 中的有效 URL

ios - '位置管理器(_ :didRangeBeacons:inRegion: )' conflicts with optional requirement method ' locationManager(_:didRangeBeacons:inRegion:)' in protocol

ios - 在使用 @IBOutletCollection 扩展 ObjC 子类的 Swift 类中不符合键值编码

ios - ImageView 16 :9 aspect ratio