unity3d 如何在Unity iOS中打开PDF文件?

2exbekwf  于 2023-08-06  发布在  iOS
关注(0)|答案(2)|浏览(277)

我在Android上有应用程序。我想把它转换成iOS。这是我在Unity做的还有一个问题。我无法在iOS中打开pdf文件。
第一个月
这是Android的代码行。我可以用这行打开pdf文件。Application.OpenURL不适用于本地文件。
我怎么能做到这一点的iOS?

mspsb9vt

mspsb9vt1#

在iOS上,显然你必须通过UIDocumentInteractionController进行本地化。
你可以创建一个原生插件,比如

Assets/Plugins/iOS/iOSOpenURL.mm

字符串
内容如下

#import <UIKit/UIKit.h>

@interface FileOpener : NSObject

- (void)openFileAtPath:(NSString *)path;

@end

@implementation FileOpener

- (id)init
{
    self = [super init];
    return self;
}

- (void)openFileAtPath:(NSString *)path {
    NSURL *fileURL = [NSURL fileURLWithPath:path];
    UIDocumentInteractionController *controller = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
    controller.delegate = (id<UIDocumentInteractionControllerDelegate>)self;
    [controller presentPreviewAnimated:YES];
}

@end

// NOTE: parts below here are coming from the example under https://docs.unity3d.com/uploads/Examples/iPhoneNativeCodeSample.zip

static FileOpener* fileOpener = nil;

// Converts C style string to NSString
NSString* CreateNSString (const char* string)
{
    if (string)
        return [NSString stringWithUTF8String: string];
    else
        return [NSString stringWithUTF8String: ""];
}

// When native code plugin is implemented in .mm / .cpp file, then functions
// should be surrounded with extern "C" block to conform C function naming rules
extern "C" 
{
    void _OpenFile(const char* path)
    {
        if (fileOpener == nil)
            fileOpener = [FileOpener alloc];

        [fileOpener openFileAtPath: CreateNSString(path)];
    }
}


它基本上将您的请求转发到上述UIDocumentInteractionController
然后在它旁边有相应的c#文件,例如

Assets/Plugins/iOS/iOSOpenURL.cs


与此内容

using System.Runtime.InteropServices;

public static class iOSOpenURL
{
    [DllImport("__Internal")]
    private static extern void _OpenFile(string path);

    public static void OpenFile(string path)
    {
#if UNITY_IOS && !UNITY_EDITOR
        _OpenFile(path);
#else
        throw new OperationNotSupportedException("Only works on iOS!");
#endif
    }
}


然后回到Unity编辑器中单击本机iOSOpenURL(它应该有一个插件图标),并在检查器中进一步配置它-因为使用相应的文件夹名称,它应该已经选择了iOS作为支持的目标平台-这意味着对于任何其他构建目标,此插件将不会包含在__Internal.dll
参见See also Building plug-ins for iOS
最后,再加上你已经拥有的Unity-Android-Files-Opener,你基本上可以把它们组合成这样的东西。

public static class LocalFileUtility
{
    public static void OpenFile(string filePath)
    {
#if UNITY_ANDOIRD && !UNITY_EDITOR
        UnityAndroidOpenUrl.AndroidOpenUrl.OpenFile(filePath, "application/pdf");
#elif UNITY_IOS && !UNITY_EDITOR
        iOSOpenURL.OpenFile(filePath);
#else
        Application.OpenURL(filePath);
#endif
    }
}

rjzwgtxy

rjzwgtxy2#

这是derHugo's answer的后续版本。
我试图实现derHugo的代码,但在执行过程中似乎遇到了一个错误:

UIDocumentInteractionController delegate must implement documentInteractionControllerViewControllerForPreview: to allow preview

字符串
为了避免这种情况,应该在derHugo的代码中添加:
1/在界面中:

- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller;


2/在执行中

- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
    return UnityGetGLViewController();

相关问题