Search Unity

iOS file association and launch app with custom method

Discussion in 'Unity Build Automation' started by AshyB, May 18, 2016.

  1. AshyB

    AshyB

    Joined:
    Aug 9, 2012
    Posts:
    191
    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;

    Code (csharp):
    1.    // INFO.PLIST
    2.   string plistPath = path + "/Info.plist";
    3.   PlistDocument plist = new PlistDocument();
    4.   plist.ReadFromString(File.ReadAllText(plistPath));
    5.  
    6.   // Get root
    7.   PlistElementDict rootDict = plist.root;
    8.  
    9.   // Change value of keys in Xcode plist
    10.   rootDict.SetBoolean("ITSAppUsesNonExemptEncryption", false);
    11.  
    12.   // this should allow the app to copy the PDF file to its local app dir under documents
    13.   rootDict.SetBoolean("UIFileSharingEnabled", true); // bool, string or int?
    14.  
    15.   // file association
    16.   var documentTypes = rootDict.CreateArray("CFBundleDocumentTypes");
    17.  
    18.   var docDic = documentTypes.AddDict();
    19.  
    20.   docDic.SetString("CFBundleTypeName", "PDF File");
    21.   docDic.SetString("LSHandlerRank", "Owner");
    22.   docDic.SetString("CFBundleTypeRole", "Viewer");
    23.  
    24.   var listContent = docDic.CreateArray("LSItemContentTypes");
    25.  
    26.   listContent.AddString("com.adobe.pdf");
    27.  
    28.   // Write to file
    29.   File.WriteAllText(plistPath, plist.WriteToString());
    30.  
    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 :)
     
    NAKAS likes this.
  2. AshyB

    AshyB

    Joined:
    Aug 9, 2012
    Posts:
    191
    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;

    Code (csharp):
    1. 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.
     
  3. TigerHix

    TigerHix

    Joined:
    Oct 20, 2015
    Posts:
    69
    Bump this. Right now there is still a lack of documentation of associating file types for both iOS and Android.
     
  4. NAKAS

    NAKAS

    Joined:
    Jan 16, 2013
    Posts:
    23
    Hey, this was very helpful and combined with some very helpful unity answers posts, mainly this one 4 android ( https://answers.unity.com/questions/1350799/convert-android-uri-to-a-file-path-unity-can-read.html ) 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.

    Code (CSharp):
    1. public void Start(){
    2.  
    3. #if UNITY_IOS && !UNITY_EDITOR
    4.         if(!Directory.Exists(Application.persistentDataPath + "/Imported"))
    5.         {  
    6.         //if it doesn't, create it
    7. Directory.CreateDirectory(Application.persistentDataPath + "/Imported");
    8.         }
    9.         //check for new files at persistanc edata path / inbox
    10.         string[] fileList = Directory.GetFiles (Application.persistentDataPath + "/Inbox");
    11.         foreach (string s in fileList){
    12.         Debug.Log (s);
    13.         //do something with filepath
    14.         //bodgy but lets get the last 4 char of string to put on end of file move
    15.         //this can also then be used as an if check to sort and do different things based on file type.
    16.         string lastEXT = s.Substring (s.Length-4);
    17.         File.Move (s, Application.persistentDataPath + "/Imported/import" + lastEXT );
    18.         //now that we removed the file we can
    19.         File.Delete(s);
    20.         }
    21.     #endif
    22. }
    23. void OnApplicationFocus(bool hasFocus)
    24.     { if (hasFocus == true){
    25.  
    26. #if UNITY_IOS && !UNITY_EDITOR
    27.             if(!Directory.Exists(Application.persistentDataPath + "/Imported"))
    28.             {  
    29.             //if it doesn't, create it
    30.             Directory.CreateDirectory(Application.persistentDataPath + "/Imported");
    31.             }
    32.             //check for new files at persistanc edata path / inbox
    33.             string[] fileList = Directory.GetFiles (Application.persistentDataPath + "/Inbox");
    34.             foreach (string s in fileList){
    35.             Debug.Log (s);
    36.             //do something with filepath
    37.             //bodgy but lets get the last 4 char of string to put on end of file move
    38.             //this can also then be used as an if check to sort and do different things based on file type.
    39.             //will have extra char in name if two char extention but that fix is easy if you are importing that stuff :)
    40.             string lastEXT = s.Substring (s.Length-4);
    41.             File.Move (s, Application.persistentDataPath + "/Imported/import" + lastEXT );
    42.             //now that we removed the file we can
    43.             File.Delete(s);
    44.             }
    45.  
    46.  
    47.  
    48.             #endif
    49.         }
    50. }
    51.  
     
    TigerHix likes this.