Skip to content

Commit

Permalink
Cleaned up code and reorganised.
Browse files Browse the repository at this point in the history
  • Loading branch information
vasiabikeru committed Sep 3, 2015
1 parent ee9e7f0 commit 09a659a
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 257 deletions.
12 changes: 6 additions & 6 deletions GettingLocationWhenSuspended.xcodeproj/project.pbxproj
Expand Up @@ -14,8 +14,8 @@
1D4D72C71A54F80B000BD788 /* GettingLocationWhenSuspendedTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1D4D72C61A54F80B000BD788 /* GettingLocationWhenSuspendedTests.m */; };
1D4D72D61A54F873000BD788 /* LocationAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1D4D72D11A54F873000BD788 /* LocationAppDelegate.m */; };
1D4D72D71A54F873000BD788 /* LocationViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1D4D72D31A54F873000BD788 /* LocationViewController.m */; };
1D4D72D81A54F873000BD788 /* LocationShareModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 1D4D72D51A54F873000BD788 /* LocationShareModel.m */; };
1D4D72DA1A54F882000BD788 /* Location-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 1D4D72D91A54F882000BD788 /* Location-Info.plist */; };
7BEE0DA21B9841E600779C9A /* LocationManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 7BEE0DA11B9841E600779C9A /* LocationManager.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -41,9 +41,9 @@
1D4D72D11A54F873000BD788 /* LocationAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocationAppDelegate.m; sourceTree = "<group>"; };
1D4D72D21A54F873000BD788 /* LocationViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocationViewController.h; sourceTree = "<group>"; };
1D4D72D31A54F873000BD788 /* LocationViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocationViewController.m; sourceTree = "<group>"; };
1D4D72D41A54F873000BD788 /* LocationShareModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocationShareModel.h; sourceTree = "<group>"; };
1D4D72D51A54F873000BD788 /* LocationShareModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocationShareModel.m; sourceTree = "<group>"; };
1D4D72D91A54F882000BD788 /* Location-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Location-Info.plist"; sourceTree = "<group>"; };
7BEE0DA01B9841E600779C9A /* LocationManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocationManager.h; sourceTree = "<group>"; };
7BEE0DA11B9841E600779C9A /* LocationManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocationManager.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -89,8 +89,8 @@
1D4D72D11A54F873000BD788 /* LocationAppDelegate.m */,
1D4D72D21A54F873000BD788 /* LocationViewController.h */,
1D4D72D31A54F873000BD788 /* LocationViewController.m */,
1D4D72D41A54F873000BD788 /* LocationShareModel.h */,
1D4D72D51A54F873000BD788 /* LocationShareModel.m */,
7BEE0DA01B9841E600779C9A /* LocationManager.h */,
7BEE0DA11B9841E600779C9A /* LocationManager.m */,
1D4D72B41A54F80B000BD788 /* Main.storyboard */,
1D4D72B71A54F80B000BD788 /* Images.xcassets */,
1D4D72B91A54F80B000BD788 /* LaunchScreen.xib */,
Expand Down Expand Up @@ -231,8 +231,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7BEE0DA21B9841E600779C9A /* LocationManager.m in Sources */,
1D4D72D61A54F873000BD788 /* LocationAppDelegate.m in Sources */,
1D4D72D81A54F873000BD788 /* LocationShareModel.m in Sources */,
1D4D72AD1A54F80B000BD788 /* main.m in Sources */,
1D4D72D71A54F873000BD788 /* LocationViewController.m in Sources */,
);
Expand Down
12 changes: 3 additions & 9 deletions GettingLocationWhenSuspended/LocationAppDelegate.h
Expand Up @@ -7,18 +7,12 @@
//

#import <UIKit/UIKit.h>
#import "LocationShareModel.h"
#import "LocationManager.h"

@interface LocationAppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate>
@interface LocationAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong,nonatomic) LocationShareModel * shareModel;

@property (nonatomic) CLLocationCoordinate2D myLastLocation;
@property (nonatomic) CLLocationAccuracy myLastLocationAccuracy;

@property (nonatomic) CLLocationCoordinate2D myLocation;
@property (nonatomic) CLLocationAccuracy myLocationAccuracy;
@property (strong,nonatomic) LocationManager * shareModel;

