如何将原生ios第三方库集成到react原生应用程序中



我需要在react native应用程序中提取tar文件,我找到了一个库(react nativearchiver(,但它只支持android而不支持IOS。

我在github tarkit上找到了这个库,但怎么可能像通过npm安装一样将它添加到节点模块中呢。

他们提到如何解开文件:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* dataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"test.tar.gz"];
NSString* toPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"testDir"];
[DCTar decompressFileAtPath:dataPath toPath:toPath error:nil];

他们提到,安装tarkit的推荐方法是通过CocoaPods包管理器(像大多数库一样(:

pod 'tarkit', '~> 0.1.3'

我发现这个库支持react native,但只支持zip文件react nativezip归档:

在RNZipArchive.m:中

RCT_EXPORT_METHOD(unzip:(NSString *)from
destinationPath:(NSString *)destinationPath
charset:(NSString *)charset
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
self.progress = 0.0;
self.processedFilePath = @"";
[self zipArchiveProgressEvent:0 total:1]; // force 0%
NSError *error = nil;
BOOL success = [SSZipArchive unzipFileAtPath:from toDestination:destinationPath overwrite:YES password:nil error:&error delegate:self];
self.progress = 1.0;
[self zipArchiveProgressEvent:1 total:1]; // force 100%
if (success) {
resolve(destinationPath);
} else {
reject(@"unzip_error", [error localizedDescription], error);
}
}

在tarkit中,我发现了以下函数untarFileAtPath:

+(BOOL)untarFileAtPath:(NSString*)tarFilePath toPath:(NSString*)path error:(NSError**)error
{
NSFileManager *manager = [NSFileManager defaultManager];
if([manager fileExistsAtPath:tarFilePath]) {
NSDictionary *attributes = [manager attributesOfItemAtPath:tarFilePath error:nil];
unsigned long long size = [attributes[NSFileSize] longLongValue];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:tarFilePath];
return [self untarObject:fileHandle size:size toPath:path error:error];
}
if(error)
*error = [self errorWithDetail:@"tar file not found" code:-2];
return NO;
}

如何在tarkit中导出untarFileAtPath,就像在react原生zip归档中解压缩一样。

只需为iOS实现一个自定义的本机模块。复制粘贴工作代码并用RCT_EXPORT_METHOD包装它,然后使用解析器返回值。

你需要以某种方式命名模块,这样你就会得到这样的东西:

@implementation UnTar
RCT_EXPORT_MODULE(UnTar);
RCT_EXPORT_METHOD((untarFileAtPath:(NSDictionary *)fields
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
NSString *tarFilePath = [RCTConvert NSString:fields[@"tarFilePath"]];
NSString *destinationPath = [RCTConvert NSString:fields[@"destinationPath"]];
NSFileManager *manager = [NSFileManager defaultManager];
if([manager fileExistsAtPath:tarFilePath]) {
NSDictionary *attributes = [manager attributesOfItemAtPath:tarFilePath error:nil];
unsigned long long size = [attributes[NSFileSize] longLongValue];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:tarFilePath];
resolve([self untarObject:fileHandle size:size toPath:path error:error]);
}
if(error)
*error = [self errorWithDetail:@"tar file not found" code:-2];
reject();
}
@end

在React Native中:

import { NativeModules } from 'react-native'
NativeModules.UnTar.untarFileAtPath({ tarFilePath, destinationPath })

我没有运行代码,所以用它作为指南(我稍后会尝试100%使用代码,但我需要将所有库放在一个地方。

这是我的解决方案:首先我通过在podfile中添加pod 'tarkit', '~> 0.1.3'来安装pod库,然后运行cd ios&吊舱安装。

extracttar.h:中的代码

#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTLog.h>
#import <React/RCTEventEmitter.h>
#import "tarkit/DCTar.h"
NS_ASSUME_NONNULL_BEGIN
@interface extracttar : NSObject <RCTBridgeModule>
@end
NS_ASSUME_NONNULL_END

extracttar.m:中的代码

#import "extracttar.h"
#import <React/RCTBridgeModule.h>

#if __has_include(<React/RCTEventDispatcher.h>)
#import <React/RCTEventDispatcher.h>
#else
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#endif
@implementation extracttar
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
{
RCTLogInfo(@"Pretending to create an event %@ at %@", name, location);
}
RCT_EXPORT_METHOD(getsomestring:(RCTResponseSenderBlock)callback)
{
NSString *string = @"hello from xcode react native";
callback(@[string]);
}
RCT_EXPORT_METHOD(untar:(NSString *)from
destinationPath:(NSString *)destinationPath
charset:(NSString *)charset
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
NSError *error = nil;
BOOL success =[DCTar decompressFileAtPath:from toPath:destinationPath error:nil];
//BOOL success =[SSZipArchive unzipFileAtPath:from toDestination:destinationPath overwrite:YES password:nil error:&error delegate:self];
if (success) {
resolve(destinationPath);
} else {
reject(@"unzip_error", [error localizedDescription], error);
}
}
@end

react native中的代码:

import {NativeModules, Alert} from "react-native";
var untarIOS = NativeModules.extracttar;
.....
untarIOS.untar(`${RNFS.DocumentDirectoryPath}/5.tar`,RNFS.DocumentDirectoryPath,"")

最新更新