I’d like to retrieve file paths in an iOS app developed with Unity.
Place the following source code in Assets/Plugins/iOS.
FileSelector.mm
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "UnityInterface.h"
@interface FileSelectorDelegate : NSObject <UIDocumentPickerDelegate>
@end
@implementation FileSelectorDelegate
{
NSString* selectedFilePath;
}
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls
{
NSURL *url = [urls firstObject];
if (url)
{
BOOL securityScoped = [url startAccessingSecurityScopedResource];
if (securityScoped)
{
selectedFilePath = [url path];
UnitySendMessage("FileSelector", "SetSelectedFilePath", [selectedFilePath UTF8String]);
[url stopAccessingSecurityScopedResource];
}
else
{
NSLog(@"Failed to start accessing security scoped resource.");
}
}
}
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller
{
NSLog(@"Document picker was cancelled.");
}
@end
extern "C" void _openFileDialog()
{
FileSelectorDelegate *delegate = [[FileSelectorDelegate alloc] init];
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.data"] inMode:UIDocumentPickerModeImport];
documentPicker.delegate = delegate;
documentPicker.modalPresentationStyle = UIModalPresentationFullScreen;
UIViewController *viewController = UnityGetGLViewController();
[viewController presentViewController:documentPicker animated:YES completion:nil];
}
Place the following source code in Assets/Scripts.
FileSelector.cs
using UnityEngine;
using System.Runtime.InteropServices;
using TMPro;
public class FileSelector : MonoBehaviour
{
public TMP_Text tmp;
[DllImport("__Internal")]
private static extern void _openFileDialog();
private string selectedFilePath;
// Method to assign to On Click event of UI button
public void selectFile()
{
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
_openFileDialog();
}
else
{
Debug.Log("File dialog is only supported on iOS.");
}
}
// Method called from the native side
public void SetSelectedFilePath(string path)
{
selectedFilePath = path;
tmp.text = "Selected file path: " + selectedFilePath;
Debug.Log("Selected file path: " + selectedFilePath);
}
// Method to retrieve the selected file path
public string GetSelectedFilePath()
{
return selectedFilePath;
}
}
In the Inspector, add a button to open a file dialog on the screen and a TMP_Text component to display the path of the selected file.
Select iOS as the platform and proceed with the build.
Build the output project file and run it on iOS.
The selected file is a text file manually copied from Finder to the application’s folder in the File APP.
Upon pressing the load button on the device, nothing appears on the screen, and the following message appears in the Xcode console.
Failed to associate thumbnails for picked URL
file:///private/var/mobile/Containers/Data/Application/xxxx-xxxx-xxxx-xxxx-xxxx/Documents/Bea.vrl
with the Inbox copy
file:///private/var/mobile/Containers/Data/Application/xxxx-xxxx-xxxx-xxxx-xxxx/tmp/com.atinde.xrlite-Inbox/Bea.vrl:
Error Domain=QLThumbnailErrorDomain Code=102 “(null)”
UserInfo={NSUnderlyingError=0x301cde340 {Error
Domain=GSLibraryErrorDomain Code=3 “Generation not found”
UserInfo={NSDescription=Generation not found}}}
Could you please let us know if there are any issues?
Thank you in advance.