How to get a file path in an iOS app built in Unity?

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật