github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/clients/ios-objc/photobackup/LAAppDelegate.m (about) 1 // 2 // LAAppDelegate.m 3 // photobackup 4 // 5 // Created by Nick O'Neill on 10/20/13. 6 // Copyright (c) 2013 The Camlistore Authors. All rights reserved. 7 // 8 9 #import "LAAppDelegate.h" 10 #import "LACamliUtil.h" 11 #import "LACamliFile.h" 12 #import "LAViewController.h" 13 #import <AssetsLibrary/AssetsLibrary.h> 14 #import <HockeySDK/HockeySDK.h> 15 16 17 @implementation LAAppDelegate 18 19 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 20 { 21 [[BITHockeyManager sharedHockeyManager] configureWithIdentifier:@"de94cf9f0f0ad2ea0b19b2ad18ebe11f" 22 delegate:self]; 23 [[BITHockeyManager sharedHockeyManager] startManager]; 24 [[BITHockeyManager sharedHockeyManager].updateManager setDelegate:self]; 25 [[BITHockeyManager sharedHockeyManager].updateManager checkForUpdate]; 26 27 self.locationManager = [[CLLocationManager alloc] init]; 28 self.locationManager.delegate = self; 29 [self.locationManager startMonitoringSignificantLocationChanges]; 30 31 [self loadCredentials]; 32 33 self.library = [[ALAssetsLibrary alloc] init]; 34 35 return YES; 36 } 37 38 - (NSString *)customDeviceIdentifierForUpdateManager:(BITUpdateManager *)updateManager 39 { 40 if ([[UIDevice currentDevice] respondsToSelector:@selector(uniqueIdentifier)]) { 41 return [[UIDevice currentDevice] performSelector:@selector(uniqueIdentifier)]; 42 } 43 44 return nil; 45 } 46 47 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 48 { 49 [self checkForUploads]; 50 } 51 52 - (void)loadCredentials 53 { 54 NSURL *serverURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] stringForKey:CamliServerKey]]; 55 NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:CamliUsernameKey]; 56 57 NSString *password = nil; 58 if (username) { 59 password = [LACamliUtil passwordForUsername:username]; 60 } 61 62 if (serverURL && username && password) { 63 [LACamliUtil statusText:@[@"found credentials"]]; 64 [LACamliUtil logText:@[@"found credentials"]]; 65 self.client = [[LACamliClient alloc] initWithServer:serverURL username:username andPassword:password]; 66 67 // TODO there must be a better way to get the current instance of this 68 LAViewController *mainView = (LAViewController *)[(UINavigationController *)self.window.rootViewController topViewController]; 69 [self.client setDelegate:mainView]; 70 } else { 71 [LACamliUtil statusText:@[@"credentials or server not found"]]; 72 } 73 74 [self checkForUploads]; 75 } 76 77 - (void)checkForUploads 78 { 79 if (self.client && [self.client readyToUpload]) { 80 NSInteger __block filesToUpload = 0; 81 82 [LACamliUtil statusText:@[@"looking for new files..."]]; 83 84 // checking all assets can take some time 85 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 86 [self.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 87 88 [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { 89 if (result && [result valueForProperty:ALAssetPropertyType] != ALAssetTypeVideo) { // enumerate returns null after the last item 90 LACamliFile *file = [[LACamliFile alloc] initWithAsset:result]; 91 92 if (![_client fileAlreadyUploaded:file]) { 93 filesToUpload++; 94 LALog(@"found %ld files",(long)filesToUpload); 95 [LACamliUtil logText:@[[NSString stringWithFormat:@"found %ld files",(long)filesToUpload]]]; 96 [_client addFile:file withCompletion:nil]; 97 } else { 98 LALog(@"file already uploaded: %@",file.blobRef); 99 } 100 } 101 }]; 102 103 if (filesToUpload == 0) { 104 [LACamliUtil statusText:@[@"no new files to upload"]]; 105 } 106 107 [UIApplication sharedApplication].applicationIconBadgeNumber = filesToUpload; 108 109 } failureBlock:^(NSError *error) { 110 LALog(@"failed enumerate: %@",error); 111 [LACamliUtil errorText:@[@"failed enumerate: ",[error description]]]; 112 }]; 113 }); 114 } 115 } 116 117 - (void)applicationWillResignActive:(UIApplication *)application 118 { 119 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 120 // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 121 } 122 123 - (void)applicationDidEnterBackground:(UIApplication *)application 124 { 125 // 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. 126 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 127 } 128 129 - (void)applicationWillEnterForeground:(UIApplication *)application 130 { 131 // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 132 } 133 134 - (void)applicationDidBecomeActive:(UIApplication *)application 135 { 136 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 137 [self checkForUploads]; 138 } 139 140 - (void)applicationWillTerminate:(UIApplication *)application 141 { 142 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 143 } 144 145 @end