Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-03-2012, 02:09 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2012
Posts: 3
soonw29 is on a distinguished road
Default Retrieve parameter from didReceiveRemoteNotification,load json data into tableview

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;
}
soonw29 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 399
17 members and 382 guests
Atatator, chiataytuday, dre, FrankWeller, imac74, ipodphone, jeroenkeij, kukat, LunarMoon, MAMN84, n00b, QuantumDoja, reficul, Retouchable, Sami Gh, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,675
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Retouchable
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:10 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0