001.
002.
003.
004.
005.
006.
007.
008.
009.
#import "showViewController.h"
010.
011.
@interface
showViewController ()
012.
{
013.
NSMutableArray
*myObject;
014.
015.
016.
NSDictionary
*dict;
017.
018.
019.
NSString
*device_id;
020.
NSString
*ip_address;
021.
NSString
*mac_address;
022.
NSMutableData
*receivedData;
023.
UIAlertView
*loading;
024.
}
025.
026.
@end
027.
028.
@implementation
showViewController
029.
030.
- (
void
)viewDidLoad
031.
{
032.
[
super
viewDidLoad];
033.
034.
035.
036.
device_id = @
"device_id"
;
037.
ip_address = @
"ip_address"
;
038.
mac_address = @
"mac_address"
;
039.
040.
041.
myObject = [[
NSMutableArray
alloc] init];
042.
043.
044.
NSURLRequest
*theRequest =
046.
cachePolicy:
NSURLRequestReloadIgnoringLocalCacheData
047.
timeoutInterval:10.0];
048.
049.
NSURLConnection
*theConnection=[[
NSURLConnection
alloc] initWithRequest:theRequest delegate:
self
];
050.
051.
052.
053.
[
UIApplication
sharedApplication].networkActivityIndicatorVisible =
YES
;
054.
055.
loading = [[
UIAlertView
alloc] initWithTitle:@
""
message:@
"Please Wait..."
delegate:
nil
cancelButtonTitle:
nil
otherButtonTitles:
nil
];
056.
UIActivityIndicatorView
*progress= [[
UIActivityIndicatorView
alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
057.
progress.activityIndicatorViewStyle =
UIActivityIndicatorViewStyleWhiteLarge
;
058.
[loading addSubview:progress];
059.
[progress startAnimating];
060.
[loading show];
061.
062.
if
(theConnection) {
063.
receivedData =
nil
;
064.
}
else
{
065.
UIAlertView
*connectFailMessage = [[
UIAlertView
alloc] initWithTitle:@
"NSURLConnection "
message:@
"Failed in viewDidLoad"
delegate:
self
cancelButtonTitle:@
"Ok"
otherButtonTitles:
nil
];
066.
[connectFailMessage show];
067.
068.
}
069.
}
070.
071.
- (
void
)connection:(
NSURLConnection
*)connection didReceiveResponse:(
NSURLResponse
*)response
072.
{
073.
receivedData = [[
NSMutableData
alloc] init];
074.
}
075.
076.
- (
void
)connection:(
NSURLConnection
*)connection didReceiveData:(
NSData
*)data
077.
{
078.
sleep(2);
079.
[receivedData appendData:data];
080.
}
081.
082.
- (
void
)connection:(
NSURLConnection
*)connection didFailWithError:(
NSError
*)error
083.
{
084.
085.
086.
087.
UIAlertView
*didFailWithErrorMessage = [[
UIAlertView
alloc] initWithTitle: @
"NSURLConnection "
message: @
"didFailWithError"
delegate:
self
cancelButtonTitle: @
"Ok"
otherButtonTitles:
nil
];
088.
[didFailWithErrorMessage show];
089.
090.
091.
092.
NSLog
(@
"Connection failed! Error - %@"
, [error localizedDescription]);
093.
094.
}
095.
096.
- (
void
)connectionDidFinishLoading:(
NSURLConnection
*)connection
097.
{
098.
099.
[
UIApplication
sharedApplication].networkActivityIndicatorVisible =
NO
;
100.
[loading dismissWithClickedButtonIndex:0 animated:
YES
];
101.
102.
if
(receivedData)
103.
{
104.
105.
106.
107.
108.
id
jsonObjects = [
NSJSONSerialization
JSONObjectWithData:receivedData options:
NSJSONReadingMutableContainers
error:
nil
];
109.
110.
111.
for
(
NSDictionary
*dataDict in jsonObjects) {
112.
NSString
*strdevice_id = [dataDict objectForKey:@
"Device_id"
];
113.
NSString
*strip_address = [dataDict objectForKey:@
"IP_Address"
];
114.
NSString
*strmac_address = [dataDict objectForKey:@
"MAC_Address"
];
115.
116.
dict = [
NSDictionary
dictionaryWithObjectsAndKeys:
117.
strdevice_id, device_id,
118.
strip_address, ip_address,
119.
strmac_address, mac_address,
120.
nil
];
121.
[myObject addObject:dict];
122.
}
123.
124.
[myTable reloadData];
125.
}
126.
127.
128.
129.
}
130.
131.
- (
NSInteger
)tableView:(
UITableView
*)tableView numberOfRowsInSection:(
NSInteger
)section
132.
{
133.
134.
int
nbCount = [myObject count];
135.
if
(nbCount == 0)
136.
return
1;
137.
else
138.
return
[myObject count];
139.
140.
}
141.
142.
- (
UITableViewCell
*)tableView:(
UITableView
*)tableView cellForRowAtIndexPath:(
NSIndexPath
*)indexPath
143.
{
144.
145.
static
NSString
*CellIdentifier = @
"Cell"
;
146.
147.
UITableViewCell
*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
148.
if
(cell ==
nil
) {
149.
150.
cell = [[
UITableViewCell
alloc] initWithStyle :
UITableViewCellStyleSubtitle
151.
reuseIdentifier : CellIdentifier];
152.
}
153.
154.
int
nbCount = [myObject count];
155.
if
(nbCount ==0)
156.
cell.textLabel.text = @
"Loading Data"
;
157.
else
158.
{
159.
NSDictionary
*tmpDict = [myObject objectAtIndex:indexPath.row];
160.
161.
162.
NSMutableString
*text;
163.
text = [
NSMutableString
stringWithFormat:@
"Device ID : %@"
,[tmpDict objectForKey:device_id]];
164.
165.
166.
NSMutableString
*detail;
167.
detail = [
NSMutableString
stringWithFormat:@
"MAC : %@ , IP : %@"
168.
,[tmpDict objectForKey:mac_address]
169.
,[tmpDict objectForKey:ip_address]];
170.
171.
cell.textLabel.text = text;
172.
cell.detailTextLabel.text= detail;
173.
174.
175.
176.
177.
}
178.
return
cell;
179.
}
180.
181.
182.
- (
void
)didReceiveMemoryWarning
183.
{
184.
[
super
didReceiveMemoryWarning];
185.
186.
}
187.
@end