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;
}
相关推荐
June bug17 小时前
【领域知识】广告全链路测试
macos·objective-c·cocoa
pop_xiaoli1 天前
effective-Objective-C 第一章阅读笔记
开发语言·笔记·ios·objective-c·cocoa·xcode
2601_949146532 天前
APP语音通知接口集成实战:移动端应用接入语音提醒API的开发手册
macos·objective-c·cocoa
小鹿软件办公2 天前
Apple 发布 macOS 11、watchOS 10 和 watchOS 9 更新
macos·objective-c·cocoa
pop_xiaoli4 天前
OC-实现下载单例类
ios·objective-c·cocoa·xcode
denggun123454 天前
Material 和 Cupertino
macos·objective-c·cocoa
仙剑魔尊重楼6 天前
iMazing 3.1.3官方中文版新功能介绍
macos·objective-c·cocoa
linweidong6 天前
屏幕尺寸的万花筒:如何在 iOS 碎片化生态中以不变应万变?
macos·ios·移动开发·objective-c·cocoa·ios面试·ios面经
2601_949146536 天前
Objective-C手机验证码短信接口调用流程:创建请求对象并设置报文体
智能手机·objective-c·cocoa
2601_949146536 天前
Objective-C短信验证码接口开发:封装一个基础的网络请求工具方法
macos·objective-c·cocoa