Skip to content

Commit 554e873

Browse files
fxfactorialfacebook-github-bot
authored andcommittedNov 29, 2017
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 #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 #15481 Differential Revision: D6438016 Pulled By: hramos fbshipit-source-id: 47512140f62f458c14ad2ade2b358846e168c964
1 parent 2e424fb commit 554e873

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
 

‎Libraries/CameraRoll/CameraRoll.js

+4
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ class CameraRoll {
133133
return this.saveToCameraRoll(tag, 'photo');
134134
}
135135

136+
static deletePhotos(photos: Array<string>) {
137+
return RCTCameraRollManager.deletePhotos(photos);
138+
}
139+
136140
/**
137141
* Saves the photo or video to the camera roll / gallery.
138142
*

‎Libraries/CameraRoll/RCTCameraRollManager.m

+22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import <CoreLocation/CoreLocation.h>
1313
#import <Foundation/Foundation.h>
1414
#import <UIKit/UIKit.h>
15+
#import <Photos/Photos.h>
1516

1617
#import <React/RCTBridge.h>
1718
#import <React/RCTConvert.h>
@@ -234,6 +235,27 @@ static void RCTResolvePromise(RCTPromiseResolveBlock resolve,
234235
}];
235236
}
236237

238+
RCT_EXPORT_METHOD(deletePhotos:(NSArray<NSString *>*)assets
239+
resolve:(RCTPromiseResolveBlock)resolve
240+
reject:(RCTPromiseRejectBlock)reject)
241+
{
242+
NSArray<NSURL *> *assets_ = [RCTConvert NSURLArray:assets];
243+
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
244+
PHFetchResult<PHAsset *> *fetched =
245+
[PHAsset fetchAssetsWithALAssetURLs:assets_ options:nil];
246+
[PHAssetChangeRequest deleteAssets:fetched];
247+
}
248+
completionHandler:^(BOOL success, NSError *error) {
249+
if (success == YES) {
250+
resolve(@(success));
251+
}
252+
else {
253+
reject(@"Couldn't delete", @"Couldn't delete assets", error);
254+
}
255+
}
256+
];
257+
}
258+
237259
static void checkPhotoLibraryConfig()
238260
{
239261
#if RCT_DEV

0 commit comments

Comments
 (0)