简体   繁体   中英

How to get all the pdf files avalable in interanal storage of iphone in react native using native module

我想知道如何使用目标 c 访问本机模块中 iphone 内部存储中存在的 .pdf 列表

You can implement simple native module in your xcode project to return list of files from the document directory of your app to react native eg:

FileList.h

#import <React/RCTBridgeModule.h>

@interface FileList : NSObject <RCTBridgeModule>
@end

FileList.m

#import "FileList.h"

@implementation FileList

RCT_EXPORT_MODULE(FileList)

RCT_EXPORT_METHOD(get:(NSString *)extension callback:(RCTResponseSenderBlock)callback) {
    NSString *documentsDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSArray *files = [NSFileManager.defaultManager contentsOfDirectoryAtPath:documentsDir error:nil];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self ENDSWITH %@", extension];
    NSArray *filtered = [files filteredArrayUsingPredicate:predicate];
    callback(@[filtered]);
}
@end

How to use:

import {
    ...
    NativeModules,
} from 'react-native';

...

NativeModules.FileList.get('pdf', list => {
    print(list); // Prints: file1.pdf, file2.pdf etc.
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM