- Home /
[Solved] error copying file from streaming assets on IOS
Hello people, Im getting a error when I try to copy a file from StreamingAssets Application.dataPath
to Application.persistentDataPath
On android works fine, but on IOS im getting this error:
*App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Use HTTPS instead or add Exception Domains to your app's Info.plist.
*Cannot start load of Task <XXXXXXX>.<2> since it does not conform to ATS policy
*Task <XXXXXXX>.<2> finished with error [-1022] Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection., NSErrorFailingURLStringKey=http://localhost/private/var/containers/Bundle/Application/XXXXXXX/AppnName/Data/Raw/xlsx/puestaapunto.xlsx, NSErrorFailingURLKey=http://localhost/private/var/containers/Bundle/Application/XXXXXXX/AppnName/Data/Raw/xlsx/puestaapunto.xlsx, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <XXXXXXX>.<2>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <XXXXXXX>.<2>, NSUnderlyingError=0x280b88a80 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}}
SOLVED
this code works for android:
#if UNITY_ANDROID
string oriPath = Path.Combine("jar:file://" + Application.dataPath + "!/assets/xlsx/", ExcelFileName + ".xlsx"); //returns a DirectoryInfo object
UnityWebRequest reader = UnityWebRequest.Get(oriPath);
reader.SendWebRequest();
while (!reader.isDone) { }
realPath = Application.persistentDataPath + "/xlsx/" + ExcelFileName + ".xlsx"; //save the file with this name
File.WriteAllBytes(realPath, reader.downloadHandler.data);
Debug.Log("Excel File generated in app data from StreamingAssets");
#endif
and this code works for iOS:
#if UNITY_IOS
realPath = Application.persistentDataPath + "/xlsx/";
string oriPath = Path.Combine(Application.streamingAssetsPath + "/xlsx", ExcelFileName + ".xlsx"); //returns a DirectoryInfo object
byte[] xlsxFile = File.ReadAllBytes(oriPath);
File.WriteAllBytes(realPath + ExcelFileName + ".xlsx", xlsxFile); //save the file with this name
//File.WriteAllBytes(realPath + "puestaapunto.xlsx", xlsxFile); //alternative of above line, working
Debug.Log("Excel File generated in app data from StreamingAssets");
#endif
Answer by xxmariofer · Apr 06 at 10:08 AM
check this solution https://stackoverflow.com/a/55114077
Hi, thanks for answering, I tried this from that link:
public class BypassCertificate : CertificateHandler
{
protected override bool ValidateCertificate(byte[] certificateData)
{
//Simply return true no matter what
return true;
}
}
If I dont understand bad, all what I need to do is put that script code on plugin folder, right?, no need to attatch to some gameobject?, Im sure im missing something.
also I tried the method of edit info.plist on xcode:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
but do nothing, the error keeps as same.
Also I triying another method to avoid certificates:
public void Test6()
{
Directory.CreateDirectory(Application.persistentDataPath + "/xlsx"); //create a directory/folder
string oriPath = Path.Combine(Application.dataPath + "/Raw/xlsx/", "puestaapunto"); //returns a DirectoryInfo object //check if this line works on ios
realPath = Application.persistentDataPath + "/xlsx/";
//realPath = Application.persistentDataPath + "/xlsx/" + "puestaapunto2.xlsx";
var rewrite = Resources.Load<TextAsset>(oriPath);
byte[] data = rewrite.bytes;
File.WriteAllBytes(realPath, data);
}
but I get: nullReferenceException: Object reference not set to an instance of an object
to the plugin folder no, to your scripts folder. You would also need to add this to your coroutine
www.certificateHandler = new BypassCertificate();
oriPath is not a file (or it has no extension) so I am not sure what you are trying to load in resources.load
Hi again, finally I make it work for iOS with a correct syntaxis:
public void Test10()
{
realPath = Application.persistentDataPath + "/xlsx/";
string oriPath = Path.Combine(Application.streamingAssetsPath + "/xlsx", "puestaapunto" + ".xlsx");
Debug.Log("file read from streamingassets");
byte[] xlsxFile = File.ReadAllBytes(oriPath);
File.WriteAllBytes(realPath + "puestaapuntoWrited2.xlsx", xlsxFile);
Debug.Log("file copied to persistentDataPath");
}
Your answer
Follow this Question
Related Questions
Unity + XCode Problem (really wierd) 0 Answers
MarshalAs UnmanagedType.LPArray always returns with array of size 1 in iOS 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers