Hi all,
I am a newbie to iOS development. I am developing an iPad app to receive push notifications from the server and pass a parameter taken from the push notification payload to access json data hosted by my server. Afterwhich, display the information grabbed into a tableView. Currently i am able to retrieve the information from the json file but i am facing difficulties in loading the data into the tableview. Kindly read through my code and assist me. THanks.
Appdelegate.m
Code:
-(void)application: (UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
for (id key in userInfo){
NSLog(@"key: %@,value: %@", key, [userInfo objectForKey:key]);
}
NSLog(@"Parameter is %@",[userInfo objectForKey:@"acme"]);
param = [userInfo objectForKey:@"acme"];
NSString *urlString = [NSString stringWithFormat:@"http://api.kivaws.org/v1/loans/%@",param];
NSLog(@"Url is %@",urlString);
// Create NSURL string from urlString
NSURL *url = [NSURL URLWithString:urlString];
// Setup and start async download
request = [[NSURLRequest alloc] initWithURL:url];
theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
[request release];
//NSString *urlString = (@"http://192.168.20.4:8180/RfWms/json/%@",[userInfo valueForKey:@"acme"]);
//AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
//[[NSNotificationCenter defaultCenter]postNotificationName:@"updateRoot" object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[appdelegate.receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[appdelegate.receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// release the connection, and the data object
//[connection release];
//[receivedData release];
// inform the user
// NSLog(@"Connection failed! Error - %@ %@",
//[error localizedDescription],
//[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
// Store incoming data into a string
NSString *jsonString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
// Create a dictionary from JSON string
faculties = [[jsonString JSONValue]objectForKey:@"loans"];
NSLog(@"faculties is %@",faculties);
// NSArray* latestLoans = [(NSDictionary*)[responseString JSONValue] objectForKey:@"loans"];
[jsonString release];
// release the connection, and the data object
[connection release];
[receivedData release];
//[viewController tableView:<#(UITableView *)#> numberOfRowsInSection:<#(NSInteger)#>];
[viewController.theTableView reloadData];
}
//-(void)updateView:(NSNotification *)notification{
// [theTableView reloadData];
//}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
NSLog(@"Registering for push notifications...");
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
return YES;
}
Viewcontroller.m
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
return [appdelegate.faculties count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
NSDictionary *faculty = [appdelegate.faculties objectAtIndex:[indexPath row]];
//NSLog(@"faculty is",faculty);
[[cell textLabel] setText:[faculty objectForKey:@"name"]];
NSString *image = [faculty objectForKey:@"image"];
//NSLog(@"Image is %@",image);
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:image]];
UIImage *theImage = [[UIImage alloc]initWithData:imageData];
cell.imageView.image = theImage;
[theImage release];
[imageData release];
return cell;
}