VLCActivityViewControllerVendor.m 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*****************************************************************************
  2. * VLCActivityViewControllerVendor.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2017 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <nitz.carola # gmail.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCActivityViewControllerVendor.h"
  13. #import "VLCOpeninActivity.h"
  14. #import <Photos/PHPhotoLibrary.h>
  15. @implementation VLCActivityViewControllerVendor
  16. + (UIActivityViewController *)activityViewControllerForFiles:(NSArray *)files
  17. presentingButton:(UIButton *)button
  18. presentingViewController:(UIViewController *)viewController
  19. completionHandler:(void (^)(BOOL))completionHandler
  20. {
  21. if (![files count]) {
  22. [viewController vlc_showAlertWithTitle:NSLocalizedString(@"SHARING_ERROR_NO_FILES", nil)
  23. message:nil
  24. buttonTitle:NSLocalizedString(@"BUTTON_OK", nil)];
  25. return nil;
  26. }
  27. VLCOpenInActivity *openInActivity = [[VLCOpenInActivity alloc] init];
  28. openInActivity.presentingViewController = viewController;
  29. openInActivity.presentingButton = button;
  30. UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:files applicationActivities:@[openInActivity]];
  31. NSMutableArray *excludedActivities = [@[
  32. UIActivityTypePrint,
  33. UIActivityTypeAssignToContact,
  34. UIActivityTypeAddToReadingList,
  35. UIActivityTypeOpenInIBooks
  36. ] mutableCopy];
  37. if (@available(iOS 11_0, *)) {
  38. [excludedActivities addObject:UIActivityTypeMarkupAsPDF];
  39. }
  40. controller.excludedActivityTypes = excludedActivities;
  41. controller.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
  42. APLog(@"UIActivityViewController finished with activity type: %@, completed: %i", activityType, completed);
  43. // 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.
  44. // 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.
  45. if (completed && [activityType isEqualToString:UIActivityTypeSaveToCameraRoll]) {
  46. [viewController vlc_showAlertWithTitle:NSLocalizedString(@"SHARING_SUCCESS_CAMERA_ROLL", nil)
  47. message:nil
  48. buttonTitle:NSLocalizedString(@"BUTTON_OK", nil)];
  49. }
  50. completionHandler(completed);
  51. };
  52. return controller;
  53. }
  54. @end