0

I am a bit new to Xcode and iOS in general. I want to get data, specifically floating point data, from a url request and store it in an array. Example, I have the url:

http://www.hazewatch.unsw.edu.au/get-data.php?latitudes=-33.91714104308085&longitudes=151.2331476539389&datetimes=2015-05-13%2003:27:52&pollutants=co

If I enter that url in the browser, it shows up the text 'co=0.1124'. How can I grab this data (0.1124) into my program? This is what I wrote (below) but this is incorrect.

NSString *stringPollution = [NSString stringWithFormat:@"http://www.hazewatch.unsw.edu.au/get-data.php?latitudes=-33.91714104308085&longitudes=151.2331476539389&datetimes=2015-05-13%2003:27:52&pollutants=co"];
NSURL *urlPollution = [NSURL URLWithString:stringPollution];
NSData *pollutionData = [NSData dataWithContentsOfURL:urlPollution];

It seems like a simple task but I have no idea how to do it. Any help would be appreciated. Thanks!

Hank
  • 3
  • 3
  • Is the code you presented here incorrect only because you haven't taken it far enough, or because there's a problem with what you already have? Converting your `NSData` to `NSString` in this case is not difficult, see http://stackoverflow.com/questions/2467844/convert-utf-8-encoded-nsdata-to-nsstring. But... why is your question tagged php and Xcode? It has nothing to do with either. – mah May 16 '15 at 13:38
  • I guess I didn't take it far enough. Thanks anyway, it has been solved now. And you're right, I don't know why I put those tags in there. – Hank May 17 '15 at 04:48

2 Answers2

0

viewController.h

NSMutableData *responseData;
NSURLConnection *connection;

and add <NSURLConnectionDataDelegate>

ViewDidLoad method

NSString *urlString = @"http://www.hazewatch.unsw.edu.au/get-data.php?latitudes=-33.91714104308085&longitudes=151.2331476539389&datetimes=2015-05-13%2003:27:52&pollutants=co";
    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NO timeoutInterval:20.0f];


    responseData = [[NSMutableData alloc] init];
    connection = [NSURLConnection connectionWithRequest:request delegate:self];

ViewController.m

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"Your Data :%@",newStr);
}
Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
0

Its simple buddy just replace your code with the below code

NSString *stringPollution = [NSString stringWithFormat:@"http://www.hazewatch.unsw.edu.au/get-data.php?latitudes=-33.91714104308085&longitudes=151.2331476539389&datetimes=%@&pollutants=co",@"2015-05-13%2003:27:52"];
NSURL *urlPollution = [NSURL URLWithString:stringPollution];
NSData *pollutionData = [NSData dataWithContentsOfURL:urlPollution];

NSString *myString = [[NSString alloc] initWithData:pollutionData
                                           encoding:NSUTF8StringEncoding];


NSLog(@"%@",myString);
  1. Getting the url into String with specific format.
  2. Passing the string in the URL.
  3. Get the Data from the URL in NSData format.
  4. converting the NSData into String Format.
  5. Checking the output in the console with NSLog

Thanks

alex
  • 518
  • 3
  • 10