Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by diegomh7 · Apr 17, 2017 at 08:11 PM · c#androidiosxcodefilestream

*SOLVED* Error writing to IOS file system works on android & editor

Hi all, Ive got a function that downloads json and the tries to save a copy on the device. It works fine on android and in the editor. Can't put my finger on it. Ipad is running IOS 10 and settings in unity are 9 upwards if that adds anything.

Below is the unity code plus the error on 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

Location service updates are not enabled. Check LocationService.status before querying last location.

IsolatedStorageException: Could not find a part of the path "/var/containers/Bundle/Application/BC5FA0A8-9767-4689-8707-EBB7FA1886F5/Documents/FoodnDrink_Cats.json". at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in :0 at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean isAsync, Boolean anonymous) [0x00000] in :0 at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access) [0x00000] in :0 at loadJSONHome.writeStringToFile (System.String str, System.String filename) [0x00000] in :0 at loadJSONHome+c__Iterator5.MoveNext () [0x00000] in :0

(Filename: currently not available on il2cpp Line: -1)

Help appreciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by diegomh7 · Apr 18, 2017 at 07:21 PM

I figured it out with help of a youtube video (posted at the end).

I changed the methods to store in binary data form to get around apple isolation and authorization file issues. This is also a lot cleaner.

Here's the code :

 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;


 }


Here's the Youtube Video : https://www.youtube.com/watch?v=yxziv4ISfys

The location services is next to resolve but I think it's down to xcode p.list gps settings

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

369 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Crazy high RAM usage on IOS 0 Answers

Can I export IOS project from Windows machine with Unity 5.x ? 2 Answers

ios app crashes on start 0 Answers

My sprite disappears from the camera view after some time moving 5 Answers

Why can't I send out OSC messages from my iOS app? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges