Search Unity

Native File Picker for Android & iOS [Open Source]

Discussion in 'Assets and Asset Store' started by yasirkula, Jun 15, 2020.

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Last edited: May 31, 2023
  2. KibsgaardUnityStudios

    KibsgaardUnityStudios

    Joined:
    Jul 4, 2017
    Posts:
    8
    yasirkula likes this.
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Thanks! The plugin you've mentioned probably works similar to NativeGallery.

    SimpleFileBrowser works fine on Android so I didn't consider creating another file picker plugin for Android. User experience may not be as pretty as a native file picker and I might add support for native file pickers using Document Providers in the future but I am not planning to do so anytime soon.
     
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    yahyabarroh likes this.
  5. Krisna_V

    Krisna_V

    Joined:
    Nov 21, 2017
    Posts:
    3
    Hello @yasirkula,
    This is very usefull, but somehow i cannot pick a file (files appear greyed out) in IOS even though i filling the file types parameter on NativeFilePicker.PickMultipleFiles(Action<string>, string[]).
    is there anything i've missed?

    Thanks :D
     
  6. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Are there any error messages in Xcode console and can I see your code?
     
  7. Luchunpen_0

    Luchunpen_0

    Joined:
    Aug 7, 2014
    Posts:
    58
    Hello @yasirkula.
    When compiling by Xcode, I have this error:

    Undefined symbols for architecture arm64:
    "_kUTTagClassFilenameExtension", referenced from:
    +[UNativeFilePicker convertExtensionToUTI:] in NativeFilePicker.o
    "_UTTypeCreatePreferredIdentifierForTag", referenced from:
    +[UNativeFilePicker convertExtensionToUTI:] in NativeFilePicker.o
    ld: symbol(s) not found for architecture arm64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    How can I fix it ? Thanks.
     
    ROBYER1 likes this.
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  9. Luchunpen_0

    Luchunpen_0

    Joined:
    Aug 7, 2014
    Posts:
    58
    Enabling automated IOS setup solved problem. Sorry i forgot to read readmy )).
    Thanks for help.
     
    yasirkula likes this.
  10. KibsgaardUnityStudios

    KibsgaardUnityStudios

    Joined:
    Jul 4, 2017
    Posts:
    8
  11. UDN_30011711-3d42-4608-a55a-5bf3a3109e16

    UDN_30011711-3d42-4608-a55a-5bf3a3109e16

    Joined:
    Nov 23, 2016
    Posts:
    6
    Can it work in editor as well?
     
  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    It doesn't but I might add support for it later.
     
  13. KubusOne

    KubusOne

    Joined:
    May 2, 2019
    Posts:
    7
    When building for iOS the following error occurs during the XCode build:
    Code (Boo):
    1. Libraries/Plugins/NativeFilePicker/iOS/NativeFilePicker.mm(26,24): error: assigning to 'id<UIDocumentPickerDelegate> _Nullable' from incompatible type 'const Class'
    2.         filePicker.delegate = self;
    3.                               ^~~~
    4. Libraries/Plugins/NativeFilePicker/iOS/NativeFilePicker.mm(55,25): error: assigning to 'id<UIDocumentPickerDelegate> _Nullable' from incompatible type 'const Class'
    5.                 filePicker.delegate = self;
    6.                                       ^~~~
    7. 2 errors generated.
    8.    + [arm64] Linking UnityFramework...
    9. ld: file not found: build/UnityFramework/Release/arm64/Libraries/Plugins/NativeFilePicker/iOS/NativeFilePicker.mm.obj
    Toolchain is Unity 2019.4.13f1 / XCode 12

    Any ideas how to fix it? Thanks in advance.
     
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You are building it on an actual Mac device, right?
     
  15. KubusOne

    KubusOne

    Joined:
    May 2, 2019
    Posts:
    7
    Actually we are building on a windows pc with the iOS project builder. But the XCode SDK that we use is up to date. We are using this project builder tools since years and did not have any similar issues before, so I guess(!) the issue is not related to the tool chain (e.g. your plugin "native gallery" works without any issues).

    We also tried the Unity cloud build, which also fails, but with a different error message:
    Code (Boo):
    1. ...
    2. ▸ ⚠;️  /BUILD_PATH/evospark-gmbh.augmaintenance.default-ios/temp20201203-3737-1dbo8wa/Libraries/Plugins/NativeFilePicker/iOS/NativeFilePicker.mm:3:18: extra tokens at end of #ifdef directive [-Wextra-tokens]
    3. 169: ▸ ❌;  Undefined symbols for architecture arm64
    4. 170: ▸ ❌;  ld: symbol(s) not found for architecture arm64
    5. 171: ▸ ❌;  clang: error: linker command failed with exit code 1 (use -v to see invocation)
    6. ...
    7. 332: ❌;  Undefined symbols for architecture arm64
    8. 333: ❌;  ld: symbol(s) not found for architecture arm64
    9. 334: ❌;  clang: error: linker command failed with exit code 1 (use -v to see invocation)
    10. 335: ▸ clang: error: linker command failed with exit code 1 (use -v to see invocation)
    11. 336: ! build of 'default-ios' failed. ! xcode build failed.
    12. 337: publishing finished successfully.
    13. 338: Finished: FAILURE
    XCode-Version for the cloud build is 11.4.1

    The project is only an empty project with nothing else but the native file picker - no other plugins included.
     
  16. KubusOne

    KubusOne

    Joined:
    May 2, 2019
    Posts:
    7
    Sorry, my last post was a bit incomplete.
    The previously posted error is for the build when ENABLED is set to false.

    Wenn ENABLED ist set to true, the following error occurs (again Unity cloud build with XCode 11.4.1):

    Code (Boo):
    1. 789: error: exportArchive: exportOptionsPlist error for key 'iCloudContainerEnvironment': expected one of {Development, Production}, but no value was provided
    2. 790: Error Domain=IDEFoundationErrorDomain Code=1 "exportOptionsPlist error for key 'iCloudContainerEnvironment': expected one of {Development, Production}, but no value was provided" UserInfo={NSLocalizedDescription=exportOptionsPlist error for key 'iCloudContainerEnvironment': expected one of {Development, Production}, but no value was provided}
    3. 791: Error packaging up the application
    4. 792: [!] Error packaging up the application
    5. 793: ! build of 'default-ios' failed. ! xcode build failed.
    6. 794: publishing finished successfully.
    7. 795: Finished: FAILURE
     
    yasirkula likes this.
  17. KubusOne

    KubusOne

    Joined:
    May 2, 2019
    Posts:
    7
    With the help of @yasirkula I was able to get the build running.
    In the NativeFilePicker.mm you have to change all lines with
    filePicker.delegate = self;
    into
        filePicker.delegate = (id) self;
    (lines 26 and 55).

    With this change my local build with the iOS project builder succeeds if I use XCode 12 (SDK 14.2). With an older XCode (SDK 13.2) it still fails.
    The cloud build is still not possible, as for the cloud build XCode 12 is not available yet.

    Can someone confirm that it depends on the XCode version if the Native File Picker can be build?

    Thanks @yasirkula for providing the fix.
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  19. Rafael_CS

    Rafael_CS

    Joined:
    Sep 16, 2013
    Posts:
    162
    Hello @yasirkula, thank you for this amazing plugin.

    How can i pick the byte[] from returned file paths?

    Can i use System.IO.File.ReadAllBytes(file)?

    I really don't know if i can read internal android file paths this away.

    Thanks in advance
    Att
     
    Nestiny likes this.
  20. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Yes, you can use all File functions on that path.
     
  21. zuuuuuuuu

    zuuuuuuuu

    Joined:
    Jan 22, 2021
    Posts:
    1
    Hello!! I really love this asset and it helps a lot for my program since im still a beginner!! Thank you!!

    I have a (stupid maybe) question. I noticed on your example code section on GitHub that you use several if-elseif to deal with different file types.

    Is that the only way to do it, like adding a huge list of if-elseif to handle each one of them? or is there way to do one if statement that allow user to pick any type of files? Thanks!!
     
  22. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    If your app can handle any file type, then you may want to use SimpleFileBrowser instead. Regardless, allowedFileTypes for any file type on Android should be
    */*
    and on iOS should be
    [public.item, public.content]
    (an array of 2 strings).
     
    Last edited: Aug 25, 2023
  23. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Thanks for the great plugin!

    Can it be used on iOS to pick any file from the photo gallery? I think right now its tied to icloud storage?
     
  24. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I wish Photos would be included in list of apps displayed by UIDocumentPickerController on iOS but unfortunately it isn't. I don't think it is possible to pick media from Photos with this plugin. You can have a look at NativeGallery.GetMixedMediaFromGallery, if you wish.
     
  25. See_Sharp

    See_Sharp

    Joined:
    Mar 3, 2016
    Posts:
    74
    I'm getting the following error when using the cloud build for iOS:


    Code (CSharp):
    1. 17326: +-------------+-------------------------------+
    2. 17327: |              Build environment              |
    3. 17328: +-------------+-------------------------------+
    4. 17329: | xcode_path  | /APPLICATION_PATH/Xcode12_2_0.app |
    5. 17330: | gym_version | 2.170.0                       |
    6. 17331: | sdk         | iPhoneOS14.2.sdk              |
    7. 17332: +-------------+-------------------------------+
    8. 17333: ▸       +[UNativeFilePicker convertExtensionToUTI:] in NativeFilePicker.o
    9. 17334: ▸   "_UTTypeCreatePreferredIdentifierForTag", referenced from:
    10. 17335: ▸       +[UNativeFilePicker convertExtensionToUTI:] in NativeFilePicker.o
    11. 17336: ▸ ld: symbol(s) not found for architecture arm64
    12. 17337: ▸ clang: error: linker command failed with exit code 1 (use -v to see invocation)
    Got latest plugin files from assetstore.
     
  26. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  27. See_Sharp

    See_Sharp

    Joined:
    Mar 3, 2016
    Posts:
    74
  28. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Does inserting
    pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework SystemConfiguration" );
    to this line resolve the issue? If not, can you reproduce the issue on a new Unity project with Cloud Build?
     
  29. See_Sharp

    See_Sharp

    Joined:
    Mar 3, 2016
    Posts:
    74
    That did get it a little bit further, now I'm stuck at:

    Code (CSharp):
    1. 1492: ▸ Code Signing Error: Provisioning profile "Provision profile xxxxxxxx" doesn't include the com.apple.developer.icloud-container-identifiers and com.apple.developer.icloud-services entitlements and doesn't match the entitlements file's value for the com.apple.developer.ubiquity-container-identifiers entitlement.
    2. 1493: ▸ Code Signing Error: Code signing is required for product type 'Application' in SDK 'iOS 14.2'
    3. 1494: ▸ Code Signing Error: Code signing is required for product type 'Application' in SDK 'iOS 14.2'
    4. 1495: ! build of 'default-ios' failed. ! xcode build failed.
    5. 1496: publishing finished successfully.
    6. 1497: Finished: FAILURE
    I have added an iCloud container, although from this point I have not much experience in iOS:

    upload_2021-3-22_10-6-54.png

    I also regenerated the appropriate files for the build server and reuploaded them, no idea if it was needed but did it anyway.

    Thanks for the help by the way!
     
  30. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Well, does switching to "Include CloudKit support" make any differentce? I wonder if you can reproduce this issue on a new project with default Player Settings and Cloud Build settings but only enabling NativeFilePicker's automated setup?
     
  31. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    In my case I have this same error when doing manual setup, app works fine with automatic but we have another plugin we need to use which is not working when we use automatic File picker postbuild settings

    **Edit I just made a fresh project to test the manual IOS setup out and came out with the same error so I made an issue here on github

    https://github.com/yasirkula/UnityNativeFilePicker/issues/9

    Thanks for your work on this fantastic plugin
     
    Last edited: Mar 26, 2021
  32. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    @mr-satru I found the fix for the manual setup!

    In the guide for manual steps it says:
    "add MobileCoreServices.framework to Link Binary With Libraries list in Build Phases", this is not clear as the screenshots show the Unity-Iphone Target selected on the left, one would assume you link the MobileCoreServices.framework to there - but you actually need to link it to the target 'UnityFramework' in that target's Build Phases - Link Binary With Libraries list.

    Screenshot 2021-03-26 at 19.37.41.png

    See the issue and full explanation here:
    https://github.com/yasirkula/UnityNativeFilePicker/issues/9#issuecomment-808472691
     
  33. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    Using the example for PDF here on IOS, I'm using the asset PDF Renderer from Unity asset store and passing it the path of the pdf from the example code you have but it's not working, do I need to do something with the '
    new string[] { pdfFileType } );' part of the call instead of using the string 'path'?
    https://github.com/yasirkula/UnityNativeFilePicker
     
  34. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    You don't need anything else. You can insert your code at
    Debug.Log( "Picked file: " + path );
    line of the example code. Are there any error messages in Xcode console? Can it also be possible that PDF Renderer doesn't support some PDF files?
     
    Last edited: Mar 31, 2021
  35. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    I meant to delete my post when I realised I was being very silly and had a silent error before the pdf reader got the string I was passing it, but my internet here died. It worked exactly as you said!
     
    Last edited: Apr 1, 2021
    yasirkula likes this.
  36. boothy1

    boothy1

    Joined:
    Feb 13, 2014
    Posts:
    6
    My Android build is fine and native file picker works correctly however the IOS build fails with the following errors:

    ld: warning: arm64 function not 4-byte aligned: _unwind_tester from /Users/simonbooth/Desktop/Projects/AppleBlossumIOS/Libraries/libiPhone-lib.a(unwind_test_arm64.o)
    Undefined symbols for architecture arm64:
    "_kUTTagClassFilenameExtension", referenced from:
    +[UNativeFilePicker convertExtensionToUTI:] in NativeFilePicker.o
    "_UTTypeCreatePreferredIdentifierForTag", referenced from:
    +[UNativeFilePicker convertExtensionToUTI:] in NativeFilePicker.o
    ld: symbol(s) not found for architecture arm64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    ld: warning: arm64 function not 4-byte aligned: _unwind_tester from /Users/simonbooth/Desktop/Projects/AppleBlossumIOS/Libraries/libiPhone-lib.a(unwind_test_arm64.o)
    Undefined symbols for architecture arm64:
    "_kUTTagClassFilenameExtension", referenced from:
    +[UNativeFilePicker convertExtensionToUTI:] in NativeFilePicker.o
    "_UTTypeCreatePreferredIdentifierForTag", referenced from:
    +[UNativeFilePicker convertExtensionToUTI:] in NativeFilePicker.o
    ld: symbol(s) not found for architecture arm64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)


    I'm using XCode 12.3 and Unity 2019.4

    Any suggestions of how to fix this?
     
    Last edited: Apr 25, 2021
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  38. dbillings

    dbillings

    Joined:
    Mar 3, 2020
    Posts:
    6
    hello @yasirkula I'm running into issues trying to open a PDF on android in it's internal PDF viewing options(photos/gallery/etc) stored locally in my unity project in Streaming Assets folder. Would you happen to have a plugin or know of a solution to solve this problem? I've found solutions for downloading and then opening. But am hitting a wall with trying to open a file that was built into it initially.

    Thank you for any assistance you can provide!
     
  39. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    I don't have a plugin for viewing PDF files but the problem you are encountering is probably related to storing the PDF files inside StreamingAssets. On Android, these files are stored inside the APK file, so they are not really files in the filesystem. They must be extracted from the APK first. You can do it via WWW (google should help), UnityWebRequest or 3rd party assets.
     
  40. leonardoduarteuerj

    leonardoduarteuerj

    Joined:
    Aug 10, 2020
    Posts:
    10
    Hello friend! I love your assets.
    I want to pick mp3 file from mobile and save the path. Like your amazing Native Gallery.
    How can I do this, please?
     
    zacharyaghaizu likes this.
  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    If you are supporting only Android, then you can use NativeGallery.GetAudioFromGallery. Otherwise, you can modify the example code so that it picks mp3 files instead of pdf files.
     
  42. unity_gSkPxib9Vm2_uQ

    unity_gSkPxib9Vm2_uQ

    Joined:
    Oct 7, 2020
    Posts:
    1
    hello, i cannot, for the life of me, where the AUTO_SETUP_FRAMEWORKS and AUTO_SETUP_ICLOUD are. i've been trying to solve the iOS building issue for days, and i even tried doing the manual set up but to no avail.
     
  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
  44. ribketxs

    ribketxs

    Joined:
    Apr 1, 2020
    Posts:
    1
    hello @yasirkula i have a question. The plugin can open a pdf file on IOS?
    because I can see the file, but when I select it nothing happens, the selection window closes and I do not receive any error in the console

    heres my code

    Code (CSharp):
    1. string pdfType = NativeFilePicker.ConvertExtensionToFileType("pdf");
    2.  
    3. #if UNITY_IOS && !UNITY_EDITOR
    4.      
    5.             NativeFilePicker.Permission permission = NativeFilePicker.PickFile((path) => { if( path == null )
    6.                     Debug.Log( "Operation cancelled" );
    7.                 else
    8.                     Debug.Log( "Picked file: " + path ); }, new string[] { pdfType});
    9.          
    10. #endif
     
  45. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    NativeFilePicker doesn't have this functionality, it can only pick files. Processing/opening these files must be handled in your script. Currently,
    Debug.Log( "Picked file: " + path );
    is executed when a PDF file is picked. You need to add your PDF opening logic there.
     
  46. nax_unity

    nax_unity

    Joined:
    Jan 24, 2018
    Posts:
    63
    Hello.
    I have a question.

    Is it possible to determine that a file is being read when a file is selected?
    When I select a large file, such as a video file, I don't know if it's loading.

    I want to display the loading.
     
  47. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Until the callback function is called (or permission returns Denied), you can assume that the file is loading. You just need to make sure that
    NativeFilePicker.IsFilePickerBusy()
    returns false before calling NativeFilePicker.PickFile.
     
  48. nax_unity

    nax_unity

    Joined:
    Jan 24, 2018
    Posts:
    63
    Hello.
    I have a question.

    When I select a video file stored in a USB memory, it waits for about 10 seconds.
    Is it possible to close the browser immediately after selecting it?

    I don't know if I can select it because I have been waiting for a long time.
     
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,879
    Video file is being copied to local storage during that time. Closing the browser immediately will require modification to the native source code. I unfortunately can't focus on this for the time being.
     
  50. nax_unity

    nax_unity

    Joined:
    Jan 24, 2018
    Posts:
    63
    Is it possible to determine whether copying to local storage is in progress?