001.
#import "Viewlogin.h"
002.
#import "loginViewController.h"
003.
004.
005.
@implementation
Viewlogin
006.
@synthesize
txtID;
007.
@synthesize
txtPassword;
008.
@synthesize
inputlogin;
009.
@synthesize
btnLogin;
010.
@synthesize
loging;
011.
012.
013.
014.
- (
void
)viewDidLoad
015.
{
016.
[
super
viewDidLoad];
017.
018.
}
019.
- (
IBAction
)clearInputText:(
id
)sender {
020.
txtID.text = @
""
;
021.
txtPassword.text = @
""
;
022.
}
023.
024.
- (
IBAction
)btnLogin:(
UIBarButtonItem
*)sender {
025.
026.
027.
028.
[
UIApplication
sharedApplication].networkActivityIndicatorVisible =
YES
;
029.
030.
loging
031.
= [[
UIAlertView
alloc] initWithTitle:@
""
message:@
"Login Checking..."
delegate:
nil
cancelButtonTitle:
nil
otherButtonTitles:
nil
];
032.
UIActivityIndicatorView
*progress= [[
UIActivityIndicatorView
alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
033.
progress.activityIndicatorViewStyle =
UIActivityIndicatorViewStyleWhiteLarge
;
034.
[loging addSubview:progress];
035.
[progress startAnimating];
036.
[progress release];
037.
[loging show];
038.
039.
040.
041.
NSString
*post = [
NSString
stringWithFormat:@
"sID=%@&sPassword=%@"
,[txtID text],[txtPassword text]];
042.
043.
NSData
*postData = [post dataUsingEncoding:
NSASCIIStringEncoding
allowLossyConversion:
YES
];
044.
NSString
*postLength = [
NSString
stringWithFormat:@
"%d"
, [postData length]];
045.
046.
047.
NSURL
*url = [
NSURL
URLWithString:@
"http://localhost:8888/checkLogin.php"
];
048.
049.
NSMutableURLRequest
*request = [
NSMutableURLRequest
requestWithURL:url
050.
cachePolicy:
NSURLRequestReloadIgnoringLocalCacheData
051.
timeoutInterval:10.0];
052.
[request setHTTPMethod:@
"POST"
];
053.
[request setValue:postLength forHTTPHeaderField:@
"Content-Length"
];
054.
[request setValue:@
"application/x-www-form-urlencoded"
forHTTPHeaderField:@
"Content-Type"
];
055.
[request setHTTPBody:postData];
056.
057.
NSURLConnection
*theConnection=[[
NSURLConnection
alloc] initWithRequest:request delegate:
self
];
058.
059.
060.
061.
if
(theConnection) {
062.
self
.inputlogin = [[
NSMutableData
data]
retain
];
063.
}
else
{
064.
UIAlertView
*connectFailMessage = [[
UIAlertView
alloc] initWithTitle:@
"NSURLConnection "
message:@
"Failed in viewDidLoad"
delegate:
self
cancelButtonTitle:@
"Ok"
otherButtonTitles:
nil
];
065.
[connectFailMessage show];
066.
[connectFailMessage release];
067.
}
068.
069.
}
070.
071.
- (
IBAction
)btnClose:(
id
)sender {
072.
}
073.
074.
075.
076.
077.
- (
void
)connection:(
NSURLConnection
*)connection didReceiveResponse:(
NSURLResponse
*)response
078.
{
079.
[inputlogin setLength:0];
080.
}
081.
082.
- (
void
)connection:(
NSURLConnection
*)connection didReceiveData:(
NSData
*)data
083.
{
084.
sleep(3);
085.
[inputlogin appendData:data];
086.
}
087.
088.
- (
void
)connection:(
NSURLConnection
*)connection didFailWithError:(
NSError
*)error
089.
{
090.
091.
092.
[
UIApplication
sharedApplication].networkActivityIndicatorVisible =
NO
;
093.
[loging dismissWithClickedButtonIndex:0 animated:
YES
];
094.
095.
[connection release];
096.
[inputlogin release];
097.
098.
099.
UIAlertView
*didFailWithErrorMessage = [[
UIAlertView
alloc] initWithTitle: @
"เกิดข้อผิดพลาด "
message: @
"ไม่สามารถเชื่อมต่อกับเซิร์ฟเวอร์ได้"
delegate:
self
cancelButtonTitle: @
"Ok"
otherButtonTitles:
nil
];
100.
[didFailWithErrorMessage show];
101.
[didFailWithErrorMessage release];
102.
103.
104.
105.
NSLog
(@
"Connection failed! Error - %@"
, [error localizedDescription]);
106.
107.
}
108.
109.
110.
- (
void
)connectionDidFinishLoading:(
NSURLConnection
*)connection
111.
{
112.
113.
[
UIApplication
sharedApplication].networkActivityIndicatorVisible =
NO
;
114.
[loging dismissWithClickedButtonIndex:0 animated:
YES
];
115.
116.
117.
118.
119.
120.
121.
if
(inputlogin)
122.
{
123.
124.
id
jsonObjects = [
NSJSONSerialization
JSONObjectWithData:inputlogin options:
NSJSONReadingMutableContainers
error:
nil
];
125.
126.
127.
NSString
*strStatus = [jsonObjects objectForKey:@
"Status"
];
128.
NSString
*strID = [jsonObjects objectForKey:@
"ID"
];
129.
NSString
*strMessage = [jsonObjects objectForKey:@
"Message"
];
130.
NSString
*strName = [jsonObjects objectForKey:@
"FNameE"
];
131.
NSLog
(@
"Status = %@"
,strStatus);
132.
NSLog
(@
"ID = %@"
,strID);
133.
NSLog
(@
"postID = %@"
,txtID.text);
134.
NSLog
(@
"Message = %@"
,strMessage);
135.
NSLog
(@
"Name = %@"
,strName);
136.
137.
138.
139.
NSUserDefaults
*prefs = [
NSUserDefaults
standardUserDefaults];
140.
141.
142.
[prefs setObject:txtID.text forKey:@
"ID"
];
143.
[prefs setObject:txtPassword.text forKey:@
"password"
];
144.
145.
[prefs synchronize];
146.
147.
148.
149.
if
( [strStatus isEqualToString:@
"1"
] ){
150.
151.
[
self
performSegueWithIdentifier:@
"LoginSuccess"
sender:
self
];
152.
153.
154.
}
155.
156.
157.
else
158.
{
159.
UIAlertView
*error =[[
UIAlertView
alloc]
160.
initWithTitle:@
"เกิดข้อผิดพลาด"
161.
message:strMessage delegate:
self
162.
cancelButtonTitle:@
"OK"
otherButtonTitles:
nil
];
163.
[error show];
164.
}
165.
166.
167.
}
168.
169.
170.
[connection release];
171.
[inputlogin release];
172.
173.
}
174.
175.
176.
- (
void
)didReceiveMemoryWarning
177.
{
178.
[
super
didReceiveMemoryWarning];
179.
180.
}
181.
182.
- (
void
)dealloc {
183.
[txtID release];
184.
[txtPassword release];
185.
[
super
dealloc];
186.
}
187.
188.
189.
190.
191.
@end