alinz / react-native-share-extension

react-native as an engine to drive share extension
MIT License
762 stars 396 forks source link

Support for all file types on iOS #125

Open radreamer opened 5 years ago

radreamer commented 5 years ago

Hey, thanks for the great library

I need to support sharing for all file types and as a proof of concept, I modified ReactNativeShareExtension.m in the next way:

  1. Define the identifier for all file types #define CONTENT_IDENTIFIER @"public.content"
  2. Modify extractDataFromContext:
- (void)extractDataFromContext:(NSExtensionContext *)context withCallback:(void(^)(NSString *value, NSString* contentType, NSException *exception))callback {

    @try {
        NSExtensionItem *item = [context.inputItems firstObject];
        NSArray *attachments = item.attachments;

        __block NSItemProvider *contentProvider = nil;

        [attachments enumerateObjectsUsingBlock:^(NSItemProvider *provider, NSUInteger idx, BOOL *stop) {

            if ([provider hasItemConformingToTypeIdentifier:CONTENT_IDENTIFIER]) {
                contentProvider = provider;
                *stop = YES;
            }
        }];

        if (contentProvider) {
            [contentProvider loadItemForTypeIdentifier:CONTENT_IDENTIFIER options:nil completionHandler:^(id<NSSecureCoding> item, NSError *error) {
                NSURL *url = (NSURL *)item;

                if(callback) {
                    callback([url absoluteString], @"text/plain", nil);
                }
            }];
        } else {
            if(callback) {
                callback(nil, nil, [NSException exceptionWithName:@"Error" reason:@"couldn't find provider" userInfo:nil]);
            }
        }
    }
    @catch (NSException *exception) {
        if(callback) {
            callback(nil, nil, exception);
        }
    }
}

After that looks like everything works well, but I am afraid that I can lose sight of some iOS restrictions as I`m not an Objective-C developer. Can you help with this and confirm or deny my thoughts?

isaachinman commented 5 years ago

@tallen11 Perhaps you might be able to help?

ajith-ab commented 4 years ago

use react-native-file-share-intent