VLCDocumentPickerController.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*****************************************************************************
  2. * VLCDocumentPickerController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tamas Timar <ttimar.vlc # gmail.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCDocumentPickerController.h"
  13. #import <MobileCoreServices/MobileCoreServices.h>
  14. #import "VLCMediaFileDiscoverer.h"
  15. #import "VLCLibraryViewController.h"
  16. @interface VLCDocumentPickerController () <UIDocumentMenuDelegate, UIDocumentPickerDelegate>
  17. @end
  18. @implementation VLCDocumentPickerController
  19. #pragma mark - Public Methods
  20. - (void)showDocumentMenuViewController:(id)sender
  21. {
  22. UIDocumentMenuViewController *importMenu = [[UIDocumentMenuViewController alloc] initWithDocumentTypes:@[(id)kUTTypeAudiovisualContent] inMode:UIDocumentPickerModeImport];
  23. importMenu.delegate = self;
  24. UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
  25. UIPopoverPresentationController *popoverPres = importMenu.popoverPresentationController;
  26. if (popoverPres) { // not-nil on iPad
  27. UIView *sourceView = nil;
  28. if ([sender isKindOfClass:[UIView class]]) {
  29. sourceView = sender;
  30. } else {
  31. sourceView = rootVC.view;
  32. }
  33. popoverPres.sourceView = sourceView;
  34. popoverPres.sourceRect = sourceView.bounds;
  35. popoverPres.permittedArrowDirections = UIPopoverArrowDirectionLeft;
  36. }
  37. [rootVC presentViewController:importMenu animated:YES completion:nil];
  38. }
  39. #pragma mark - UIDocumentMenuDelegate
  40. - (void)documentMenu:(UIDocumentMenuViewController *)documentMenu didPickDocumentPicker:(UIDocumentPickerViewController *)documentPicker
  41. {
  42. documentPicker.delegate = self;
  43. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:documentPicker animated:YES completion:nil];
  44. }
  45. #pragma mark - UIDocumentPickerDelegate
  46. - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url
  47. {
  48. NSFileManager *fileManager = [NSFileManager defaultManager];
  49. NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  50. NSString *filePath = [documentsPath stringByAppendingPathComponent:[url lastPathComponent]];
  51. NSError *error = nil;
  52. [fileManager moveItemAtPath:[url path] toPath:filePath error:&error];
  53. if (!error) {
  54. [[VLCMediaFileDiscoverer sharedInstance] updateMediaList];
  55. } else {
  56. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"GDRIVE_ERROR_DOWNLOADING_FILE_TITLE", nil) message:error.description preferredStyle:UIAlertControllerStyleAlert];
  57. UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
  58. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  59. [rootVC dismissViewControllerAnimated:true completion:nil];
  60. }];
  61. [alert addAction:okAction];
  62. [rootVC presentViewController:alert animated:true completion:nil];
  63. }
  64. }
  65. @end