설명 없음

RNFetchBlob.m 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. //
  2. // RNFetchBlob.m
  3. //
  4. // Created by wkh237 on 2016/4/28.
  5. //
  6. #import "RNFetchBlob.h"
  7. #import "RNFetchBlobFS.h"
  8. #import "RNFetchBlobNetwork.h"
  9. #import "RNFetchBlobConst.h"
  10. #import "RNFetchBlobReqBuilder.h"
  11. #import "RNFetchBlobProgress.h"
  12. __strong RCTBridge * bridgeRef;
  13. dispatch_queue_t commonTaskQueue;
  14. dispatch_queue_t fsQueue;
  15. ////////////////////////////////////////
  16. //
  17. // Exported native methods
  18. //
  19. ////////////////////////////////////////
  20. #pragma mark RNFetchBlob exported methods
  21. @implementation RNFetchBlob
  22. @synthesize filePathPrefix;
  23. @synthesize documentController;
  24. @synthesize bridge = _bridge;
  25. - (dispatch_queue_t) methodQueue {
  26. if(commonTaskQueue == nil)
  27. commonTaskQueue = dispatch_queue_create("RNFetchBlob.queue", DISPATCH_QUEUE_SERIAL);
  28. return commonTaskQueue;
  29. }
  30. + (RCTBridge *)getRCTBridge
  31. {
  32. RCTRootView * rootView = [[UIApplication sharedApplication] keyWindow].rootViewController.view;
  33. return rootView.bridge;
  34. }
  35. RCT_EXPORT_MODULE();
  36. - (id) init {
  37. self = [super init];
  38. self.filePathPrefix = FILE_PREFIX;
  39. if(commonTaskQueue == nil)
  40. commonTaskQueue = dispatch_queue_create("RNFetchBlob.queue", DISPATCH_QUEUE_SERIAL);
  41. if(fsQueue == nil)
  42. fsQueue = dispatch_queue_create("RNFetchBlob.fs.queue", DISPATCH_QUEUE_SERIAL);
  43. BOOL isDir;
  44. // if temp folder not exists, create one
  45. if(![[NSFileManager defaultManager] fileExistsAtPath: [RNFetchBlobFS getTempPath] isDirectory:&isDir]) {
  46. [[NSFileManager defaultManager] createDirectoryAtPath:[RNFetchBlobFS getTempPath] withIntermediateDirectories:YES attributes:nil error:NULL];
  47. }
  48. bridgeRef = _bridge;
  49. [RNFetchBlobNetwork emitExpiredTasks];
  50. return self;
  51. }
  52. - (NSDictionary *)constantsToExport
  53. {
  54. return @{
  55. @"MainBundleDir" : [RNFetchBlobFS getMainBundleDir],
  56. @"DocumentDir": [RNFetchBlobFS getDocumentDir],
  57. @"CacheDir" : [RNFetchBlobFS getCacheDir]
  58. };
  59. }
  60. // Fetch blob data request
  61. RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
  62. taskId:(NSString *)taskId
  63. method:(NSString *)method
  64. url:(NSString *)url
  65. headers:(NSDictionary *)headers
  66. form:(NSArray *)form
  67. callback:(RCTResponseSenderBlock)callback)
  68. {
  69. [RNFetchBlobReqBuilder buildMultipartRequest:options
  70. taskId:taskId
  71. method:method
  72. url:url
  73. headers:headers
  74. form:form
  75. onComplete:^(__weak NSURLRequest *req, long bodyLength)
  76. {
  77. // something went wrong when building the request body
  78. if(req == nil)
  79. {
  80. callback(@[@"RNFetchBlob.fetchBlobForm failed to create request body"]);
  81. }
  82. // send HTTP request
  83. else
  84. {
  85. RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
  86. [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
  87. }
  88. }];
  89. }
  90. // Fetch blob data request
  91. RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
  92. taskId:(NSString *)taskId
  93. method:(NSString *)method
  94. url:(NSString *)url
  95. headers:(NSDictionary *)headers
  96. body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
  97. {
  98. [RNFetchBlobReqBuilder buildOctetRequest:options
  99. taskId:taskId
  100. method:method
  101. url:url
  102. headers:headers
  103. body:body
  104. onComplete:^(NSURLRequest *req, long bodyLength)
  105. {
  106. // something went wrong when building the request body
  107. if(req == nil)
  108. {
  109. callback(@[@"RNFetchBlob.fetchBlob failed to create request body"]);
  110. }
  111. // send HTTP request
  112. else
  113. {
  114. __block RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
  115. [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
  116. }
  117. }];
  118. }
  119. #pragma mark - fs.createFile
  120. RCT_EXPORT_METHOD(createFile:(NSString *)path data:(NSString *)data encoding:(NSString *)encoding callback:(RCTResponseSenderBlock)callback) {
  121. NSFileManager * fm = [NSFileManager defaultManager];
  122. NSData * fileContent = nil;
  123. if([[encoding lowercaseString] isEqualToString:@"utf8"]) {
  124. fileContent = [[NSData alloc] initWithData:[data dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
  125. }
  126. else if([[encoding lowercaseString] isEqualToString:@"base64"]) {
  127. fileContent = [[NSData alloc] initWithBase64EncodedData:data options:0];
  128. }
  129. else if([[encoding lowercaseString] isEqualToString:@"uri"]) {
  130. NSString * orgPath = [data stringByReplacingOccurrencesOfString:FILE_PREFIX withString:@""];
  131. fileContent = [[NSData alloc] initWithContentsOfFile:orgPath];
  132. }
  133. else {
  134. fileContent = [[NSData alloc] initWithData:[data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
  135. }
  136. BOOL success = [fm createFileAtPath:path contents:fileContent attributes:NULL];
  137. if(success == YES)
  138. callback(@[[NSNull null]]);
  139. else
  140. callback(@[[NSString stringWithFormat:@"failed to create new file at path %@ please ensure the folder exists"]]);
  141. }
  142. #pragma mark - fs.createFileASCII
  143. // method for create file with ASCII content
  144. RCT_EXPORT_METHOD(createFileASCII:(NSString *)path data:(NSArray *)dataArray callback:(RCTResponseSenderBlock)callback) {
  145. NSFileManager * fm = [NSFileManager defaultManager];
  146. NSMutableData * fileContent = [NSMutableData alloc];
  147. // prevent stack overflow, alloc on heap
  148. char * bytes = (char*) malloc([dataArray count]);
  149. for(int i = 0; i < dataArray.count; i++) {
  150. bytes[i] = [[dataArray objectAtIndex:i] charValue];
  151. }
  152. [fileContent appendBytes:bytes length:dataArray.count];
  153. BOOL success = [fm createFileAtPath:path contents:fileContent attributes:NULL];
  154. free(bytes);
  155. if(success == YES)
  156. callback(@[[NSNull null]]);
  157. else
  158. callback(@[[NSString stringWithFormat:@"failed to create new file at path %@ please ensure the folder exists"]]);
  159. }
  160. #pragma mark - fs.pathForAppGroup
  161. RCT_EXPORT_METHOD(pathForAppGroup:(NSString *)groupName
  162. resolver:(RCTPromiseResolveBlock)resolve
  163. rejecter:(RCTPromiseRejectBlock)reject)
  164. {
  165. NSString * path = [RNFetchBlobFS getPathForAppGroup:groupName];
  166. if(path) {
  167. resolve(path);
  168. } else {
  169. reject(@"RNFetchBlob file not found", @"could not find path for app group", nil);
  170. }
  171. }
  172. #pragma mark - fs.exists
  173. RCT_EXPORT_METHOD(exists:(NSString *)path callback:(RCTResponseSenderBlock)callback) {
  174. [RNFetchBlobFS exists:path callback:callback];
  175. }
  176. #pragma mark - fs.writeFile
  177. RCT_EXPORT_METHOD(writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  178. {
  179. [RNFetchBlobFS writeFile:path encoding:[NSString stringWithString:encoding] data:data append:append resolver:resolve rejecter:reject];
  180. }
  181. #pragma mark - fs.writeArray
  182. RCT_EXPORT_METHOD(writeFileArray:(NSString *)path data:(NSArray *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  183. {
  184. [RNFetchBlobFS writeFileArray:path data:data append:append resolver:resolve rejecter:reject];
  185. }
  186. #pragma mark - fs.writeStream
  187. RCT_EXPORT_METHOD(writeStream:(NSString *)path withEncoding:(NSString *)encoding appendData:(BOOL)append callback:(RCTResponseSenderBlock)callback)
  188. {
  189. RNFetchBlobFS * fileStream = [[RNFetchBlobFS alloc] initWithBridgeRef:self.bridge];
  190. NSFileManager * fm = [NSFileManager defaultManager];
  191. BOOL isDir = nil;
  192. BOOL exist = [fm fileExistsAtPath:path isDirectory:&isDir];
  193. if( exist == NO || isDir == YES) {
  194. callback(@[[NSString stringWithFormat:@"target path `%@` may not exists or it's a folder", path]]);
  195. return;
  196. }
  197. NSString * streamId = [fileStream openWithPath:path encode:encoding appendData:append];
  198. callback(@[[NSNull null], streamId]);
  199. }
  200. #pragma mark - fs.writeArrayChunk
  201. RCT_EXPORT_METHOD(writeArrayChunk:(NSString *)streamId withArray:(NSArray *)dataArray callback:(RCTResponseSenderBlock) callback)
  202. {
  203. RNFetchBlobFS *fs = [[RNFetchBlobFS getFileStreams] valueForKey:streamId];
  204. char * bytes = (char *) malloc([dataArray count]);
  205. for(int i = 0; i < dataArray.count; i++) {
  206. bytes[i] = [[dataArray objectAtIndex:i] charValue];
  207. }
  208. NSMutableData * data = [NSMutableData alloc];
  209. [data appendBytes:bytes length:dataArray.count];
  210. [fs write:data];
  211. free(bytes);
  212. callback(@[[NSNull null]]);
  213. }
  214. #pragma mark - fs.writeChunk
  215. RCT_EXPORT_METHOD(writeChunk:(NSString *)streamId withData:(NSString *)data callback:(RCTResponseSenderBlock) callback)
  216. {
  217. RNFetchBlobFS *fs = [[RNFetchBlobFS getFileStreams] valueForKey:streamId];
  218. [fs writeEncodeChunk:data];
  219. callback(@[[NSNull null]]);
  220. }
  221. #pragma mark - fs.closeStream
  222. RCT_EXPORT_METHOD(closeStream:(NSString *)streamId callback:(RCTResponseSenderBlock) callback)
  223. {
  224. RNFetchBlobFS *fs = [[RNFetchBlobFS getFileStreams] valueForKey:streamId];
  225. [fs closeOutStream];
  226. callback(@[[NSNull null], @YES]);
  227. }
  228. #pragma mark - unlink
  229. RCT_EXPORT_METHOD(unlink:(NSString *)path callback:(RCTResponseSenderBlock) callback)
  230. {
  231. NSError * error = nil;
  232. NSString * tmpPath = nil;
  233. [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
  234. if(error == nil || [[NSFileManager defaultManager] fileExistsAtPath:path] == NO)
  235. callback(@[[NSNull null]]);
  236. else
  237. callback(@[[NSString stringWithFormat:@"failed to unlink file or path at %@", path]]);
  238. }
  239. #pragma mark - fs.removeSession
  240. RCT_EXPORT_METHOD(removeSession:(NSArray *)paths callback:(RCTResponseSenderBlock) callback)
  241. {
  242. NSError * error = nil;
  243. NSString * tmpPath = nil;
  244. for(NSString * path in paths) {
  245. [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
  246. if(error != nil) {
  247. callback(@[[NSString stringWithFormat:@"failed to remove session path at %@", path]]);
  248. return;
  249. }
  250. }
  251. callback(@[[NSNull null]]);
  252. }
  253. #pragma mark - fs.ls
  254. RCT_EXPORT_METHOD(ls:(NSString *)path callback:(RCTResponseSenderBlock) callback)
  255. {
  256. NSFileManager* fm = [NSFileManager defaultManager];
  257. BOOL exist = nil;
  258. BOOL isDir = nil;
  259. exist = [fm fileExistsAtPath:path isDirectory:&isDir];
  260. if(exist == NO || isDir == NO) {
  261. callback(@[[NSString stringWithFormat:@"failed to list path `%@` for it is not exist or it is not a folder", path]]);
  262. return ;
  263. }
  264. NSError * error = nil;
  265. NSArray * result = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
  266. if(error == nil)
  267. callback(@[[NSNull null], result == nil ? [NSNull null] :result ]);
  268. else
  269. callback(@[[error localizedDescription], [NSNull null]]);
  270. }
  271. #pragma mark - fs.stat
  272. RCT_EXPORT_METHOD(stat:(NSString *)target callback:(RCTResponseSenderBlock) callback)
  273. {
  274. [RNFetchBlobFS getPathFromUri:target completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
  275. __block NSMutableArray * result;
  276. if(path != nil)
  277. {
  278. NSFileManager* fm = [NSFileManager defaultManager];
  279. BOOL exist = nil;
  280. BOOL isDir = nil;
  281. NSError * error = nil;
  282. exist = [fm fileExistsAtPath:path isDirectory:&isDir];
  283. if(exist == NO) {
  284. callback(@[[NSString stringWithFormat:@"failed to stat path `%@` for it is not exist or it is not exist", path]]);
  285. return ;
  286. }
  287. result = [RNFetchBlobFS stat:path error:&error];
  288. if(error == nil)
  289. callback(@[[NSNull null], result]);
  290. else
  291. callback(@[[error localizedDescription], [NSNull null]]);
  292. }
  293. else if(asset != nil)
  294. {
  295. __block NSNumber * size = [NSNumber numberWithLong:[asset size]];
  296. result = [asset metadata];
  297. [result setValue:size forKey:@"size"];
  298. callback(@[[NSNull null], result]);
  299. }
  300. else
  301. {
  302. callback(@[@"failed to stat path, could not resolve URI", [NSNull null]]);
  303. }
  304. }];
  305. }
  306. #pragma mark - fs.lstat
  307. RCT_EXPORT_METHOD(lstat:(NSString *)path callback:(RCTResponseSenderBlock) callback)
  308. {
  309. NSFileManager* fm = [NSFileManager defaultManager];
  310. BOOL exist = nil;
  311. BOOL isDir = nil;
  312. path = [RNFetchBlobFS getPathOfAsset:path];
  313. exist = [fm fileExistsAtPath:path isDirectory:&isDir];
  314. if(exist == NO) {
  315. callback(@[[NSString stringWithFormat:@"failed to list path `%@` for it is not exist or it is not exist", path]]);
  316. return ;
  317. }
  318. NSError * error = nil;
  319. NSArray * files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
  320. NSMutableArray * res = [[NSMutableArray alloc] init];
  321. if(isDir == YES) {
  322. for(NSString * p in files) {
  323. NSString * filePath = [NSString stringWithFormat:@"%@/%@", path, p];
  324. [res addObject:[RNFetchBlobFS stat:filePath error:&error]];
  325. }
  326. }
  327. else {
  328. [res addObject:[RNFetchBlobFS stat:path error:&error]];
  329. }
  330. if(error == nil)
  331. callback(@[[NSNull null], res == nil ? [NSNull null] :res ]);
  332. else
  333. callback(@[[error localizedDescription], [NSNull null]]);
  334. }
  335. #pragma mark - fs.cp
  336. RCT_EXPORT_METHOD(cp:(NSString*)src toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback)
  337. {
  338. // path = [RNFetchBlobFS getPathOfAsset:path];
  339. [RNFetchBlobFS getPathFromUri:src completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
  340. NSError * error = nil;
  341. if(path == nil)
  342. {
  343. [RNFetchBlobFS writeAssetToPath:asset dest:dest];
  344. callback(@[[NSNull null], @YES]);
  345. }
  346. else
  347. {
  348. BOOL result = [[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:path] toURL:[NSURL fileURLWithPath:dest] error:&error];
  349. if(error == nil)
  350. callback(@[[NSNull null], @YES]);
  351. else
  352. callback(@[[error localizedDescription], @NO]);
  353. }
  354. }];
  355. }
  356. #pragma mark - fs.mv
  357. RCT_EXPORT_METHOD(mv:(NSString *)path toPath:(NSString *)dest callback:(RCTResponseSenderBlock) callback)
  358. {
  359. NSError * error = nil;
  360. BOOL result = [[NSFileManager defaultManager] moveItemAtURL:[NSURL fileURLWithPath:path] toURL:[NSURL fileURLWithPath:dest] error:&error];
  361. if(error == nil)
  362. callback(@[[NSNull null], @YES]);
  363. else
  364. callback(@[[error localizedDescription], @NO]);
  365. }
  366. #pragma mark - fs.mkdir
  367. RCT_EXPORT_METHOD(mkdir:(NSString *)path callback:(RCTResponseSenderBlock) callback)
  368. {
  369. if([[NSFileManager defaultManager] fileExistsAtPath:path]) {
  370. callback(@[@"mkdir failed, folder already exists"]);
  371. return;
  372. }
  373. else
  374. [RNFetchBlobFS mkdir:path];
  375. callback(@[[NSNull null]]);
  376. }
  377. #pragma mark - fs.readFile
  378. RCT_EXPORT_METHOD(readFile:(NSString *)path
  379. encoding:(NSString *)encoding
  380. resolver:(RCTPromiseResolveBlock)resolve
  381. rejecter:(RCTPromiseRejectBlock)reject)
  382. {
  383. [RNFetchBlobFS readFile:path encoding:encoding onComplete:^(id content, NSString * err) {
  384. if(err != nil)
  385. {
  386. reject(@"RNFetchBlob failed to read file", err, nil);
  387. return;
  388. }
  389. if(encoding == @"ascii")
  390. {
  391. resolve((NSMutableArray *)content);
  392. }
  393. else
  394. {
  395. resolve((NSString *)content);
  396. }
  397. }];
  398. }
  399. #pragma mark - fs.readStream
  400. RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding bufferSize:(int)bufferSize tick:(int)tick streamId:(NSString *)streamId)
  401. {
  402. if(bufferSize == nil) {
  403. if([[encoding lowercaseString] isEqualToString:@"base64"])
  404. bufferSize = 4095;
  405. else
  406. bufferSize = 4096;
  407. }
  408. dispatch_async(fsQueue, ^{
  409. [RNFetchBlobFS readStream:path encoding:encoding bufferSize:bufferSize tick:tick streamId:streamId bridgeRef:_bridge];
  410. });
  411. }
  412. #pragma mark - fs.getEnvionmentDirs
  413. RCT_EXPORT_METHOD(getEnvironmentDirs:(RCTResponseSenderBlock) callback)
  414. {
  415. callback(@[
  416. [RNFetchBlobFS getDocumentDir],
  417. [RNFetchBlobFS getCacheDir],
  418. ]);
  419. }
  420. #pragma mark - net.cancelRequest
  421. RCT_EXPORT_METHOD(cancelRequest:(NSString *)taskId callback:(RCTResponseSenderBlock)callback) {
  422. [RNFetchBlobNetwork cancelRequest:taskId];
  423. callback(@[[NSNull null], taskId]);
  424. }
  425. #pragma mark - net.enableProgressReport
  426. RCT_EXPORT_METHOD(enableProgressReport:(NSString *)taskId interval:(nonnull NSNumber*)interval count:(nonnull NSNumber*)count)
  427. {
  428. RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Download interval:interval count:count];
  429. [RNFetchBlobNetwork enableProgressReport:taskId config:cfg];
  430. }
  431. #pragma mark - net.enableUploadProgressReport
  432. RCT_EXPORT_METHOD(enableUploadProgressReport:(NSString *)taskId interval:(nonnull NSNumber*)interval count:(nonnull NSNumber*)count)
  433. {
  434. RNFetchBlobProgress * cfg = [[RNFetchBlobProgress alloc] initWithType:Upload interval:interval count:count];
  435. [RNFetchBlobNetwork enableUploadProgress:taskId config:cfg];
  436. }
  437. #pragma mark - fs.slice
  438. RCT_EXPORT_METHOD(slice:(NSString *)src dest:(NSString *)dest start:(nonnull NSNumber *)start end:(nonnull NSNumber *)end resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  439. {
  440. [RNFetchBlobFS slice:src dest:dest start:start end:end encode:@"" resolver:resolve rejecter:reject];
  441. }
  442. RCT_EXPORT_METHOD(previewDocument:(NSString*)uri scheme:(NSString *)scheme resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  443. {
  444. NSString * utf8uri = [uri stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  445. NSURL * url = [[NSURL alloc] initWithString:utf8uri];
  446. // NSURL * url = [[NSURL alloc] initWithString:uri];
  447. documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
  448. UIViewController *rootCtrl = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
  449. documentController.delegate = self;
  450. if(scheme == nil || [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:scheme]]) {
  451. dispatch_sync(dispatch_get_main_queue(), ^{
  452. [documentController presentOptionsMenuFromRect:rootCtrl.view.bounds inView:rootCtrl.view animated:YES];
  453. });
  454. resolve(@[[NSNull null]]);
  455. } else {
  456. reject(@"RNFetchBlob could not open document", @"scheme is not supported", nil);
  457. }
  458. }
  459. # pragma mark - open file with UIDocumentInteractionController and delegate
  460. RCT_EXPORT_METHOD(openDocument:(NSString*)uri scheme:(NSString *)scheme resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  461. {
  462. NSString * utf8uri = [uri stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  463. NSURL * url = [[NSURL alloc] initWithString:utf8uri];
  464. // NSURL * url = [[NSURL alloc] initWithString:uri];
  465. documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
  466. documentController.delegate = self;
  467. if(scheme == nil || [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:scheme]]) {
  468. dispatch_sync(dispatch_get_main_queue(), ^{
  469. [documentController presentPreviewAnimated:YES];
  470. });
  471. resolve(@[[NSNull null]]);
  472. } else {
  473. reject(@"RNFetchBlob could not open document", @"scheme is not supported", nil);
  474. }
  475. }
  476. # pragma mark - exclude from backup key
  477. RCT_EXPORT_METHOD(excludeFromBackupKey:(NSString *)url resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  478. {
  479. NSError *error = nil;
  480. [ [NSURL URLWithString:url] setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
  481. if(!error)
  482. {
  483. resolve(@[[NSNull null]]);
  484. } else {
  485. reject(@"RNFetchBlob could not open document", [error description], nil);
  486. }
  487. }
  488. RCT_EXPORT_METHOD(df:(RCTResponseSenderBlock)callback)
  489. {
  490. [RNFetchBlobFS df:callback];
  491. }
  492. - (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller
  493. {
  494. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  495. return window.rootViewController;
  496. }
  497. # pragma mark - getCookies
  498. RCT_EXPORT_METHOD(getCookies:(NSString *)url resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
  499. {
  500. resolve([RNFetchBlobNetwork getCookies:url]);
  501. }
  502. # pragma mark - check expired network events
  503. RCT_EXPORT_METHOD(emitExpiredEvent:(RCTResponseSenderBlock)callback)
  504. {
  505. [RNFetchBlobNetwork emitExpiredTasks];
  506. }
  507. @end