@end
231 changes: 16 additions & 215 deletions GettingLocationWhenSuspended/LocationAppDelegate.m
Expand Up @@ -15,15 +15,15 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
{
NSLog(@"didFinishLaunchingWithOptions");

self.shareModel = [LocationShareModel sharedModel];
self.shareModel = [LocationManager sharedManager];
self.shareModel.afterResume = NO;

[self addApplicationStatusToPList:@"didFinishLaunchingWithOptions"];
[self.shareModel addApplicationStatusToPList:@"didFinishLaunchingWithOptions"];

UIAlertView * alert;

//We have to make sure that the Background App Refresh is enable for the Location updates to work in the background.
if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied){
if ([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied) {

alert = [[UIAlertView alloc]initWithTitle:@""
message:@"The app doesn't work without the Background App Refresh enabled. To turn it on, go to Settings > General > Background App Refresh"
Expand All @@ -32,16 +32,16 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
otherButtonTitles:nil, nil];
[alert show];

}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted){
} else if ([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted) {

alert = [[UIAlertView alloc]initWithTitle:@""
message:@"The functions of this app are limited because the Background App Refresh is disable."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];

} else{
} else {

// When there is a significant changes of the location,
// The key UIApplicationLaunchOptionsLocationKey will be returned from didFinishLaunchingWithOptions
Expand All @@ -58,237 +58,38 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
// are actually from the key "UIApplicationLaunchOptionsLocationKey"
self.shareModel.afterResume = YES;

self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
self.shareModel.anotherLocationManager.delegate = self;
self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;

if(IS_OS_8_OR_LATER) {
[self.shareModel.anotherLocationManager requestAlwaysAuthorization];
}

[self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];

[self addResumeLocationToPList];
[self.shareModel startMonitoringLocation];
[self.shareModel addResumeLocationToPList];
}
}

return YES;
}


-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

NSLog(@"locationManager didUpdateLocations: %@",locations);

for(int i=0;i<locations.count;i++){

CLLocation * newLocation = [locations objectAtIndex:i];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;

self.myLocation = theLocation;
self.myLocationAccuracy = theAccuracy;
}

[self addLocationToPList:self.shareModel.afterResume];
}


- (void)applicationDidEnterBackground:(UIApplication *)application
{
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"applicationDidEnterBackground");
[self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges];

if(IS_OS_8_OR_LATER) {
[self.shareModel.anotherLocationManager requestAlwaysAuthorization];
}
[self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
[self.shareModel restartMonitoringLocation];

[self addApplicationStatusToPList:@"applicationDidEnterBackground"];
[self.shareModel addApplicationStatusToPList:@"applicationDidEnterBackground"];
}



- (void)applicationDidBecomeActive:(UIApplication *)application
{
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"applicationDidBecomeActive");

[self addApplicationStatusToPList:@"applicationDidBecomeActive"];
[self.shareModel addApplicationStatusToPList:@"applicationDidBecomeActive"];

//Remove the "afterResume" Flag after the app is active again.
self.shareModel.afterResume = NO;

if(self.shareModel.anotherLocationManager)
[self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges];

self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
self.shareModel.anotherLocationManager.delegate = self;
self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;

if(IS_OS_8_OR_LATER) {
[self.shareModel.anotherLocationManager requestAlwaysAuthorization];
}
[self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
[self.shareModel startMonitoringLocation];
}


-(void)applicationWillTerminate:(UIApplication *)application{
- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(@"applicationWillTerminate");
[self addApplicationStatusToPList:@"applicationWillTerminate"];
}


///////////////////////////////////////////////////////////////
// Below are 3 functions that add location and Application status to PList
// The purpose is to collect location information locally

-(void)addResumeLocationToPList{

NSLog(@"addResumeLocationToPList");
UIApplication* application = [UIApplication sharedApplication];

NSString * appState;
if([application applicationState]==UIApplicationStateActive)
appState = @"UIApplicationStateActive";
if([application applicationState]==UIApplicationStateBackground)
appState = @"UIApplicationStateBackground";
if([application applicationState]==UIApplicationStateInactive)
appState = @"UIApplicationStateInactive";

self.shareModel.myLocationDictInPlist = [[NSMutableDictionary alloc]init];
[self.shareModel.myLocationDictInPlist setObject:@"UIApplicationLaunchOptionsLocationKey" forKey:@"Resume"];
[self.shareModel.myLocationDictInPlist setObject:appState forKey:@"AppState"];
[self.shareModel.myLocationDictInPlist setObject:[NSDate date] forKey:@"Time"];

NSString *plistName = [NSString stringWithFormat:@"LocationArray.plist"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", docDir, plistName];

NSMutableDictionary *savedProfile = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];

if (!savedProfile){
savedProfile = [[NSMutableDictionary alloc] init];
self.shareModel.myLocationArrayInPlist = [[NSMutableArray alloc]init];
}
else{
self.shareModel.myLocationArrayInPlist = [savedProfile objectForKey:@"LocationArray"];
}

if(self.shareModel.myLocationDictInPlist)
{
[self.shareModel.myLocationArrayInPlist addObject:self.shareModel.myLocationDictInPlist];
[savedProfile setObject:self.shareModel.myLocationArrayInPlist forKey:@"LocationArray"];
}

if (![savedProfile writeToFile:fullPath atomically:FALSE] ) {
NSLog(@"Couldn't save LocationArray.plist" );
}
}



