uniapp原生插件开发实战——iOS打开文件到自己的app

用原生开发获取文件的名称、路径等能力封装为一个插件包供前端使用

首先根据ios插件开发教程,创建一个插件工程,template 选framework

开始编写代码:

iOS 9 及以下版本会调用以下方法:

复制代码
- (BOOL)application:(UIApplication *_Nullable)application openURL:(NSURL *_Nullable)url sourceApplication:(NSString *_Nullable)sourceApplication annotation:(id _Nonnull )annotation{
}

iOS9以上的版本会调用以下方法:

复制代码
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options {

这两个方法实现的功能是一样的,代码相同。

主要功能点:

  1. 获取ios沙盒目录所在位置,将文件复制到iOS系统应用沙盒目录下的"/Library/Pandora/documents",防止文件因权限问题无法读取

  2. 回调函数调用时机:等到文件复制iOS系统应用沙盒目录下的"/Library/Pandora/documents"后调用,防止前端读取不到文件信息;这里使用的是:

    uni.requireNativePlugin("插件名")?.render({}, function(result){
    // result为插件传回来的文件信息,这里包含fileName,filePath
    })

  3. 基于2原生插件中需要在render判断下是否已经获取到文件的具体信息,如果没有则设置回调函数:

    #pragma mark - Export Method

    • (void)render:(NSDictionary *)options callback:(WXModuleKeepAliveCallback)callback
      {
      if (SingleTon.getInstance.dict) {
      callback(SingleTon.getInstance.dict,YES);
      }
      else {
      SingleTon.getInstance.openURLCallback = ^(NSSObject *obj) { // 设置一个回调函数在openURL执行完成后调用
      if (callback) {
      callback(obj,YES);
      }
      };
      }
      }
  4. 最后通过plus.io.resolveLocalFileSystemURL获取文件对象

读取文件代码及功能点解释:

复制代码
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);// 获取应用程序的 Library 目录路径。

    NSString *documentsDirectory = [paths firstObject];//获取 Library 目录下的第一个路径。

    NSString *pandoraDocumentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Pandora/documents"];//创建一个新的路径,将 "Pandora/documents" 目录添加到 Library 目录路径后面。

    NSFileManager *fileManager = [NSFileManager defaultManager];//创建一个 NSFileManager 对象,用于文件管理操作。

    if (![fileManager fileExistsAtPath:pandoraDocumentsDirectory]) {//检查指定路径是否存在

    BOOL success = [fileManager createDirectoryAtPath:pandoraDocumentsDirectory withIntermediateDirectories:YES attributes:nil error:&error];// 创建指定路径的文件夹,如果创建成功则返回 YES。

    if (url != nil) {//检查传入的 URL 是否为空。

    NSString *path = [url path];//从 URL 中获取路径。

    NSString *decodedPath = [path stringByRemovingPercentEncoding];//对路径进行解码,去除百分号编码。

    NSString *fileName = [decodedPath lastPathComponent];//获取路径中的文件名。

    NSString *fileExtension = [fileName pathExtension];//获取文件的扩展名。
    NSString *str = @"file.";

    NSString *fileName_ = [str stringByAppendingString:fileExtension];// 将 "file." 和文件扩展名拼接在一起。

    NSString *destinationPath = [pandoraDocumentsDirectory stringByAppendingPathComponent:fileName_];//创建目标文件的完整路径。

    if ([fileManager fileExistsAtPath:destinationPath]) {//检查目标文件是否已经存在。

    BOOL removeSuccess = [fileManager removeItemAtPath:destinationPath error:&error];//如果目标文件已经存在,尝试删除已存在的文件。

    BOOL success = [fileManager copyItemAtPath:decodedPath toPath:destinationPath error:&error];//将解码后的文件拷贝到目标路径。

    NSDictionary *dict = @{@"decodedPath": decodedPath, @"filePath": destinationPath, @"fileName": fileName};// 创建一个包含文件相关信息的字典。

    SingleTon.getInstance.dict = dict;

    if (SingleTon.getInstance.openURLCallback) {         
       SingleTon.getInstance.openURLCallback(SingleTon.getInstance.dict); // 调用回调函数传递文件信息给前端
    }
    return YES;
}
相关推荐
你踩到我法袍了2 天前
深入理解Objective-C中的消息传递机制
objective-c·参数·消息传递·编程实践·嵌套消息
依旧风轻2 天前
objc_object 与 objc_class 是一定要了解的底层结构
ios·objective-c·isa·objc_class·objc_object
Sheffi663 天前
RunLoop Mode 深度剖析:为什么滚动时 Timer 会“失效“?
ios·objective-c
Sheffi664 天前
RunLoop 深度探索:线程为什么不会自动退出
macos·objective-c·cocoa
AirDroid_cn4 天前
iPhone放大镜跟随模式下,画面抖动,如何稳定?
macos·ios·objective-c·cocoa·iphone·ipad
AirDroid_cn4 天前
iPhone 新安装的APP无法调用摄像头,如何重置权限?
macos·ios·objective-c·cocoa·iphone
Sheffi665 天前
ARC 的自动释放机制与 autoreleasepool 深度解析
macos·objective-c·cocoa
笑尘pyrotechnic6 天前
手势识别器内容
ios·objective-c
他们都不看好你,偏偏你最不争气6 天前
【iOS】SDWebImage解析
macos·ios·objective-c·cocoa·sdwebimage
Sheffi666 天前
Objective-C 黑魔法:Method Swizzling 的正确姿势与滥用风险
开发语言·macos·objective-c