- Home /
Opening a pdf file on iOS devices is failed
Hello there,
I am developing an app on both Android and iOS devices (says, iPhone and iPad). Some functions work well on Android devices (where I build them on Windows 7): opening a PDF file and uploading screen shots. Here I mainly ask a solution to open the PDF file on iOS devices.
Here are what I have done and what the code (C#) works well on Android devices:
The PDF file 'manual.pdf' is initially put in the 'Resources' folder (in Unity editor).
I change the file extension of the PDF file from 'pdf' to 'bytes'
When the function to load the PDF file is triggered, I load this file using the code:
TextAsset manual = Resources.Load("manual", typeof(TextAsset)) as TextAsset;
The 'TextAsset manual' is then saved as a PDF file on the devices:
System.IO.File.WriteAllBytes(Application.persistentDataPath + "/manual.pdf", manual.bytes);
I also tried this pieces of codes on iOS devices:
string path = System.IO.Path.Combine(Application.persistentDataPath, "manual.pdf");
FileStream fs = new FileStream(path, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
w.Write(manual.bytes);
w.Close();
fs.Close();
Then load the PDF file which was just saved onto the devices; I tried these codes:
Application.OpenURL(path);
Application.OpenURL("file://"+path);
Application.OpenURL("file:///"+path);
The above is what I have done and works well on Android devices, but not works on iOS devices at all (tested on new iPad).
Some questions I would like to ask:
Is there any better method to save the PDF file onto the devices when the app is installed?
I found that if one wants to open Facebook page using Facebook app, one should call OpenURL with the parameter: "fb://xxx...". Does it have the similar way to open a pdf file?
I am wondering if there is something wrong with these codes? The codes work well on Android devices and even on Mac's Unity to test it directly. Maybe I have to do some modifications on Build Settings?
Hopefully some people can provide me suggestions! Thank you.
Answer by xwx217 · May 27, 2014 at 06:49 AM
Hi! I want to write a function to open a pdf(that have build with the app) on the android platform.I googled your answer, see you use this code work well , So I write like this ,but it does't work ,is something i did't done,So I hope you can give me a help! Thinks a lot! this is my code:
using UnityEngine; using System.Collections; using System.IO; .... ... TextAsset pdf = Resources.Load("pdf", typeof(TextAsset)) as TextAsset; System.IO.File.WriteAllBytes(Application.persistentDataPath + "/pdf.pdf",pdf.bytes); string path = System.IO.Path.Combine(Application.persistentDataPath,"pdf.pdf"); System.IO.FileStream fs =new System.IO.FileStream(path,System.IO.FileMode.Create);
BinaryWriter w= new BinaryWriter(fs); w.Write(pdf.bytes); w.Close(); fs.Close(); // 加载PDF文档 Application.OpenURL(path); Application.OpenURL("file://"+path); Application.OpenURL("file:///"+path);
Your answer
![](https://koobas.hobune.stream/wayback/20220613093231im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Q about Building my game for so many devices 2 Answers
Detect RAM usage of program 0 Answers
UDPSend not working with iPad over LAN 0 Answers