当iPhone应用程序在后台时,有没有办法收听文件系统更改?我想创建一个云托管应用程序,如果您添加新文件而不必先打开应用程序,它将自动将您的照片上传到云端。
例如,如果用户拍摄新照片或拍摄屏幕截图或录制内容。我是否可以收到有关已将项目添加到文件系统的通知?
答案 0 :(得分:3)
您可以使用宣布iOS8发布的照片框架来执行此操作。
阅读有关更改观察者 Link
的文档Apple示例sample
更改观察。使用共享的 PHPhotoLibrary 对象为您获取的资产和集合注册更改处理程序。当其他应用或设备更改资产的内容或元数据或集合中的资产列表时,照片会告知您的应用。 PHChange 对象提供有关每次更改之前和之后的对象状态的信息,其语义使得更新集合视图或类似接口变得容易
如果对照片应用有任何更新,您可以使用**照片框架
拍照首先,您需要注册照片观察员
- (void)applicationWillTerminate:(UIApplication *)application {
[[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
[[UIApplication sharedApplication] endBackgroundTask:UIBackgroundTaskInvalid];
[[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
// dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
// //Background Thread
// isBackgroundOrForeground = YES;
// [self getGreenEvent];
// dispatch_async(dispatch_get_main_queue(), ^(void){
// //Run UI Updates
// });
// });
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"=== DID ENTER BACKGROUND ===");
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
}];
if (bgTask == UIBackgroundTaskInvalid) {
NSLog(@"This application does not support background mode");
} else {
//if application supports background mode, we'll see this log.
NSLog(@"Application will continue to run in background");
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
[PHPhotoLibrary.sharedPhotoLibrary registerChangeObserver:self];
}
}];
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
isBackgroundOrForeground = NO;
NSString *oAuthToken = [[NSUserDefaults standardUserDefaults]stringForKey:KEY_ACCESS_TOKEN];
if(oAuthToken.length!=0 || oAuthToken!=nil)
[self getGreenEvent];
}
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)photoLibraryDidChange:(PHChange *)changeInstance {
NSLog(@"content being changed");
/*
Change notifications may be made on a background queue. Re-dispatch to the
main queue before acting on the change as we'll be updating the UI.
*/
__block BOOL reloadRequired = NO;
__block NSIndexSet *removedIndex;
__block NSIndexSet *insertedIndexes;
dispatch_async(dispatch_get_main_queue(), ^{
// Loop through the section fetch results, replacing any fetch results that have been updated.
NSMutableArray *updatedSectionFetchResults = [self.sectionFetchResults mutableCopy];
// __block BOOL reloadRequired = NO;
[self.sectionFetchResults enumerateObjectsUsingBlock:^(PHFetchResult *collectionsFetchResult, NSUInteger index, BOOL *stop) {
PHFetchResultChangeDetails *changeDetails = [changeInstance changeDetailsForFetchResult:collectionsFetchResult];
removedIndex = changeDetails.removedIndexes;
insertedIndexes = changeDetails.insertedIndexes;
if (changeDetails != nil) {
[updatedSectionFetchResults replaceObjectAtIndex:index withObject:[changeDetails fetchResultAfterChanges]];
reloadRequired = YES;
self.sectionFetchResults = updatedSectionFetchResults;
if(insertedIndexes != nil){
[self backgroundUpload];
}else{
}
}
}];
if (reloadRequired) {
self.sectionFetchResults = updatedSectionFetchResults;
}
});
}
-(void)getAllPhotosFromCamera{
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
PHFetchResult *allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
PHFetchResult *topLevelUserCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
// Store the PHFetchResult objects and localized titles for each section.
self.sectionFetchResults = @[allPhotos, smartAlbums, topLevelUserCollections];
}
-(NSMutableArray *)getNumberOfPhotoFromCameraRoll:(NSArray *)array{
PHFetchResult *fetchResult = array[1];
int index = 0;
unsigned long pictures = 0;
for(int i = 0; i < fetchResult.count; i++){
unsigned long temp = 0;
temp = [PHAsset fetchAssetsInAssetCollection:fetchResult[i] options:nil].count;
if(temp > pictures ){
pictures = temp;
index = i;
}
}
PHCollection *collection = fetchResult[index];
if (![collection isKindOfClass:[PHAssetCollection class]]) {
// return;
}
// Configure the AAPLAssetGridViewController with the asset collection.
PHAssetCollection *assetCollection = (PHAssetCollection *)collection;
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
self. assetsFetchResults = assetsFetchResult;
self. assetCollection = assetCollection;
self.numberOfPhotoArray = [NSMutableArray array];
for (int i = 0; i<[assetsFetchResult count]; i++) {
PHAsset *asset = assetsFetchResult[i];
[self.numberOfPhotoArray addObject:asset];
}
NSLog(@"%lu",(unsigned long)[self.numberOfPhotoArray count]);
return self.numberOfPhotoArray;
}
其中bgTask是
的全局对象UIBackgroundTaskIdentifier bgTask;
@property (nonatomic, strong) NSArray *sectionFetchResults;
@property (nonatomic, strong) PHFetchResult *assetsFetchResults;
@property (nonatomic, strong) PHAssetCollection *assetCollection;
@property (nonatomic, strong) NSMutableArray *numberOfPhotoArray;