- Home /
iOS Loading Local PDF/HTML from StreamingAssets
In our standalone build, we use Application.OpenURL to open our local helpsystem (bunch of loose html files) and our training reference system (bunch of loose pdf files).
We have now been tasked with putting our application on on iPad and I noticed it doesn't appear we can use OpenURL with assets in StreamingAssets like we can on standalone.
After looking at many other posts, it appears (?) we might be able to copy these files from streamingassets to persistentdata and then use Application.OpenURL, but I've yet to get it working.
For testing, I have a simple pdf and html file that reside in the root of StreamingAssets and am using the following code to try to open either one. Has anybody gotten this to work? I've seen some say it used to work, but no more... (I'm on Unity 4.6 RC2 and iOS 8.0).
With the following code, the files exist in streamingassets, the www1.bytesDownloaded field is correct after getting via WWW, and the files are copied to persistentDataPath correctly. I just can't seem to get the Application.OpenURL to work with either the html or the pdf... I assume I'm supposed to use the file:// protocol schema but maybe this is the issue. Any pointers are greatly appreciated.
public IEnumerator LoadLocal()
{
string fromPath = Application.streamingAssetsPath + "/";
string toPath = Application.persistentDataPath + "/";
string pdfToLoad = "simple.pdf";
string htmlToLoad = "simplehtml.html";
if(File.Exists(fromPath + pdfToLoad))
{
UnityEngine.Debug.Log(string.Format("Sees {0} in streamingassets\n", pdfToLoad));
}
if(File.Exists(fromPath + htmlToLoad))
{
UnityEngine.Debug.Log(string.Format("Sees {0} in streamingassets\n", htmlToLoad));
}
WWW www1 = new WWW("file://" + fromPath + pdfToLoad);
yield return www1;
UnityEngine.Debug.Log("Sees isDone as : " + www1.isDone);
UnityEngine.Debug.Log("Sees bytesDownloaded as : " + www1.bytesDownloaded);
if (www1.error != null)
{
UnityEngine.Debug.Log("Error in www\n");
UnityEngine.Debug.Log(www1.error);
}
File.WriteAllBytes(toPath + pdfToLoad , www1.bytes);
UnityEngine.Debug.Log(string.Format("Wrote pdf to {0}\n", toPath + pdfToLoad\n));
if(File.Exists(toPath + pdfToLoad))
{
UnityEngine.Debug.Log("Sees pdf in written to persistentdata location\n");
}
UnityEngine.Debug.Log("trying to open " + toPath + pdfToLoad);
// none of these work, even if I change to http:// when using the htmlToLoad
// Application.OpenURL("file://" + toPath + pdfToLoad);
// Application.OpenURL("file:///" + toPath + pdfToLoad);
// Application.OpenURL("File:///" + toPath + pdfToLoad);
}
SOLUTION
It didn't look this was going to be possible or ideal, so the solution for us involved writing native iOS plugins following Unity's docs that used UIWebView (for local html files) and UIDocumentInteractionController (for local pdf files) to view the documents without having to leave the app. We were able to load the files from their respective Application.dataPath + "/Raw" locations.
Hi, that is very interesting. Would you share some code?
Hi @DottorFeelgood, I've been fighting with this all morning and finally got it working. If you're still interested have a look at my answer here: http://answers.unity3d.com/answers/1426652/view.html