From 05680c13bde4f5610121df1899f7308de99ce3e5 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sat, 25 Nov 2017 18:53:24 +0530 Subject: [PATCH 1/2] fixing https://github.com/wso2/product-iots/issues/1502 --- iOSMDMAgent/AppDelegate.m | 15 +++++++++++++++ iOSMDMAgent/Endpoints.plist | 2 ++ iOSMDMAgent/LoginViewController.m | 9 ++++++++- iOSMDMAgent/URLUtils.h | 5 +++++ iOSMDMAgent/URLUtils.m | 22 +++++++++++++++++++++- 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/iOSMDMAgent/AppDelegate.m b/iOSMDMAgent/AppDelegate.m index db30851..fc8c3d5 100644 --- a/iOSMDMAgent/AppDelegate.m +++ b/iOSMDMAgent/AppDelegate.m @@ -6,6 +6,8 @@ #import "AppDelegate.h" #import "MDMUtils.h" #import "ConnectionUtils.h" +#import "ConnectionUtils.h" +#import "URLUtils.h" #define systemSoundID 1154 @@ -187,6 +189,19 @@ - (void) registerForPushToken { } } +- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { + if (status == kCLAuthorizationStatusAuthorized || status == kCLAuthorizationStatusDenied) { + NSLog(@"User responded to location"); + NSString *enrollURL = [URLUtils getEnrollmentURLFromPlist]; + NSString *serverURL = [URLUtils getEnrollmentURLFromPlist]; + if(enrollURL && ![@"" isEqualToString:enrollURL] && serverURL && ![@"" isEqualToString:serverURL]) { + [URLUtils saveServerURL:serverURL]; + [URLUtils saveEnrollmentURL:enrollURL]; + [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[URLUtils getEnrollmentURL]]]; + } + } +} + - (void)initLocation { NSLog(@"Initializing location manager"); // __block UIBackgroundTaskIdentifier bgTask =0; diff --git a/iOSMDMAgent/Endpoints.plist b/iOSMDMAgent/Endpoints.plist index a1d8bcc..71bcb43 100644 --- a/iOSMDMAgent/Endpoints.plist +++ b/iOSMDMAgent/Endpoints.plist @@ -20,6 +20,8 @@ 8243 ENROLMENT_PORT 9443 + ENROLLMENT_URL + SERVER_URL https://gateway.api.cloudstaging.wso2.com diff --git a/iOSMDMAgent/LoginViewController.m b/iOSMDMAgent/LoginViewController.m index af86938..90e5e41 100644 --- a/iOSMDMAgent/LoginViewController.m +++ b/iOSMDMAgent/LoginViewController.m @@ -18,6 +18,7 @@ - (void)viewDidLoad { [super viewDidLoad]; self.txtServer.delegate = self; // Do any additional setup after loading the view. + } - (void)didReceiveMemoryWarning { @@ -61,8 +62,14 @@ - (void)enroll { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:INVALID_SERVER_URL message:INVALID_SERVER_URL_MESSAGE delegate:nil cancelButtonTitle:OK_BUTTON_TEXT otherButtonTitles:nil, nil]; [alertView show]; } else { + /* + If the user is allowed to type the server url, assuption is this is not a production environment, + Therefore, both Enrollment URL(manager node URL where the enrollment app is stored) and the gateway url are + taken as the same. Check didChangeAuthorizationStatus method in AppDeligate.m for production behaviour, + Where the URLs are hard coded. + */ [URLUtils saveServerURL:self.txtServer.text]; - + [URLUtils saveEnrollmentURL:self.txtServer.text]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[URLUtils getEnrollmentURL]]]; } } diff --git a/iOSMDMAgent/URLUtils.h b/iOSMDMAgent/URLUtils.h index ff2d9d0..a2c47bd 100644 --- a/iOSMDMAgent/URLUtils.h +++ b/iOSMDMAgent/URLUtils.h @@ -39,6 +39,7 @@ extern NSString *const OPERATION_URI; extern NSString *const OPERATION_ID_RESPOSNE; extern NSString *const STATUS; extern int OAUTH_FAIL_CODE; +extern NSString *const ENROLLMENT_URL; + (void)saveServerURL:(NSString *)serverURL; + (NSString *)getServerURL; @@ -50,6 +51,10 @@ extern int OAUTH_FAIL_CODE; + (NSString *)getUnenrollURL; + (NSString *)getRefreshTokenURL; + (NSString *)getOperationURL; ++ (void)saveEnrollmentURL:(NSString *)enrollURL; ++ (NSString *)getSavedEnrollmentURL; ++ (NSString *)getEnrollmentURLFromPlist; ++ (NSString *)getServerURLFromPlist; @end diff --git a/iOSMDMAgent/URLUtils.m b/iOSMDMAgent/URLUtils.m index 53d01e4..541c414 100644 --- a/iOSMDMAgent/URLUtils.m +++ b/iOSMDMAgent/URLUtils.m @@ -41,6 +41,7 @@ @implementation URLUtils NSString *const FORM_ENCODED = @"application/x-www-form-urlencoded"; NSString *const OPERATION_ID_RESPOSNE = @"operationId"; NSString *const STATUS = @"status"; +NSString *const ENROLLMENT_URL = @"ENROLLMENT_URL"; + (NSDictionary *)readEndpoints { @@ -61,10 +62,29 @@ + (NSString *)getAPIPort { return [[URLUtils readEndpoints] objectForKey:API_PORT]; } ++ (NSString *)getEnrollmentURLFromPlist { + return [[URLUtils readEndpoints] objectForKey:ENROLLMENT_URL]; +} + ++ (NSString *)getServerURLFromPlist { + return [[URLUtils readEndpoints] objectForKey:SERVER_URL]; +} + + (NSString *)getEnrolmentPort { return [[URLUtils readEndpoints] objectForKey:ENROLMENT_PORT]; } ++ (NSString *)getSavedEnrollmentURL { + NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; + return [userDefaults objectForKey:ENROLLMENT_URL]; +} + ++ (void)saveEnrollmentURL:(NSString *)enrollURL { + NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; + [userDefaults setObject:enrollURL forKey:ENROLLMENT_URL]; + [userDefaults synchronize]; +} + + (void)saveServerURL:(NSString *)serverURL { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:serverURL forKey:SERVER_URL]; @@ -92,7 +112,7 @@ + (NSString *)getRefreshTokenURL{ } + (NSString *)getEnrollmentURL { - return [NSString stringWithFormat:@"%@:%@%@", [URLUtils getServerURL], [URLUtils getEnrolmentPort], [[URLUtils readEndpoints] objectForKey:ENROLLMENT_URI]]; + return [NSString stringWithFormat:@"%@:%@%@", [URLUtils getSavedEnrollmentURL], [URLUtils getEnrolmentPort], [[URLUtils readEndpoints] objectForKey:ENROLLMENT_URI]]; } @end From ccde697d0a4985840ea0fa6356279cca09b511cf Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sat, 25 Nov 2017 18:59:08 +0530 Subject: [PATCH 2/2] fixing minor styling issue --- iOSMDMAgent/LoginViewController.m | 1 - 1 file changed, 1 deletion(-) diff --git a/iOSMDMAgent/LoginViewController.m b/iOSMDMAgent/LoginViewController.m index 90e5e41..95f7658 100644 --- a/iOSMDMAgent/LoginViewController.m +++ b/iOSMDMAgent/LoginViewController.m @@ -18,7 +18,6 @@ - (void)viewDidLoad { [super viewDidLoad]; self.txtServer.delegate = self; // Do any additional setup after loading the view. - } - (void)didReceiveMemoryWarning {