-(void)addLocationToPList:(BOOL)fromResume{
NSLog(@"addLocationToPList");

UIApplication* application = [UIApplication sharedApplication];

NSString * appState;
if([application applicationState]==UIApplicationStateActive)
appState = @"UIApplicationStateActive";
if([application applicationState]==UIApplicationStateBackground)
appState = @"UIApplicationStateBackground";
if([application applicationState]==UIApplicationStateInactive)
appState = @"UIApplicationStateInactive";

self.shareModel.myLocationDictInPlist = [[NSMutableDictionary alloc]init];
[self.shareModel.myLocationDictInPlist setObject:[NSNumber numberWithDouble:self.myLocation.latitude] forKey:@"Latitude"];
[self.shareModel.myLocationDictInPlist setObject:[NSNumber numberWithDouble:self.myLocation.longitude] forKey:@"Longitude"];
[self.shareModel.myLocationDictInPlist setObject:[NSNumber numberWithDouble:self.myLocationAccuracy] forKey:@"Accuracy"];

[self.shareModel.myLocationDictInPlist setObject:appState forKey:@"AppState"];

if(fromResume)
[self.shareModel.myLocationDictInPlist setObject:@"YES" forKey:@"AddFromResume"];
else
[self.shareModel.myLocationDictInPlist setObject:@"NO" forKey:@"AddFromResume"];

[self.shareModel.myLocationDictInPlist setObject:[NSDate date] forKey:@"Time"];

NSString *plistName = [NSString stringWithFormat:@"LocationArray.plist"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", docDir, plistName];

NSMutableDictionary *savedProfile = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];

if (!savedProfile){
savedProfile = [[NSMutableDictionary alloc] init];
self.shareModel.myLocationArrayInPlist = [[NSMutableArray alloc]init];
}
else{
self.shareModel.myLocationArrayInPlist = [savedProfile objectForKey:@"LocationArray"];
}

NSLog(@"Dict: %@",self.shareModel.myLocationDictInPlist);

if(self.shareModel.myLocationDictInPlist)
{
[self.shareModel.myLocationArrayInPlist addObject:self.shareModel.myLocationDictInPlist];
[savedProfile setObject:self.shareModel.myLocationArrayInPlist forKey:@"LocationArray"];
}

if (![savedProfile writeToFile:fullPath atomically:FALSE] ) {
NSLog(@"Couldn't save LocationArray.plist" );
}
}



-(void)addApplicationStatusToPList:(NSString*)applicationStatus{

NSLog(@"addApplicationStatusToPList");
UIApplication* application = [UIApplication sharedApplication];

NSString * appState;
if([application applicationState]==UIApplicationStateActive)
appState = @"UIApplicationStateActive";
if([application applicationState]==UIApplicationStateBackground)
appState = @"UIApplicationStateBackground";
if([application applicationState]==UIApplicationStateInactive)
appState = @"UIApplicationStateInactive";

self.shareModel.myLocationDictInPlist = [[NSMutableDictionary alloc]init];
[self.shareModel.myLocationDictInPlist setObject:applicationStatus forKey:@"applicationStatus"];
[self.shareModel.myLocationDictInPlist setObject:appState forKey:@"AppState"];
[self.shareModel.myLocationDictInPlist setObject:[NSDate date] forKey:@"Time"];

NSString *plistName = [NSString stringWithFormat:@"LocationArray.plist"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", docDir, plistName];

NSMutableDictionary *savedProfile = [[NSMutableDictionary alloc] initWithContentsOfFile:fullPath];

if (!savedProfile){
savedProfile = [[NSMutableDictionary alloc] init];
self.shareModel.myLocationArrayInPlist = [[NSMutableArray alloc]init];
}
else{
self.shareModel.myLocationArrayInPlist = [savedProfile objectForKey:@"LocationArray"];
}

if(self.shareModel.myLocationDictInPlist)
{
[self.shareModel.myLocationArrayInPlist addObject:self.shareModel.myLocationDictInPlist];
[savedProfile setObject:self.shareModel.myLocationArrayInPlist forKey:@"LocationArray"];
}

if (![savedProfile writeToFile:fullPath atomically:FALSE] ) {
NSLog(@"Couldn't save LocationArray.plist" );
}
[self.shareModel addApplicationStatusToPList:@"applicationWillTerminate"];
}


Expand Down
Expand Up @@ -11,15 +11,28 @@

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

@interface LocationShareModel : NSObject
@interface LocationManager : NSObject

@property (nonatomic) CLLocationManager * anotherLocationManager;

@property (nonatomic) CLLocationCoordinate2D myLastLocation;
@property (nonatomic) CLLocationAccuracy myLastLocationAccuracy;

@property (nonatomic) CLLocationCoordinate2D myLocation;
@property (nonatomic) CLLocationAccuracy myLocationAccuracy;

@property (nonatomic) NSMutableDictionary *myLocationDictInPlist;
@property (nonatomic) NSMutableArray *myLocationArrayInPlist;

@property (nonatomic) BOOL afterResume;

+(id)sharedModel;
+ (id)sharedManager;

- (void)startMonitoringLocation;
- (void)restartMonitoringLocation;

- (void)addResumeLocationToPList;
- (void)addLocationToPList:(BOOL)fromResume;
- (void)addApplicationStatusToPList:(NSString*)applicationStatus;

@end

0 comments on commit 09a659a

Please sign in to comment.