0

I am new to iOS and working on an app which runs on a real device (iPad). So, when I launch my app on the iPad after the view is visible, the app should be able poll a web server or something (without any user interaction) and get some information over HTTP and based on this information, I want fill some text fields in the app view. can you let me know if it is possible to do something like this in iOS? if so how and some sample pieces of code would be much appreciated.

Thanks.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Teja
  • 21
  • 1

1 Answers1

0

You can download information over http using NSURLConnection in the viewWillAppear or viewDidLoad. After download the data if its XML parse using NSXMLParser (or any other XML parser for iOS).

//Lets say you have download and process method
- (void)downloadAndProcess
{
    //URL you want to download Info from
    NSURL* url = [NSURL URLWithString:@"http://google.com"];
    //Make a mutable url request
    NSMutableURLRequest* req = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
    NSURLConnection* conn = [NSURLConnection connectionWithRequest:req delegate:self];
    if(conn)
    {
        //NSMutableData receivedData is an instance variable
        receivedData = [[NSMutableData alloc] init];
    }
}

//NSURLConnection Delegate methods here
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Error downloading data :%@",[error localizedDescription]);
    // release receivedData object when connection fails
    [receivedData release],receivedData = nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Connection did finish downloading data which you can process based on what your data is
    // release receivedData object once you are done processing it.
    [receivedData release],receivedData = nil;
}
0x8badf00d
  • 6,391
  • 3
  • 35
  • 68
  • 1
    thanks for the reply, both the methods viewWillAppear and viewDidLoad are actually called before the app is ready for user interaction. But is there a way to initiate this process when a particular view is loaded(we can actually see the app view in device) and ready for user interaction. So when ever this particular view is loaded in the app it will always poll for the information and keep updating the fields on the page. correct me if I interpreted ur answer wrongly. – Teja Sep 09 '11 at 21:04
  • If you are in ViewController B ( where your app has view controllers A,B,C loaded on Nvigation controller where A is root view controller of nav controller. From A they can navigate to B and C, From B or C they can navigate back to A.) Do you want to start polling if B view controller is loaded onto nav controller whether or not if that is the current view user is looking at A or C ? is that what you want – 0x8badf00d Sep 09 '11 at 21:14
  • No, consider the scenario I launched my app and the first(root) view is A now I want to poll for the information after this polling action user can go to B,C ... and whenever he comes back to A, again I have to poll for this info. sorry if I got you confused – Teja Sep 09 '11 at 21:19
  • Implement the above code in View controller A in viewWillAppear i.e., [self downloadAndProcess];, and while NSURLConnection is downloading from your server and then we are processing it... during that show some activity indicator on the screen showing that some activity is in progress and users have to wait till thats complete to go further to B or C vc's. – 0x8badf00d Sep 09 '11 at 21:23