NSURL returns Nil Value - objective-c

Here Below is my code
NSString *string = [NSString stringWithFormat:#" http://abc.com /Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#& ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];
Here in string there is value but url returns nil.
Can Anyone tell why this happened.
Thanks ....
"This won't work, so here's what I did instead"
NSString *string = [NSString stringWithFormat:#"http://abc.com/Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];

NSURL will return nil for URLs that contain illegal chars, like spaces.
Before using your string with [NSURL URLWithString:] make sure to escape all the disallowed chars by using [NSString stringByAddingPercentEscapesUsingEncoding:].
Here is the class reference for NSString.

Hi All Now My Problem Is solved here I did a mistake left a blank space before "http".
The correct code is
NSString *string = [NSString stringWithFormat:#"http://myserver.com/Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];

Related

Contents from url to string

NSString *urlAddress = [NSString stringWithFormat:#"http://www.domain.com?input=%#",[alertView textFieldAtIndex:0].text];
NSURL *myUrl = [NSURL URLWithString:[urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *openURL = [NSString stringWithContentsOfURL:myUrl encoding:NSUTF8StringEncoding error:NULL];
NSLog(#"%#",openURL);
openUrl is always returning (null), probably because of the encoding, but I don't know how to fix it.
Double check these three things:
Check that the URL which you are pointing to is a valid URL. The null value could be a result of an invalid URL.
Catch the NSError which the stringWithContentsOfURL throws. That will give you some insight on what is wrong.
Like you suspect, try changing the string encoding.
NSError *error;
NSString *urlString = [NSString stringWithFormat:#"http://www.domain.com?input=%#", stringVariable];
NSURL *urlAdress = [NSURL URLWithString:urlString];
NSString *urlContent = [[NSString alloc] initWithContentsOfURL:urlAdress encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#", urlContent);
This works fine, the problem was an failing internet connection.

How to append a variable to a string in xcode

Im trying to to append a variable to a string but its showing an error. Im missing something ridiculously easy here but my mind is shot.
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSURL *urla = [NSURL URLWithString:#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=",eID];
You can't concat 2 string variables just by putting a comma between them. Try this instead:
NSURL *urla = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=%#",eID]];
If all you are doing is appending, you have several options:
A. Use NSString & stringWithFormat:
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *urlString = [NSString stringWithFormat:#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=%#", eID];
NSURL *urla = [NSURL URLWithString:urlString];
B. Use NSString & stringByAppendingString:
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *baseUrl = #"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSString *urlString = [baseUrl stringByAppendingString:eID];
NSURL *urla = [NSURL URLWithString:urlString];
C. Use NSMutableString & appendString:
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *baseUrl = #"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSMutableString *urlString = [NSMutableString stringWithString:baseUrl];
[urlString appendString:eID];
NSURL *urla = [NSURL URLWithString:urlString];
Try like this:-
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *url=#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSURL *urla = [NSURL URLWithString:[url stringByAppendingString:eID];

Why do I get this error when I try to pass a parameter in NSURL in iOS app?

This is what I have in a public method - (IBAction)methodName
NSString *quoteNumber = [[self textBox] text];
NSURL *url = [[NSURL alloc] initWithString:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber];
The error I get is:
Too many arguments to method call, expected 1, have 2
What am I doing wrong?
I think you are thinking of NSString's stringWithFormat::
[NSURL URLWithString:[NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%#", quoteNumber]]
Also note the change to %# for the format specifier, since it is an instance of NSString (not an int)
You need to format your string. Try this:
NSString *urlString = [NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%#", quoteNumber];
NSURL *url = [[NSURL alloc] initWithString:urlString];
The initWithString method can only accept a normal NSString, you are passing it a formatted NSString, Take a look at this code:
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quotedNumber]];
That might be a bit confusing, you can break it up as follows:
NSString *urlString = [NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quotedNumber];
NSURL *url = [[NSURL alloc] initWithString:urlString];
Now your string is properly formated, and the NSURL initWithString method will work!
Also, just so it is clearer for you in the future, you can take advantage of Objective-C's dot notation syntax when you set your quoteNumber string, as follows:
NSString *quoteNumber = self.textBox.text;
Also, you are trying to pass this quoted number into your urlString as a digit (as seen with the %d), remember that quotedNumber is a NSString object, and will crash when you try to pass it to the stringWithFormat method. You must convert the string first to a NSInteger, or NSUInteger.
Please refer to this SO question to how to do that (don't worry it's very easy)!
The problem is
[NSURL initWithString:]
requires ONE parameter of NSString type but you passed TWO parameters .
You need to pass a single NSString parameter . Change your code from
NSURL *url = [[NSURL alloc] initWithString:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber];
to
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber]];

Convert NSData to a NSURL

I have seen this done the other way. But I have a NSData object returned that is suppose to be a URL of an image file.
How do I do this convert a NSData object into a NSURL?
Thanks
You first need to convert the NSData object to an NSString object and then you can just create a NSURL with the new string.
NSString *urlString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding]; // Or any other appropriate encoding
NSURL *url = [[NSURL alloc] initWithString:[urlString autorelease]];

Append NSString to NSURL?

I have an NSURL, a file path, and I want to add an NSString to the end of it (the file name) how can I do this? But after this is don't I want the entire thing to be an NSURL.
Thanks.
I think it's good solution:
NSURL *bUrl = [aUrl URLByAppendingPathComponent:#"newString"];
In Swift you could do the following,
var bURL = aURL.URLByAppendingPathComponent( "newString" )
You can also state whether the URL is a directory,
var bURL = aURL.URLByAppendingPathComponent( "newString", isDirectory: true )
I think it's as simple as:
NSString *s = [aUrl.path stringByAppendingString:#"newString"];
If you have a file NSURL to a directory and you want to end up with a NSString containing the NSURL's path with a file name appended to it, use this:
NSURL *url = [NSURL fileURLWithPath:#"/System" isDirectory:YES];
NSString *filename = #"foo";
NSString *result = [url.path stringByAppendingPathComponent:filename];
You can also use URLByAppendingPathComponent but that adds an extra step which creates an extra NSURL object that isn't needed.
NSURL *url = [NSURL fileURLWithPath:#"/System" isDirectory:YES];
NSString *filename = #"foo";
NSURL *newURL = [url URLByAppendingPathComponent:filename];
NSString *result = newURL.path;