-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(ios): use PHPickerViewController for iOS 14+
#937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e087231
1ddec50
8836975
3db5ea5
7c4f856
edc2832
59aeca9
8a3888f
e381490
fc296b6
8b56311
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok understand, I will change this |
||
|
|
||
| #endif | ||
|
|
||
| enum CDVDestinationType { | ||
| DestinationTypeDataUrl = 0, | ||
| DestinationTypeFileUri | ||
|
|
@@ -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; | ||
|
|
@@ -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 | ||
Uh oh!
There was an error while loading. Please reload this page.