1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/app-mobile/ios/ShareExtension/Source/RNShareExtension/ShareExtension.m

68 lines
1.9 KiB
Mathematica
Raw Normal View History

2021-04-24 10:22:11 +02:00
//
// ShareExtension.m
// Joplin
//
// Created by Duncan Cunningham on 2/6/21.
// Copyright © 2021 joplinapp.org. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import "ShareData.h"
#import "ShareExtensionConstants.h"
@interface ShareExtension : NSObject <RCTBridgeModule>
@property (nonatomic, strong) ShareData* shareData;
@end
@implementation ShareExtension
RCT_EXPORT_MODULE();
RCT_REMAP_METHOD(data, resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
NSDictionary* dictionary = [self decodeShareDataFromURL:self.shareDataURL];
self.shareData = [[ShareData alloc] initWithDictionary:dictionary];
resolve(dictionary);
}
RCT_EXPORT_METHOD(close) {
if (self.shareData.resources != nil) {
for (NSDictionary* resource in self.shareData.resources) {
NSString* uri = [ShareData resourceURLFromDictionary:resource];
if (uri != nil && [NSFileManager.defaultManager fileExistsAtPath:uri]) {
[NSFileManager.defaultManager removeItemAtPath:uri error:nil];
}
}
}
[NSFileManager.defaultManager removeItemAtPath:[[self shareDataURL] path] error:nil];
}
+ (BOOL)requiresMainQueueSetup {
return YES;
}
- (NSDictionary*)constantsToExport {
return @{@"SHARE_EXTENSION_SHARE_URL": ShareExtensionShareURL};
}
- (NSDictionary*)decodeShareDataFromURL:(NSURL*)url {
NSData* data = [NSData dataWithContentsOfFile:[url path]];
if (data != nil) {
return [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
} else {
return nil;
}
}
- (NSURL*)shareDataURL {
NSFileManager* fileManager = [NSFileManager defaultManager];
NSURL* sharedContainerURL = [fileManager containerURLForSecurityApplicationGroupIdentifier:ShareExtensionGroupIdentifier];
return [sharedContainerURL URLByAppendingPathComponent:ShareExtensionShareDataFilename];
}
@end