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:

  1. The PDF file ‘manual.pdf’ is initially put in the ‘Resources’ folder (in Unity editor).

  2. I change the file extension of the PDF file from ‘pdf’ to ‘bytes’

  3. 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;

  4. The ‘TextAsset manual’ is then saved as a PDF file on the devices:

    System.IO.File.WriteAllBytes(Application.persistentDataPath + “/manual.pdf”, manual.bytes);

  5. 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();

  6. 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:

  1. Is there any better method to save the PDF file onto the devices when the app is installed?
  2. 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?
  3. 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.

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