Too many arguments to method call ,expected 1 have 2 - objective-c

I'm calling a JSON URL from the NSURL class. My URL is:
like://Design_Time_Addresses/IServices/RoleService/json/Role/?name=%#",[textField text]];
When I write "doctor" in the textfield I have to get all the details of doctor through that URL. Similarly when I write "engineer" I have to get all the details of engineer. So it means what I type in the textfield, the name in URL should be replaced with the textfield value. But when I write the code like this:
NSURL *jsonUrl =[NSURL URLWithString:#"http://Design_Time_Addresses/ICloudServices/RoleService/json/Role/?name=%#",[textField text]];
NSString *jsonStr =[[NSString alloc] initWithContentsOfURL:jsonUrl];
NSMutableDictionary *jsonDetails = [[NSMutableDictionary alloc]initWithDictionary:[jsonStr JSONValue]];
As shown here I am using the NSURL class to get this URL.
I am getting an error in the NSURL class. How do I pass the text fields value to a URL?

First line should be
NSString *urlString = [NSString stringWithFormat:#"http://Design_Time_Addresses/ICloudServices/RoleService/json/Role/?name=%#",[textField text]];
NSURL *jsonUrl =[NSURL URLWithString:urlString];
NSURL URLWithString expects a single string argument, while you are providing two. Since you probably want to construct a single string from the #"http://..." string and [textField text], you should use an NSString method to concatenate them. You can't rely on NSURL to concatenate the string for you.

Related

xCode using variables

I'm pretty new to Objective C in general...
I'm wondering how I can use a *variable in the view controller, and add it onto the web view URL. Bellow is a UIWebViewer, It loads "site.com/something.php"... Along with that I would like for it to addon to the URL "?uuid=(UUID Variable here)".
Sorry... I'm more used to PHP/Perl coding where you can just throw in "$uuid"...
Thanks,
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *UUID = [[NSUUID UUID] UUIDString];
NSURL *myURL = [NSURL URLWithString:#"http://www.site.com/something.php?uuid="];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
}
It's very simple you just need to create a property to assign whatever value you want to it
ViewController.m
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *uuid = [[NSUUID UUID] UUIDString]; // Convention says that UUID should uuid
// All we need to do now is add the uuid variable to the end of the string and we can do
// that by using stringWithFormat:
NSURL *myURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.site.com/something.php?uuid=%#", uuid]];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
}
Check the documentation of NSString and the class method stringWithFormat:
Here's what you're looking for:
NSString *UUID = [[NSUUID UUID] UUIDString];
NSString *urlstr = [NSString stringWithFormat:
#"http://www.site.com/something.php?=%#", UUID];
NSURL *myURL = [NSURL URLWithString:urlstr];
NSString's stringWithFormat: method allows you to build strings from literal strings and varaibles. Variables are added to the literal string using format specifiers. Most of the format specifiers are the same as all the other C-based languages, %d for integer types, %f for floating types, %c for char, etc.
In the case of Objective-C, the %# is used as the place holder for objects that respond to the description selector, which returns a string. (In the case of an NSString, it just returns the string itself, but you'll notice that you can put lots of other types of objects in here too... in fact, every object that inherits from NSObject has a default description method).

stringByReplacingOccurrencesOfString not working?

The first NSLog returns the correct URL After that I get this error.
2013-07-06 21:07:56.622 Social App[69682:14003] -[NSURL stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x9a4ae10
videoURL = [[tableData objectAtIndex:indexPath.row] objectForKey:#"streamURL"];
NSLog(#"URL; %#", videoURL);
videoURL = [videoURL stringByReplacingOccurrencesOfString:#"http://www.youtube.com/watch?v=" withString:#""];
videoURL = [videoURL stringByReplacingOccurrencesOfString:#"&feature=youtube_gdata_player" withString:#""];
NSLog(#"URL; %#", videoURL);
Any help is greatly appreciated.
On the third line it seems you are sending a NSString message to a NSURL. That is why the second NSLog says [NSURL stringBy...] and unrecognized selector, since the NSURL class does not have a method named stringBy....
What you have to do is insert this line between the second and third lines:
NSString* videoString = [videoURL absoluteString];
And then substitute videoURL with videoString on the lines where you call stringBy....
Use videoURL.absoluteString.
Right now you are trying to use a NSString method ( stringByReplacingOccurrencesOfString ) on a NSURL, that of course won't work. absoluteString will get you a NSString to work with.
videoURL is a NSURL
use:
stringUrl = [videoURL absoluteString];
then use stringurl for further operations like stringByReplacingOccurrencesOfString

objective c: getting xml file with querystring

So Ive built an XML parser using the tutorial here: http://www.codeproject.com/Articles/248883/Objective-C-Fundamentals-NSXMLParser
My problem is that the XML file I am calling requires a querystring parameter. An example url would be something like:
http://domain.com/xml.php?id=00idcode00
Without the id= the XML file just returns a single XML element <error>No id specified</error>; with an id specified the XML file returns 50 odd XML elements.
My problem is that no matter what I try the XML file always returns the <error>. How do I get NSURL to use the querystring?
Here is my code:
NSString *someId = #"00idcode00";
NSString *urlString = [NSString stringWithFormat:#"http://domain.com/xml.php?id=%#", someId];
NSURL *url = [NSURL URLWithString:urlString];
parser=[[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
NSLog(#"query: %#",[url query]);
And this NSLogs the following:
Started element error
Found an element named: error with a value of: No id specified.
query: id=00idcode00
The last line where I output the query of url implies that the NSURL has the right querystring, but the response of the XML file suggests that NSURL is not using the querystring.
Can anybody help?
Build the string first, then the URL.
NSString *someId = #"00idcode00";
NSString *urlString = [NSString stringWithFormat:#"http://domain.com/xml.php?Id=%#", someId];
NSURL *url = [NSURL URLWithString:urlString];
And the error states the query param is Id, not id.

How do I add format specifiers to NSURL

I have the following code that makes a Google Places API request. The parameters are statically set at the moment. How would I go about making those parameters (types and lat/lon and the Google Key - which I've defined as a constant in the .h file) objects instead?
My problem arises with the NSURL because I can't add format specifiers to it.
thanks for any help.
-(void)ParseXML_of_Google_PlacesAPI
{
NSURL *googlePlacesURL = [NSURL URLWithString:#"https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=bar&sensor=false&key=MyGoogleAPIKey"];
NSData *xmlData = [NSData dataWithContentsOfURL:googlePlacesURL];
xmlDocument = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil];
NSArray *arr = [xmlDocument.rootElement elementsForName:#"result"];
for(GDataXMLElement *e in arr )
{
[placesOutputArray addObject:e];
}
Good ol' stringWithFormat?
`NSString* urlToCall = [NSString stringWithFormat:#"http:://url.to.webservice/api?param1=%#&param2=%#", param1, param2]`
This may be helpful for you
float lat=34.0522222,lon=-118.2427778;
NSString *typestr=#"bar";
NSString *key=#"MyGoogleAPIKey";
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&types=%#&sensor=false&key=%#",lat,lon,typestr,key]];
NSLog(#"url values ==%#",url);
Format specifiers directly to NSURL like this:
NSURL *googlePlacesURL = [NSURL URLWithString:[NSString stringWithFormat:#"http:://url.to.webservice/api?param1=%#%param2=%#", param1, param2]];

How to download file from particular url?

NSURL * url = #"http://192.168.100.161/UploadWhiteB/wh.txt";
NSData * data = [NSData dataWithContentsOfURL:url];
if (data != nil) {
NSLog(#"\nis not nil");
NSString *readdata = [[NSString alloc] initWithContentsOfURL:(NSData *)data ];
I write this code to download a file from given url... but i get an error on line
NSData * data = [NSData dataWithContentsOfURL:url];
uncaught exception...
so please help me out.
Your first line should be
NSURL * url = [NSURL URLWithString:#"http://192.168.100.161/UploadWhiteB/wh.txt"];
(NSURL is not a string, but can easily be constructed from one.)
I'd expect you to get a compiler warning on your first line--ignoring compiler warnings is bad. The second line fails because dataWithContentsOfURL: expects to be given a pointer to an NSURL object and while you're passing it a pointer that you've typed NSURL*, url is actually pointing to an NSString object.
NSString *file = #"http://192.168.100.161/UploadWhiteB/wh.txt";
NSURL *fileURL = [NSURL URLWithString:file];
NSLog(#"qqqqq.....%#",fileURL);
NSData *fileData = [[NSData alloc] initWithContentsOfURL:fileURL];
-[NSString initWithContentsOfURL:] is deprecated. You should be using -[NSString (id)initWithContentsOfURL:encoding:error:]. In either case, the URL paramter is an NSURL instance, not an NSData instance. Of course you get an error trying to initialize a string with the wrong type. You can initialize the string with the URL data using -[NSString initWithData:encoding:], or just initialize the string directly from the URL.