Implement deletion of asset-library files

Summary:
WIP: Starting point insofar as I'm not sure if there should be an Android equivalent.

My application needs me to delete photos from the photos in iOS.

I have tested this in my application and it works, have a screenshot from iOS asking as well and it does successfully delete the photos from the global Photos app.

Related to https://github.com/facebook/react-native/issues/15253, it kind of continues using the deprecated assets-library framework, but that does need (and will be an bigger and bigger issue coming up) of assets-library URLs being used.

I also assume RN prefers error handling at the JS level? Are the URLs starting with `asset-library`, etc.
Closes https://github.com/facebook/react-native/pull/15481

Differential Revision: D6438016

Pulled By: hramos

fbshipit-source-id: 47512140f62f458c14ad2ade2b358846e168c964
This commit is contained in:
Edgar Aroutiounian 2017-11-29 12:07:49 -08:00 committed by Facebook Github Bot
parent e94fa7d8fc
commit 58503ff2c9
2 changed files with 26 additions and 0 deletions

View File

@ -133,6 +133,10 @@ class CameraRoll {
return this.saveToCameraRoll(tag, 'photo');
}
static deletePhotos(photos: Array<string>) {
return RCTCameraRollManager.deletePhotos(photos);
}
/**
* Saves the photo or video to the camera roll / gallery.
*

View File

@ -12,6 +12,7 @@
#import <CoreLocation/CoreLocation.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
#import <React/RCTBridge.h>
#import <React/RCTConvert.h>
@ -234,6 +235,27 @@ RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params
}];
}
RCT_EXPORT_METHOD(deletePhotos:(NSArray<NSString *>*)assets
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
{
NSArray<NSURL *> *assets_ = [RCTConvert NSURLArray:assets];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHFetchResult<PHAsset *> *fetched =
[PHAsset fetchAssetsWithALAssetURLs:assets_ options:nil];
[PHAssetChangeRequest deleteAssets:fetched];
}
completionHandler:^(BOOL success, NSError *error) {
if (success == YES) {
resolve(@(success));
}
else {
reject(@"Couldn't delete", @"Couldn't delete assets", error);
}
}
];
}
static void checkPhotoLibraryConfig()
{
#if RCT_DEV