Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/ios/CDVCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@
#import <CoreLocation/CLLocationManager.h>
#import <Cordova/CDVPlugin.h>

// Since iOS 14, we can use PHPickerViewController to select images from the photo library
//
// The following condition checks if the iOS 14 SDK is available for XCode
// which is true for XCode 12+. It does not check on runtime, if the device is running iOS 14+.
// For that API_AVAILABLE(ios(14)) is used for methods declarations and @available(iOS 14, *) for the code.
// The condition here makes just sure that the code can compile in XCode
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000

// Import UniformTypeIdentifiers.h for using UTType* things, available since iOS 14,
// which replaces for e.g. kUTTypeImage with UTTypeImage, which must be used in the future instead
// Currently only used for PHPickerViewController
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>

// Import PhotosUI framework for using PHPickerViewController
// PhotosUI is already available since iOS 8, but since we need it currently
// only for the PHPickerViewController, we import it conditionally here
#import <PhotosUI/PhotosUI.h>

// Define a macro to indicate that iOS SDK 14+ is available for PHPicker related things
#define IOS_SDK_14_AVAILABLE 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a publicly visible define in the header file is essentially expanding the public API of the plugin. People will start using this outside of this plugin when they shouldn't, and if we ever want to clean up code by removing this it will be a breaking change.

I know it's more verbose, but it's probably better in the long run to just use the __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 check everywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok understand, I will change this


#endif

enum CDVDestinationType {
DestinationTypeDataUrl = 0,
DestinationTypeFileUri
Expand Down Expand Up @@ -78,12 +101,22 @@ typedef NSUInteger CDVMediaType;
@end

// ======================================================================= //

// Use PHPickerViewController in iOS 14+ to select images from the photo library
// PHPickerViewControllerDelegate is only available since iOS 14
#ifdef IOS_SDK_14_AVAILABLE
@interface CDVCamera : CDVPlugin <UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
UIPopoverControllerDelegate,
CLLocationManagerDelegate,
PHPickerViewControllerDelegate>
{}
#else
@interface CDVCamera : CDVPlugin <UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
UIPopoverControllerDelegate,
CLLocationManagerDelegate>
{}
#endif

@property (strong) CDVCameraPicker* pickerController;
@property (strong) NSMutableDictionary *metadata;
Expand All @@ -104,12 +137,25 @@ typedef NSUInteger CDVMediaType;
- (void)cleanup:(CDVInvokedUrlCommand*)command;
- (void)repositionPopover:(CDVInvokedUrlCommand*)command;

// UIImagePickerControllerDelegate methods
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info;
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo;
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker;

// UINavigationControllerDelegate method
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

// CLLocationManagerDelegate methods
- (void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation;
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;

// PHPickerViewController specific methods (iOS 14+)
#ifdef IOS_SDK_14_AVAILABLE // Always true on XCode12+
- (void)showPHPicker:(NSString*)callbackId withOptions:(CDVPictureOptions*)pictureOptions API_AVAILABLE(ios(14));
- (void)processPHPickerImage:(UIImage*)image assetIdentifier:(NSString*)assetIdentifier callbackId:(NSString*)callbackId options:(CDVPictureOptions*)options API_AVAILABLE(ios(14));
- (void)finalizePHPickerImage:(UIImage*)image metadata:(NSDictionary*)metadata callbackId:(NSString*)callbackId options:(CDVPictureOptions*)options API_AVAILABLE(ios(14));
// PHPickerViewControllerDelegate method
- (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPickerResult *> *)results API_AVAILABLE(ios(14));
#endif

@end
Loading
Loading