Reputation: 502
Hello SO users since i started my career in swift and my current project is in objective c i need to convert my swift code into objective c .
I've requirement to disable taking screenshot in iOS device. since Apple not giving support for that, another workaround Im trying is - get notified when screenshot is taken using UIApplicationUserDidTakeScreenshotNotification and get last photo from photo gallery and delete it as below code in swift.
func deleteAppScreenShot() {
NSLog("i am here")
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors?[0] = Foundation.NSSortDescriptor(key: "creationDate", ascending: true)
let fetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)
guard let lastAsset = fetchResult.lastObject else { return }
PHPhotoLibrary.shared().performChanges {
PHAssetChangeRequest.deleteAssets([lastAsset] as NSFastEnumeration)
} completionHandler: { (success, errorMessage) in
if !success, let errorMessage = errorMessage {
print(errorMessage.localizedDescription)
}
}
}
However the above approach is not the desired solution as it will always ask the user permission to delete the last photo
Can someone kindly tell how to disable the screenshot in iOS ?
Upvotes: 0
Views: 5250
Reputation: 885
A) First of all iOS does not allows you to disable the screenshot so you must clearly convey to the Client or Any business person that it is not a feasible thing as it is OS related.
B) Next thing the solution provided you is not an apt solution as it just detects when the screenshot is taken and after that user has to manually delete the screenshot with his permission . It cannot be done automatically(without user permission) as APPLE PHOTOS API does not allows you to do that because of strict user privacy policy. So this solution is the last implementation if any case client is adamant or anyhow wants to implement it, but according to my experience it is not a 100 percent solution and must not be implemented
I can understand the trouble of converting back from Swift to Objective c . Since i have the same experience i can share snapshot of Objective C codes which you can assemble together to achieve the desired result . The code is:
For getting last object:
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
PHAsset *lastAsset = [fetchResult lastObject];
For deleting perhaps:
PHFetchResult *asset = [PHAsset fetchAssetsWithALAssetURLs:@“Your asset url” options:nil];
[asset enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@",[obj class]);
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
BOOL req = [obj canPerformEditOperation:PHAssetEditOperationDelete];
if (req) {
NSLog(@"true");
[PHAssetChangeRequest deleteAssets:@[obj]];
}
} completionHandler:^(BOOL success, NSError *error) {
NSLog(@"Finished Delete asset. %@", (success ? @"Success." : error));
if (success) {
NSLog(@“delete successfully”);
}
}];
}];
Follow link1 link2 and yes write to me back in comment in any case.
Upvotes: 1