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
#import "FocusViewController.h"
#import "SBJson.h"
#import <QuartzCore/QuartzCore.h>

@interface FocusViewController ()
@end

@implementation FocusViewController

@synthesize txtsecurecode;
@synthesize webView;
@synthesize OrganizationCode;
@synthesize  objUserDefaults;

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (IBAction)loginClicked:(id)sender {
    
    @try {
        if([[txtsecurecode text] isEqualToString:@""]  || [[OrganizationCode text] isEqualToString:@""] ) {
            [self alertStatus:@"Please enter Access code" :@"Login Failed!":0];
        } else {
            NSString *post =[[NSString alloc] initWithFormat:@"txtsecurecode=%@ @&password=%@",[txtsecurecode text],[OrganizationCode text]];
            NSLog(@"PostData: %@",post);
                      
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myexample.com/AccountService/security/ValidateAccess?accesscode=%@&companycode=%@&type=1", txtsecurecode.text, OrganizationCode.text]];
            
           //  save/restore the access credentials
            [[ NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"OrganizationCode"];
            [[ NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"txtsecurecode"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            [[NSUserDefaults standardUserDefaults] objectForKey:@"txtsecurecode"];
            [[NSUserDefaults standardUserDefaults] objectForKey:@"OrganizationCode"];

                                                       
            NSString *responseData = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:url] encoding:NSUTF8StringEncoding];
            
            if([responseData isEqualToString:@""]){
                [self alertStatus:@"Please enter valid Access Code" :@"Login Failed !" :0];
            }
            else
            {
            
            responseData = [responseData stringByReplacingOccurrencesOfString:@" "" " withString:@""];
            responseData = [responseData stringByReplacingOccurrencesOfString:@"\\" withString:@""];
            
            NSString* encodedString = [responseData stringByReplacingPercentEscapesUsingEncoding:
                                       NSUTF8StringEncoding];
            
            NSLog(@"Response ==> %@" ,encodedString);

             UIWebView *webView;
             webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, 320, 470)];
             webView.backgroundColor = [UIColor clearColor];
             webView.opaque = NO;
             [webView setDelegate:self];

            NSString* urlTwo = [[encodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
                                stringByReplacingOccurrencesOfString:@"%22" withString:@""];
            
            NSURL *url2;
                
                if([urlTwo hasPrefix:@"http://"]){
                    url2 = [NSURL URLWithString:urlTwo];
                }else{
                    url2 = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@" , urlTwo]];
                }
                
            NSLog(@"url2:%@", url2);
                      
            NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];              

            [webView loadRequest:requestObj];
                
            [[self view] addSubview:webView];
                
        }
        }
        
    }

    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
        [self alertStatus:@"Login Failed." :@"Login Failed!" :0];
    }
    }

   - (IBAction)backgroundClick:(id)sender
{
    [txtsecurecode resignFirstResponder];
    [OrganizationCode resignFirstResponder];
}

@end