[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenShot) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
#pragma mark - 监听用户截屏事件
- (void)screenShot
{
[self getPhotosAuthorizationStatus];
}
#pragma mark - 判断相册的授权状态
- (void)getPhotosAuthorizationStatus
{
if ([PHPhotoLibrary respondsToSelector:@selector(authorizationStatus)]){
if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized){
[self deleteScreenShot];
} else {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
[self deleteScreenShot];
}
}];
}
} else
{
[self deleteScreenShot];
[MBProgressHUD showMessage:nil detailMsg:@"您保存的图片内容可能涉及到敏感信息,建议您删除" position:MBProgressHUBPositionTop showTime:3];
}
}
#pragma mark - 删除相册最后一张图片
- (void)deleteScreenShot
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
PHFetchResult *collectonResuts = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:[PHFetchOptions new]] ;
[collectonResuts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
PHAssetCollection *assetCollection = obj;
if (assetCollection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumUserLibrary) {
PHFetchOptions *options = [PHFetchOptions new];
options.fetchLimit = 1;
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult *assetResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//获取相册的最后一张照片
if (assetResult.firstObject) {
[PHAssetChangeRequest deleteAssets:@[assetResult.firstObject]];
}
} completionHandler:^(BOOL success, NSError *error) {
NSLog(@"Error: %@", error);
}];
[MBProgressHUD showMessage:nil detailMsg:@"您保存的图片内容可能涉及到敏感信息,建议您删除" position:MBProgressHUBPositionTop showTime:3];
}
}];
});
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}
网友评论