4

Is there a way to delete images (and videos) in camera roll in the photos app that my app didn't create. I know you can't delete things from Asset Library that your app didn't create. But this app isn't on app store. It's on a kiosk type environment. So I can use private APIs.

So is there a way to do this using private APIs that apple would not approve for the app store, but would work for my situation.

Thanks.

rmaddy
  • 298,130
  • 40
  • 468
  • 517
hdsenevi
  • 809
  • 14
  • 27

2 Answers2

11

Yep, you can do this in iOS 8 using Photos framework.

For example if you have Assets URLs stored in NSArray *assetsURLs

PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
    [library performChanges:^{
        PHFetchResult *assetsToBeDeleted = [PHAsset fetchAssetsWithALAssetURLs:assetsURLs options:nil];
        [PHAssetChangeRequest deleteAssets:assetsToBeDeleted];
    } completionHandler:^(BOOL success, NSError *error)
     {
         //do something here
    }];

this code will ask user to confirm removal from Camera Roll.

Andrei Malygin
  • 355
  • 2
  • 11
  • Has anyone ever made this work? I don't believe an app can delete assets from the camera roll. – coco Mar 03 '15 at 22:47
  • It's in the class reference. [deleteAssets:](https://developer.apple.com/library/ios/documentation/Photos/Reference/PHAssetChangeRequest_Class/index.html#//apple_ref/occ/clm/PHAssetChangeRequest/deleteAssets:) – cschandler Jul 24 '15 at 02:53
  • 1
    Yes it works but as he said it requires user confirmation – malhal Feb 03 '16 at 02:28
3

Here is a version for Swift that will delete all photos in the library.

First, you will need to be sure that you have the key for permissions in your app's info.plist file: enter image description here

You will need to have been given permission by the user to access Photos (not including code here for that).

Next, the import & code:

import Photos

func deleteAllPhotos() {
    let library = PHPhotoLibrary.shared()
    library.performChanges({
        let fetchOptions = PHFetchOptions()
        let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
        PHAssetChangeRequest.deleteAssets(allPhotos)
    }) { (success, error) in
        // Handle success & errors
    }
}

When this code is called, the user receives an OS prompt asking to confirm the deletion. Assuming they click yes, then everything is gone.

CodeBender
  • 30,010
  • 12
  • 103
  • 113