iOS file association and launch app with custom method

Hi guys,

I’m using unity cloud build service to build my iOS app. I’ve created a PostProcessBuild editor script which I use to set up my file association (I’m trying to load a .pdf file into my app and run a method on it). The file association works fine. I can email my ipad a pdf file and when I click and hold on the pdf attachment in mail my app pops up as a selection to open it. When I click it, it launches my app but then that’s where I’m stuck. I need to call a method I have in unity to process that pdf file.

For other noobs out there here is the code I used to set up file association;

   // INFO.PLIST
  string plistPath = path + "/Info.plist";
  PlistDocument plist = new PlistDocument();
  plist.ReadFromString(File.ReadAllText(plistPath));

  // Get root
  PlistElementDict rootDict = plist.root;

  // Change value of keys in Xcode plist
  rootDict.SetBoolean("ITSAppUsesNonExemptEncryption", false);

  // this should allow the app to copy the PDF file to its local app dir under documents
  rootDict.SetBoolean("UIFileSharingEnabled", true); // bool, string or int?

  // file association
  var documentTypes = rootDict.CreateArray("CFBundleDocumentTypes");

  var docDic = documentTypes.AddDict();

  docDic.SetString("CFBundleTypeName", "PDF File");
  docDic.SetString("LSHandlerRank", "Owner");
  docDic.SetString("CFBundleTypeRole", "Viewer");

  var listContent = docDic.CreateArray("LSItemContentTypes");

  listContent.AddString("com.adobe.pdf");

  // Write to file
  File.WriteAllText(plistPath, plist.WriteToString());

I’ve been looking over all the didFinishLaunchingWithOptions posts but I don’t know how to do this from a postprocessbuild side. It looks like everyone is creating plugins/editing files from xcode to do it.

How can I call my MethodName() from a unityscript.cs file without using a mac/xcode.

I thought maybe I could create MyOwnAppController.mm file in my project and then add the didfinishlaunchingwithoptions method but i’m a little lost.

Any help would be greatly appreciated :slight_smile:

1 Like

Alright I found a way to achieve the end result I wanted which was being able to open a pdf in my app. Using the file association above, click on the pdf in the mail app and open with my app.

Thanks to;

rootDict.SetBoolean("UIFileSharingEnabled", true);

Now the pdf has been copied to the app’s documents/inbox folder so I can just open it from there using normal unity methods. Problem was, I was using GetWorkingDirectory() when I needed to use Application.persistentDataPath.

This works. But I’d still like to know how to do it using the didFinishLaunchingWithOptions approach if anyone knows.

Bump this. Right now there is still a lack of documentation of associating file types for both iOS and Android.

Hey, this was very helpful and combined with some very helpful unity answers posts, mainly this one 4 android ( Convert Android URI to a file path Unity can read - Questions & Answers - Unity Discussions ) I was able to get a solid cross platform open into unity app working without really having to do that much extra, just android manifest and Xcode setup.

@TigerHix yes I agree there is a lack of documentation, mainly it’s just really hard to search for to filter out the non dev questions. But it does seems to be there, for android iOS at least. I have found one resource that seems to also modify windows registries from unity to give windows file associations and the biggest mystery to me at least right now is the Mac stuff, but it seems like it can be done similarly to how iOS is done, but idk as I couldn’t really find much documentation out there.

Anywho for this problem @AshyB had, The simple bodgy way to solve this without having to write any code outside of unity is to put a check in start and OnApplicationFocus that check for any new files in the application.persistantdatapath/inbox directory. If there is a new file you can grab extension if needed and then rename it and move it to a new directory or just do what ever you need to with it.

public void Start(){

#if UNITY_IOS && !UNITY_EDITOR
        if(!Directory.Exists(Application.persistentDataPath + "/Imported"))
        {   
        //if it doesn't, create it
Directory.CreateDirectory(Application.persistentDataPath + "/Imported");
        }
        //check for new files at persistanc edata path / inbox
        string[] fileList = Directory.GetFiles (Application.persistentDataPath + "/Inbox");
        foreach (string s in fileList){
        Debug.Log (s);
        //do something with filepath
        //bodgy but lets get the last 4 char of string to put on end of file move
        //this can also then be used as an if check to sort and do different things based on file type.
        string lastEXT = s.Substring (s.Length-4);
        File.Move (s, Application.persistentDataPath + "/Imported/import" + lastEXT );
        //now that we removed the file we can
        File.Delete(s);
        }
    #endif 
}
void OnApplicationFocus(bool hasFocus)
    { if (hasFocus == true){

#if UNITY_IOS && !UNITY_EDITOR
            if(!Directory.Exists(Application.persistentDataPath + "/Imported"))
            {   
            //if it doesn't, create it
            Directory.CreateDirectory(Application.persistentDataPath + "/Imported");
            }
            //check for new files at persistanc edata path / inbox
            string[] fileList = Directory.GetFiles (Application.persistentDataPath + "/Inbox");
            foreach (string s in fileList){
            Debug.Log (s);
            //do something with filepath
            //bodgy but lets get the last 4 char of string to put on end of file move
            //this can also then be used as an if check to sort and do different things based on file type.
            //will have extra char in name if two char extention but that fix is easy if you are importing that stuff :)
            string lastEXT = s.Substring (s.Length-4);
            File.Move (s, Application.persistentDataPath + "/Imported/import" + lastEXT );
            //now that we removed the file we can
            File.Delete(s);
            }



            #endif
        }
}
1 Like