-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmergencyContacts.m
executable file
·707 lines (608 loc) · 30.7 KB
/
EmergencyContacts.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
//
// EmergencyContacts.m
// DisasterHelper
//
// Created by ENCLAVEIT on 3/21/14.
// Copyright (c) 2014 Tan (Tharin) T.NGUYEN. All rights reserved.
//
#import "EmergencyContacts.h"
@interface EmergencyContacts ()
@end
UIAlertView *changePoliceNumber;
UIAlertView *changeFireNumber;
UIAlertView *changeAmbulanceNumber;
UIAlertView *userNotExist;
UIAlertView *duplicateUser;
UIAlertView *password;
UIAlertView *connectFail;
UIAlertView *updateSuccess;
UIAlertView *enterAll;
NSString *passwordField=@"";
NSString *policeNumber=@"113",*fireNumber=@"114",*ambulanceNumber=@"115";
NSString *user1Number=@"Phone number 1",*user2Number=@"Phone number 2",*user3Number=@"Phone number 3";
NSString *user1Name=@"Contact 1",*user2Name=@"Contact 2",*user3Name=@"Contact 3";
ABPeoplePickerNavigationController *picker1;
@implementation EmergencyContacts
int userID=0;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
//RGB color macro
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
//Setting View
imageBackground.backgroundColor=UIColorFromRGB(0xaad9ed);
imageUser3.backgroundColor=UIColorFromRGB(0x83cdec);
//imageEmergenyNumber.backgroundColor=UIColorFromRGB(0xf4eece);
//Custom navigator back button
UIImage *buttonImage = [UIImage imageNamed:@"back111.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
// //Drog Shadow here
// buttonUser1.layer.shadowColor = [UIColor grayColor].CGColor;
// buttonUser1.layer.shadowOffset = CGSizeMake(0, 2);
// buttonUser1.layer.shadowOpacity = 2;
// buttonUser1.layer.shadowRadius = 2;
// buttonUser1.clipsToBounds = NO;
//
// buttonUser2.layer.shadowColor = [UIColor grayColor].CGColor;
// buttonUser2.layer.shadowOffset = CGSizeMake(0, 2);
// buttonUser2.layer.shadowOpacity = 2;
// buttonUser2.layer.shadowRadius = 2;
// buttonUser2.clipsToBounds = NO;
//
// buttonUser3.layer.shadowColor = [UIColor grayColor].CGColor;
// buttonUser3.layer.shadowOffset = CGSizeMake(0, 2);
// buttonUser3.layer.shadowOpacity = 2;
// buttonUser3.layer.shadowRadius = 2;
// buttonUser3.clipsToBounds = NO;
//
// buttonPolice.layer.shadowColor = [UIColor grayColor].CGColor;
// buttonPolice.layer.shadowOffset = CGSizeMake(0, 2);
// buttonPolice.layer.shadowOpacity = 2;
// buttonPolice.layer.shadowRadius = 2;
// buttonPolice.clipsToBounds = NO;
//
// buttonFire.layer.shadowColor = [UIColor grayColor].CGColor;
// buttonFire.layer.shadowOffset = CGSizeMake(0, 2);
// buttonFire.layer.shadowOpacity = 2;
// buttonFire.layer.shadowRadius = 2;
// buttonFire.clipsToBounds = NO;
//
// buttonAmbulance.layer.shadowColor = [UIColor grayColor].CGColor;
// buttonAmbulance.layer.shadowOffset = CGSizeMake(0, 2);
// buttonAmbulance.layer.shadowOpacity = 2;
// buttonAmbulance.layer.shadowRadius = 2;
// buttonAmbulance.clipsToBounds = NO;
//
// buttonEditAmbulance.layer.shadowColor = [UIColor grayColor].CGColor;
// buttonEditAmbulance.layer.shadowOffset = CGSizeMake(0, 2);
// buttonEditAmbulance.layer.shadowOpacity = 2;
// buttonEditAmbulance.layer.shadowRadius = 2;
// buttonEditAmbulance.clipsToBounds = NO;
//
// buttonEditFire.layer.shadowColor = [UIColor grayColor].CGColor;
// buttonEditFire.layer.shadowOffset = CGSizeMake(0, 2);
// buttonEditFire.layer.shadowOpacity = 2;
// buttonEditFire.layer.shadowRadius = 2;
// buttonEditFire.clipsToBounds = NO;
//
// buttonEditPolice.layer.shadowColor = [UIColor grayColor].CGColor;
// buttonEditPolice.layer.shadowOffset = CGSizeMake(0, 2);
// buttonEditPolice.layer.shadowOpacity = 2;
// buttonEditPolice.layer.shadowRadius = 2;
// buttonEditPolice.clipsToBounds = NO;
//
// saveChangeButton.layer.shadowColor = [UIColor grayColor].CGColor;
// saveChangeButton.layer.shadowOffset = CGSizeMake(0, 2);
// saveChangeButton.layer.shadowOpacity = 2;
// saveChangeButton.layer.shadowRadius = 2;
// saveChangeButton.clipsToBounds = NO;
[super viewDidLoad];
// Do any additional setup after loading the view.
//self.view.backgroundColor = UIColorFromRGB(0x4fc1e9);
saveChangeButton.backgroundColor = UIColorFromRGB(0xffa333);
policeLabel.text=policeNumber;
fireLabel.text=fireNumber;
ambulanceLabel.text=ambulanceNumber;
phoneUser1.text=user1Number;
phoneUser2.text=user2Number;
phoneUser3.text=user3Number;
nameUser1.text=user1Name;
nameUser2.text=user2Name;
nameUser3.text=user3Name;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)editPoliceNumber:(id)sender {
//Edit police number
changePoliceNumber = [[UIAlertView alloc]initWithTitle:@"Change Police number"
message:@"Please enter number you want to add."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Save", nil];
changePoliceNumber.alertViewStyle=UIAlertViewStylePlainTextInput;
[changePoliceNumber textFieldAtIndex:0].delegate = self;
[changePoliceNumber show];
}
- (IBAction)editFireNumber:(id)sender {
//Edit fire number
changeFireNumber = [[UIAlertView alloc]initWithTitle:@"Change Fire number"
message:@"Please enter number you want to add."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Save", nil];
changeFireNumber.alertViewStyle=UIAlertViewStylePlainTextInput;
[changeFireNumber textFieldAtIndex:0].delegate = self;
[changeFireNumber show];
}
- (IBAction)editAmbulanceNumber:(id)sender {
//Edit ambulance number
changeAmbulanceNumber = [[UIAlertView alloc]initWithTitle:@"Change Ambulance number"
message:@"Please enter number you want to add."
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Save", nil];
changeAmbulanceNumber.alertViewStyle=UIAlertViewStylePlainTextInput;
[changeAmbulanceNumber textFieldAtIndex:0].delegate = self;
[changeAmbulanceNumber show];
}
- (IBAction)editUser1:(id)sender {
//edit user 1
userID=1;
picker1 = [[ABPeoplePickerNavigationController alloc] init];
picker1.peoplePickerDelegate = self;
[self presentViewController:picker1 animated:YES completion:nil];
}
- (IBAction)editUser2:(id)sender {
//edit user 2
userID=2;
picker1 = [[ABPeoplePickerNavigationController alloc] init];
picker1.peoplePickerDelegate = self;
[self presentViewController:picker1 animated:YES completion:nil];
}
- (IBAction)editUser3:(id)sender {
//edit user 3
userID=3;
picker1 = [[ABPeoplePickerNavigationController alloc] init];
picker1.peoplePickerDelegate = self;
[self presentViewController:picker1 animated:YES completion:nil];
}
- (void)peoplePickerNavigationControllerDidCancel: (ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
[self displayPerson:person];
[self dismissViewControllerAnimated:YES completion:nil];
return NO;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}
- (void)displayPerson:(ABRecordRef)person
{NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonFirstNameProperty);
NSUserDefaults *session = [NSUserDefaults standardUserDefaults];
NSString *ssid=[session objectForKey:@"ssid"];
NSUserDefaults *checkLogin = [NSUserDefaults standardUserDefaults];
NSString *check = [checkLogin valueForKey:@"checkLogin"];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *internetStatus=[user objectForKey:@"Internet"];
NSUserDefaults *ServerUrl = [NSUserDefaults standardUserDefaults];
NSString *stringurl=[ServerUrl objectForKey:@"serverurl"];
NSURL *url= [[NSURL alloc] initWithString:stringurl];
switch (userID) {
case 1:
{
//add name and phone number user 1
nameUser1.text = name;
NSString* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phone = @"[None]";
}
phoneUser1.text = phone;
CFRelease(phoneNumbers);
if ([internetStatus isEqualToString:@"NO"]) {
NSLog(@"Run with out network and don't do any thing");
} else if([internetStatus isEqualToString:@"YES"]) {
if ([check isEqualToString:@"LOGIN"]) {
NSUserDefaults *phonenumber = [NSUserDefaults standardUserDefaults];
NSString *phoneNumber =[phonenumber valueForKey:@"phone"];
// NSURL *url = [[NSURL alloc] initWithString:@"http://192.168.10.115:3000/users"];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
[myDictionary setObject:@"checkUserExist" forKey:@"key"];
[myDictionary setObject:ssid forKey:@"sid"];
[myDictionary setObject:phoneNumber forKey:@"phonenumber"];
[myDictionary setObject:phone forKey:@"targetphone"];
NSData *myData = [NSJSONSerialization dataWithJSONObject:myDictionary
options:kNilOptions
error:nil];
Server *connect = [[Server alloc]init];
NSString *result=[connect postRequest:url withData:myData];
if ([result isEqualToString:@"Found user!"]) {
break;
}else{
userNotExist = [[UIAlertView alloc]initWithTitle:@"Warning"
message:@"This user does not use this application.\nDo you want to continue?"
delegate:self
cancelButtonTitle:@"YES"
otherButtonTitles:@"NO", nil];
[userNotExist show];
break;
}
}
}
}
case 2:{
//add name and phone number user 2
nameUser2.text = name;
NSString* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phone = @"[None]";
}
phoneUser2.text = phone;
CFRelease(phoneNumbers);
if ([internetStatus isEqualToString:@"NO"]) {
NSLog(@"Run with out network and don't do any thing");
} else if([internetStatus isEqualToString:@"YES"]) {
if ([check isEqualToString:@"LOGIN"]) {
//NSURL *url = [[NSURL alloc] initWithString:@"http://192.168.10.115:3000/users"];
NSUserDefaults *phonenumber = [NSUserDefaults standardUserDefaults];
NSString *phoneNumber =[phonenumber valueForKey:@"phone"];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
[myDictionary setObject:@"checkUserExist" forKey:@"key"];
[myDictionary setObject:ssid forKey:@"sid"];
[myDictionary setObject:phoneNumber forKey:@"phonenumber"];
[myDictionary setObject:phone forKey:@"targetphone"];
NSData *myData = [NSJSONSerialization dataWithJSONObject:myDictionary
options:kNilOptions
error:nil];
Server *connect = [[Server alloc]init];
NSString *result=[connect postRequest:url withData:myData];
if ([result isEqualToString:@"Found user!"]) {
break;
}else{
userNotExist = [[UIAlertView alloc]initWithTitle:@"Warning"
message:@"This user does not use this application.\nDo you want to continue?"
delegate:self
cancelButtonTitle:@"YES"
otherButtonTitles:@"NO", nil];
[userNotExist show];
break;
}
}
}
//
}
case 3:{
//add name and phone number user 2
nameUser3.text = name;
NSString* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phone = @"[None]";
}
phoneUser3.text = phone;
CFRelease(phoneNumbers);
if ([internetStatus isEqualToString:@"NO"]) {
NSLog(@"Run with out network and don't do any thing");
} else if([internetStatus isEqualToString:@"YES"]) {
if ([check isEqualToString:@"LOGIN"]) {
//NSURL *url = [[NSURL alloc] initWithString:@"http://192.168.10.115:3000/users"];
NSUserDefaults *phonenumber = [NSUserDefaults standardUserDefaults];
NSString *phoneNumber =[phonenumber valueForKey:@"phone"];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
[myDictionary setObject:@"checkUserExist" forKey:@"key"];
[myDictionary setObject:ssid forKey:@"sid"];
[myDictionary setObject:phoneNumber forKey:@"phonenumber"];
[myDictionary setObject:phone forKey:@"targetphone"];
NSData *myData = [NSJSONSerialization dataWithJSONObject:myDictionary
options:kNilOptions
error:nil];
Server *connect = [[Server alloc]init];
NSString *result=[connect postRequest:url withData:myData];
if ([result isEqualToString:@"Found user!"]) {
break;
}else{
userNotExist = [[UIAlertView alloc]initWithTitle:@"Warning"
message:@"This user does not use this application.\nDo you want to continue?"
delegate:self
cancelButtonTitle:@"YES"
otherButtonTitles:@"NO", nil];
[userNotExist show];
break;
}
}
}
//
}
default:
break;
}
if ([phoneUser1.text isEqualToString:phoneUser2.text] || [phoneUser1.text isEqualToString:phoneUser3.text]|| [phoneUser2.text isEqualToString:phoneUser3.text]) {
//Duplicate number
duplicateUser = [[UIAlertView alloc]initWithTitle:@"Warning"
message:@"You have choosed the same number.\nPlease edit for your safe!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[duplicateUser show];
}
}
- (IBAction)callPolice:(id)sender {
//Call police
NSString *phoneNumberCall=[NSString stringWithFormat:@"tel:%@",policeLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumberCall]];
}
- (IBAction)callFire:(id)sender {
//Call fire
NSString *phoneNumberCall=[NSString stringWithFormat:@"tel:%@",fireLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumberCall]];
}
- (IBAction)callAmbulance:(id)sender {
//Call ambulance
NSString *phoneNumberCall=[NSString stringWithFormat:@"tel:%@",ambulanceLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumberCall]];
}
- (IBAction)callUser1:(id)sender {
NSString *phoneNumberCall=[NSString stringWithFormat:@"tel:%@",phoneUser1.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumberCall]];
}
- (IBAction)callUser2:(id)sender {
NSString *phoneNumberCall=[NSString stringWithFormat:@"tel:%@",phoneUser2.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumberCall]];
}
- (IBAction)callUser3:(id)sender {
NSString *phoneNumberCall=[NSString stringWithFormat:@"tel:%@",phoneUser3.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumberCall]];
}
- (IBAction)chooseImageUser1:(id)sender {
picker = [[UIImagePickerController alloc]init];
picker.delegate=self;
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker animated:YES completion:nil];
userID=1;
}
- (IBAction)chooseImageUser2:(id)sender {
picker = [[UIImagePickerController alloc]init];
picker.delegate=self;
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker animated:YES completion:nil];
userID=2;
}
- (IBAction)chooseImageUser3:(id)sender {
picker = [[UIImagePickerController alloc]init];
picker.delegate=self;
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker animated:YES completion:nil];
userID=3;
}
- (IBAction)saveChange:(id)sender {
NSLog(@"%@ %@",nameUser1.text,phoneUser1.text);
NSUserDefaults *checkLogin = [NSUserDefaults standardUserDefaults];
NSString *check = [checkLogin valueForKey:@"checkLogin"];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *internetStatus=[user objectForKey:@"Internet"];
if ([internetStatus isEqualToString:@"NO"]) {
NSLog(@"Run with out network and don't do any thing");
} else if([internetStatus isEqualToString:@"YES"]) {
if ([check isEqualToString:@"Offline"]) {
}
else if ([check isEqualToString:@"LOGIN"]) {
if (!([nameUser1.text isEqualToString:@"Contact 1"]
||[nameUser2.text isEqualToString:@"Contact 2"]
||[nameUser3.text isEqualToString:@"Contact 3"]) )
{
//Enter password to confirm
password = [[UIAlertView alloc]initWithTitle:@"Security"
message:@"Please enter your password below:"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
password.alertViewStyle=UIAlertViewStylePlainTextInput;
[password textFieldAtIndex:0].delegate = self;
[password textFieldAtIndex:0].secureTextEntry = YES;
[password show];
} else
{
//aler enter all
enterAll = [[UIAlertView alloc]initWithTitle:@"Missing Field"
message:@"Please choose three emergency contacts before saving to server for your safe."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[enterAll show];
}
}
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//UIImageView *img=[[UIImageView alloc]initWithImage:image];
switch (userID) {
case 1:
{
imageUser1.image=image;
[self dismissViewControllerAnimated:YES completion:nil];
break;
}
case 2:
{
imageUser2.image=image;
[self dismissViewControllerAnimated:YES completion:nil];
break;
}
case 3:{
imageUser3.image=image;
[self dismissViewControllerAnimated:YES completion:nil];
break;
}
default:
break;
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSUserDefaults *checkLogin = [NSUserDefaults standardUserDefaults];
NSString *check = [checkLogin valueForKey:@"checkLogin"];
NSUserDefaults *session = [NSUserDefaults standardUserDefaults];
NSString *ssid=[session objectForKey:@"ssid"];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *internetStatus=[user objectForKey:@"Internet"];
NSUserDefaults *ServerUrl = [NSUserDefaults standardUserDefaults];
NSString *stringurl=[ServerUrl objectForKey:@"serverurl"];
NSURL *url= [[NSURL alloc] initWithString:stringurl];
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
//Alert change ambulance, fire, police number
if (alertView == changeAmbulanceNumber || alertView == changeFireNumber || alertView == changePoliceNumber) {
if ([title isEqualToString:@"Cancel"]) {
[alertView dismissWithClickedButtonIndex:0 animated:YES];
}
else{
if (alertView==changePoliceNumber) {
policeLabel.text=[alertView textFieldAtIndex:0].text;
policeNumber=[alertView textFieldAtIndex:0].text;
[alertView dismissWithClickedButtonIndex:0 animated:YES];
}
if (alertView==changeFireNumber) {
fireLabel.text=[alertView textFieldAtIndex:0].text;
fireNumber=[alertView textFieldAtIndex:0].text;
[alertView dismissWithClickedButtonIndex:0 animated:YES];
}
if (alertView == changeAmbulanceNumber) {
ambulanceLabel.text=[alertView textFieldAtIndex:0].text;
ambulanceNumber=[alertView textFieldAtIndex:0].text;
[alertView dismissWithClickedButtonIndex:0 animated:YES];
}
}
}
if (alertView == userNotExist) {
if ([title isEqualToString:@"YES"]) {
[alertView dismissWithClickedButtonIndex:0 animated:YES];
}else{
picker1 = [[ABPeoplePickerNavigationController alloc] init];
picker1.peoplePickerDelegate = self;
[self presentViewController:picker1 animated:YES completion:nil];
}
}
if (alertView == password) {
if ([title isEqualToString:@"Cancel"]) {
[alertView dismissWithClickedButtonIndex:0 animated:YES];
phoneUser1.text=user1Number;
nameUser1.text=user1Name;
phoneUser2.text=user2Number;
nameUser2.text=user2Name;
phoneUser3.text=user3Number;
nameUser3.text=user3Name;
}else{
//pass.text=[password textFieldAtIndex:0].text;
passwordField=[password textFieldAtIndex:0].text ;
[alertView dismissWithClickedButtonIndex:0 animated:YES];
if ([internetStatus isEqualToString:@"NO"]) {
NSLog(@"Run with out network and don't do any thing");
}
else if([internetStatus isEqualToString:@"YES"]) {
if ([check isEqualToString:@"Offline"]) {
}
else if ([check isEqualToString:@"LOGIN"]) {
@try{
NSUserDefaults *phonenumber = [NSUserDefaults standardUserDefaults];
NSString *phoneNumber =[phonenumber valueForKey:@"phone"];
// NSURL *url = [[NSURL alloc] initWithString:@"http://192.168.10.115:3000/users"];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
[myDictionary setObject:@"updateInformation" forKey:@"key"];
//passwordField=pass.text;
//NSLog(@"Password:%@",passwordField );
[myDictionary setObject:passwordField forKey:@"password"];
[myDictionary setObject:ssid forKey:@"sid"];
[myDictionary setObject:phoneNumber forKey:@"phonenumber"];
[myDictionary setObject:phoneUser1.text forKey:@"econtact1"];
[myDictionary setObject:phoneUser2.text forKey:@"econtact2"];
[myDictionary setObject:phoneUser3.text forKey:@"econtact3"];
NSData *myData = [NSJSONSerialization dataWithJSONObject:myDictionary
options:kNilOptions
error:nil];
Server *connect = [[Server alloc] init];
NSString *result = [connect postRequest:url withData:myData];
if([result isEqualToString:@"User profile updated!"]){
//Alert send success
updateSuccess = [[UIAlertView alloc]initWithTitle:@"Updated" message:@"Your emergency contacts had been updated!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[updateSuccess show];
//Save contacts
user1Name=nameUser1.text;
user1Number=phoneUser1.text;
user2Name=nameUser2.text;
user2Number=phoneUser2.text;
user3Number=phoneUser3.text;
user3Name=nameUser3.text;
}
if ([result isEqualToString:@"Password doesnot match!"]){
//Alert password wrong
password = [[UIAlertView alloc]initWithTitle:@"Security"
message:@"Please enter your password below:"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
password.alertViewStyle=UIAlertViewStylePlainTextInput;
[password textFieldAtIndex:0].delegate = self;
[password show];
}
}@catch (NSException *e){
//Alert can not connect with server
connectFail = [[UIAlertView alloc]initWithTitle:@"Connection Failed" message:@"Can not connect with server now!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[connectFail show ];
}
}
}
//Connect to server
}
}
if (alertView == connectFail || alertView == updateSuccess || alertView == enterAll) {
if ([title isEqualToString:@"OK"]) {
//hide alertview
[alertView dismissWithClickedButtonIndex:0 animated:YES];
}
}
}
@end