VLCActivityViewControllerVendor.m 3.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*****************************************************************************
 * VLCActivityViewControllerVendor.m
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2017 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Carola Nitz <nitz.carola # gmail.com>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCActivityViewControllerVendor.h"
#import "VLCOpeninActivity.h"
15
#import <Photos/PHPhotoLibrary.h>
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

@implementation VLCActivityViewControllerVendor

+ (UIActivityViewController *)activityViewControllerForFiles:(NSArray *)files presentingButton:(UIBarButtonItem *)button presentingViewController:(UIViewController *)viewController;
{
    if (![files count]) {
        [viewController vlc_showAlertWithTitle:NSLocalizedString(@"SHARING_ERROR_NO_FILES", nil)
                             message:nil
                         buttonTitle:NSLocalizedString(@"BUTTON_OK", nil)];
        return nil;
    }

    VLCOpenInActivity *openInActivity = [[VLCOpenInActivity alloc] init];
    openInActivity.presentingViewController = viewController;
    openInActivity.presentingBarButtonItem = button;

    UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:files applicationActivities:@[openInActivity]];

34 35 36 37 38 39 40 41 42 43 44
    NSMutableArray *excludedActivities = [@[
                                            UIActivityTypePrint,
                                            UIActivityTypeAssignToContact,
                                            UIActivityTypeAddToReadingList,
                                            UIActivityTypeOpenInIBooks
                                            ] mutableCopy];

    if (@available(iOS 11_0, *)) {
        [excludedActivities addObject:UIActivityTypeMarkupAsPDF];
    }
    controller.excludedActivityTypes = excludedActivities;
45
    controller.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
46 47 48 49 50
        APLog(@"UIActivityViewController finished with activity type: %@, completed: %i", activityType, completed);

        // Provide feedback. This could cause a false positive if the user chose "Don't Allow" in the permissions dialog, and UIActivityViewController does not inform us of that, so check the authorization status.

        // By the time this is called, the user has not had time to choose whether to allow access to the Photos library, so only display the message if we are truly sure we got authorization. The first time the user saves to the camera roll he won't see the confirmation because of this timing issue. This is better than showing a success message when the user had denied access. A timing workaround could be developed if needed through UIApplicationDidBecomeActiveNotification (to know when the security alert view was dismissed) or through other ALAssets APIs.
51
        if (completed && [activityType isEqualToString:UIActivityTypeSaveToCameraRoll]) {
52 53 54 55 56 57 58 59
            [viewController vlc_showAlertWithTitle:NSLocalizedString(@"SHARING_SUCCESS_CAMERA_ROLL", nil)
                                           message:nil
                                       buttonTitle:NSLocalizedString(@"BUTTON_OK", nil)];
        }
    };
    return controller;
}
@end