2017-04-17 60 views
0

我有一個函數可以下載json並嘗試在設備上保存副本。它在Android和編輯器上工作正常。不能把我的手指放在它上面。 Ipad運行IOS 10,如果增加了任何內容,則統一設置爲9。寫入IOS文件系統時出錯在Android和編輯器上工作

下面是統一碼和xcode上的錯誤。

MonoDevelop的

using UnityEngine; 
using System.Collections; 
using System; 
using System.IO; 
using System.Collections.Generic; 
using LitJson; 


public class loadJSONHome : MonoBehaviour { 

public string url; 
public string url2; 
private string jsonString; 
public static loadJSONHome instance; 
public GameObject preloader; 

void Awake(){ 
    if(instance==null){ 
    instance = this; 
    } 

} 
void Start(){ 
     WWW www2 = new WWW(url); 
     StartCoroutine(WaitForRequest(www2)); 
    // preloader.SetActive(true); 

} 

IEnumerator WaitForRequest(WWW www2){ 
    yield return www2; 

    // check for errors 
    if (www2.error == null) 
    { 
    //SAVE JSON FROM ONLINE LOCALLY 
    jsonString = www2.text; 

     writeStringToFile(jsonString, "FoodnDrink_Cats.json"); 

    writeListings(); 
    } else { 
     Debug.Log("WWW2 Error: "+ www2.error); 
    }  
} 




    void writeListings(){ 

    WWW www3 = new WWW(url2); 
     StartCoroutine(WaitForRequest2(www3)); 
    } 

IEnumerator WaitForRequest2(WWW www3){ 
    yield return www3; 

    // check for errors 
    if (www3.error == null) 
    { 
    //SAVE JSON FROM ONLINE LOCALLY 
    jsonString = www3.text; 

     writeStringToFile(jsonString, "listings_FoodnDrink.json"); 

    } else { 
     Debug.Log("WWW3 Error: "+ www3.error); 
    } 

    //preloader.SetActive(false); 
    } 











    public void writeStringToFile(string str, string filename){ 
     #if !WEB_BUILD 
      string path = pathForDocumentsFile(filename); 
      FileStream file = new FileStream (path, FileMode.Create, FileAccess.Write); 

      StreamWriter sw = new StreamWriter(file); 
      sw.WriteLine(str); 

      sw.Close(); 
      file.Close(); 
     #endif 
    } 

    public string readStringFromFile(string filename){ //, int lineIndex) 
     #if !WEB_BUILD 
      string path = pathForDocumentsFile(filename); 

      if (File.Exists(path)) 
      { 
       FileStream file = new FileStream (path, FileMode.Open, FileAccess.Read); 
       StreamReader sr = new StreamReader(file); 

      string str = null; 
      str = sr.ReadToEnd(); 

      sr.Close(); 
      file.Close(); 

      return str; 
     }else{ 
     return null; 
     } 
     #else 
     return null; 
     #endif 
    } 


    public 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); 
     } 
    } 





} 

XCODE

位置服務更新未啓用。在查詢上一個位置之前檢查LocationService.status。

IsolatedStorageException:找不到路徑的一部分「/var/containers/Bundle/Application/BC5FA0A8-9767-4689-8707-EBB7FA1886F5/Documents/FoodnDrink_Cats.json」。在System.IO.FileStream..ctor(System.String路徑,FileMode模式,FileAccess訪問,FileShare共享,Int32 bufferSize,布爾匿名,FileOptions選項)[0x00000]中的 (System.String路徑,FileMode模式,FileAccess訪問,FileShare共享,Int32緩衝區大小,布爾isAsync,布爾匿名)[0x00000]在:0 在System.IO.FileStream..ctor(System.String路徑,FileMode模式,FileAccess訪問)[0x00000]在:0 在loadJSONHome.writeStringToFile(System.String STR,System.String文件名)[0x00000]在:0 在loadJSONHome + c__Iterator5.MoveNext()[0x00000]在:0

(文件名:目前在il2cpp上不可用:-1)

幫助讚賞。

+0

您有多個問題,在你的代碼,我相信。對於位置服務公園,請執行以下操作:http://stackoverflow.com/a/24063578/1035008 –

+0

感謝您的幫助。很多,如果問題調試代碼只是我需要清理的一些未使用的變量。問題在於您提供的位置服務,主要問題是隔離存儲線。如果你可以從查找路徑功能幫助找到它,我們將不勝感激。 – Diego

+0

它沒有工作:(我要嘗試更新統一版本和xcode,看看它是如何平移 – Diego

回答

0

我想出了一個youtube視頻的幫助(發佈在最後)。

我改變了以二進制數據形式存儲的方法來解決蘋果隔離和授權文件問題。這也是一個更清潔。

下面的代碼:

using UnityEngine; 
using System.Collections; 
using System; 
using System.Runtime.Serialization.Formatters.Binary; 
using System.IO; 


public class loadJSONHome : MonoBehaviour { 

    public string url; 
    public string url2; 
    private string jsonString; 
    public static loadJSONHome instance; 
    public GameObject preloader; 

    void Awake(){ 
     if(instance==null){ 
     instance = this; 
     } 

    } 
    void Start(){ 
      WWW www2 = new WWW(url); 
      StartCoroutine(WaitForRequest(www2)); 
      preloader.SetActive(true); 

    } 

    IEnumerator WaitForRequest(WWW www2){ 
     yield return www2; 

     // check for errors 
     if (www2.error == null) 
     { 
     //SAVE JSON FROM ONLINE LOCALLY 
     jsonString = www2.text; 

      Save(jsonString, "FoodnDrink_Cats.json"); 

     writeListings(); 
     } else { 
      Debug.Log("WWW2 Error: "+ www2.error); 
     }  
    } 




    void writeListings(){ 

     WWW www3 = new WWW(url2); 
      StartCoroutine(WaitForRequest2(www3)); 
     } 

    IEnumerator WaitForRequest2(WWW www3){ 
     yield return www3; 

     // check for errors 
     if (www3.error == null) 
     { 
     //SAVE JSON FROM ONLINE LOCALLY 
     jsonString = www3.text; 

      Save(jsonString, "listings_FoodnDrink.json"); 

     } else { 
      Debug.Log("WWW3 Error: "+ www3.error); 
     } 

     preloader.SetActive(false); 
    } 



    public void Save(string json, string filename){ 

     BinaryFormatter bf = new BinaryFormatter(); 
     FileStream file = File.Open(Application.persistentDataPath + "/" +filename, FileMode.Create); 

     myFile current = new myFile(); 
     current.theFile = json; 

     bf.Serialize(file, current); 
     file.Close(); 


    } 


    public string Load(string filename){ 

     if(File.Exists(Application.persistentDataPath + "/" + filename)){ 
      BinaryFormatter bf = new BinaryFormatter(); 
      FileStream file = File.Open(Application.persistentDataPath + "/" + filename, FileMode.Open); 
      myFile data = (myFile)bf.Deserialize(file); 
      file.Close(); 
      string fileinfo = data.theFile; 
      return fileinfo; 
     }else{ 
      return null; 
     } 
    } 




} 

[Serializable] 
class myFile { 

    public string theFile; 


} 

這裏的YouTube視頻:https://www.youtube.com/watch?v=yxziv4ISfys