How to set Input Parameters when making Service Call - ios

So I'm working on a small example on how to make a service call to a specific web service. I'm using the openweathermap.org web service. The link to this service is the following:
http://api.openweathermap.org/data/2.5/weather?q=London,uk
According to this link, I can search weather by city name. So I'm able to retrieve and NSLog the JSON data with the following code:
NSString *urlString = [NSString stringWithFormat:WXFORECAST, LOCATION];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(#"%#", json);
This does the bare minimum and retrieves the service. Now I would like to get my specific city instead of London.
If not, I would like to get JUST THE WEATHER part instead of getting the wind speed, and all that other garbage.
Here is the link for details on the API:
http://openweathermap.org/API#weather
Under that link there's a section that says this:
Restriction output:
To limit number of listed cities please setup cnt parameter api.openweathermap.org/data/2.5/find?lat=57&lon=-2.15&cnt=3
I believe this might be what I'm looking for but I don't know exactly how to use it...
All help is appreciated, thanks.

The API is accessed with a URL, try playing with this in your browser:
http://api.openweathermap.org/data/2.5/weather?q=New+York,us
See how I have a plus sign between "New" and "York"? That's to make sure the URL is valid, because they don't allow for spaces.
You have to figure out a way to get the URL you want in your variable. In your code, it's creating the URL using a format string stored in WXFORECAST. So, update that.
I don't have time to read through all of how that API works, but it's possible that you can't request that it gives you less information. But there's nothing stopping you from taking only what you need from it. It's all in that JSON dictionary, if you wanted to get the temperature your code might look like this.
NSArray *list = json[#"list"];
NSDictionary *london = list[0];
NSDictionary *main = london[#"main"];
NSString *temp = main[#"temp"];

Related

How to cache JSON web response locally on iOS - objective c

I am building a mobile iOS app for a web backend. I retrieve the JSON response using the following code:
NSError *error;
NSString *url_string = [NSString stringWithFormat: #"https://myURL"];
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:url_string]];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
What would be the best/simplest way to store this data locally so that I can use a local database when internet connectivity is not available? The intention is to check web connectivity at launch of the app. If connection is available, get JSON response, parse and update local copy, if connection is not available parse the data from local storage.
Thanks in advance.
If you wish to cache the JSON data so you can still use it offline, I would write the JSON dictionary (or the data) to a file in your app's sandbox. A subfolder of the "Application Support" folder would be a good place. You don't want to use the Caches folder in this case because the files could be purged by iOS when you need them offline.
The trick is to map a given URL to a filename. You need this mapping to both save a file for a given URL and to later load the file if offline. You should be able convert a URL to a useful filename simply by converting all / characters to something else such as an underscore.
You probably don't want these files backed up when a user backups their iOS device so be sure you mark the files with the "do not backup" attribute. There are many existing question covering that topic.
The best way is CoreData and
the simplest way is NSUserDefaults
NSUserDefaults Class Reference
[[NSUserDefaults standardUserDefaults] setObject: jsonDict forKey:#"dictionaryKey"];
//...
NSDictionary * myDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:#"dictionaryKey"];

Need to process a request to youtube inside an iOS app

If I take a browser and type http://www.youtube.com/get_video_info?video_id=$id&el=embedded&ps=default&eurl=&gl=US&hl=en into it, I get a bunch of text describing video with ID $id in a form of a file, that gets downloaded by a browser. The file contains a large string that is unique every time the request is performed.
Now, I need to gain access to that giant string inside iOS app.
Could you please tell me where to start digging? UIWebViews? Or maybe there's a simple solution?
Thanks in advance.
NSURL *url = [NSURL URLWithString:#"https://www.youtube.com/get_video_info?video_id=$id&el=embedded&ps=default&eurl=&gl=US&hl=en"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *idString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

How can I get the content of the version string property of my latest app version available in the App Store using Swift/ iOS?

Dear stack overflow community,
I wish to get the content of the version string property of the latest version of my iOS app available in the app store, based on the App ID, using Swift.
Basically I want to change the following Objective-C code to relate on the App ID instead of the bundle ID and I wish to translate it to Swift:
NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString* bundleID = infoDictionary[#"CFBundleIdentifier"];
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:#"http://itunes.apple.com/lookup?bundleId=%#", bundleID]];
NSData* data = [NSData dataWithContentsOfURL:url];
NSDictionary* lookup = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if ([lookup[#"resultCount"] integerValue] == 1){
NSString* appStoreVersion = lookup[#"results"][0][#"version"];
}
While I should be able to translate the code to swift pretty easily I don't know yet how to make it depend on the App ID instead of the bundle ID. Any help appreciated. Thanks
I'm not sure which field you mean when you say "App ID" but the way you can find the answer is to open your info.plist file in Xcode, right click in the file content area, and choose Show Raw Keys/Values.
You'll see the key CFBundleIdentifier that represents the bundle id. Find the corresponding key that matches the "App ID" that you want to use and substitute it in the code you're going to convert. (CFBundleExecutable maybe?)

Connect this NSSting with a String Variable

All I want is to change every time the NSString townLocation.
Because I take data from an API and I don't want to create different API for different location. Also I know that the "+" that I put on the link is not correct and there is not such think in Objective C but I want to make you understand what I want.
NSString*townLocation;
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://api.openweathermap.org/data/2.5/find?q="+townLocation+"&units=metric"]];
How I must do it ? Im sure you understand that I'm new at Objective C
Thank you
You only need to look into the most basic NSString documentation to find a method that will do that, stringWithFormat:.
NSString *urlString = [NSString stringWithFormat:#"http://api.openweathermap.org/data/2.5/find?q=%#&units=metric", townLocation];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
If you're new at Objective-C, a good place to find information like this is to simply search the internet or the iOS Developer Library for the class in question (in this case, NSString) to find a myriad of resources at your disposal. Another doc to check would be Formatting String Objects, which is linked in the stringWithFormat: section of the iOS Developer Library, to find more info about formatting strings.

Write content of NSURL to an array

I have a program that retrieves data from a link and i write it out to the Log like this.
NSURL *getURL=[NSURL URLWithString:#"link.php"];
NSError *error=nil;
NSString *str=[NSString stringWithContentsofURL:getURL encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%",str);
This prints to the log the three values from my php as expected.
However I am having a little difficulty saving this in an array which then displays it those values in a UISplitviewController (the leftcontroller side).
which is written like this
showArray=[[NSMutableArray alloc]initWithContentofURL:getURL];
then in cellForRowAtIndexPath: method is
cell.textLabel.text=[showArray object atIndex:indexPath.row];
A second thing i have tried is write myURL to an array and tried to initlize showArray with ContentsofArray like this
NSArray *retults=[NSArray arraywithContentsOFURL:getURL];
showArray=[[NSArray alloc]initWithArray:retults];
but THAT dont work
BUT if i say
showArray=[[NSMutableArray alloc]initWithObjects:#"One",#"Two",nil];
One and two shows in my leftview controller....
Would love is someone could help me with this...Thank you
Are you trying to add the contents of the URL or the URL itself ?
If you are trying to just add the URL, then use :
showArray = [#[getURL] mutableCopy];
However, if you are trying to add the contents of the URL, then the doc clearly states that the URL must represent a string representation of an array.
Furthermore :
Returns nil if the location can’t be opened or if the contents of the location can’t be parsed into an array.
EDIT :
I saw your comment on your post and your data looks like JSON data.
You should take a look at the NSJSONSerialisation class which is pretty straightforward to use (you'll find lots of example here on SO).
U have done web services perfectly, now wat u have to do is parse it to an array
First download the SBJSON files in this link
https://github.com/stig/json-framework/
Then, copy them to your workspace.
Then, in the viewController add this
#import "SBJson.h"
Your JSON data contains values in the form of dictionary
SO, to parse them
SBJsonParser * parser=[SBJsonParser new];
NSDictionary * jsonData=(NSDictionary *)[parser objectWithString:outputData];
NSArray * arr=(NSArray *)[NSDictionary objectForKey:#"animal"];
I think this will help

Resources