How to make NSURL variable is nil when parsed from null String? - ios

I'm a newbie in iOS development. Sometimes my API server returns a nil value. This is raising an error when I try to create a NSURL instance.
Here is the condition:
NSString *tmpURL = nil;
NSURL *url = [NSURL URLFromString:tmpURL];
In this condition, it will make the app crash. I just want to make the url variable nil, not raise an error.

How about:
NSString *tmpURL = nil;
NSURL *url = tmpUrl ? [NSURL URLFromString:tmpURL] : nil;

Related

NSURL URLWithString returns nil

I am stuck in a very obvious method of NSURL class URLWithString I am passing a string and while creating URL it returns nil, my string is perfect. When ever I uses same string in browser then it is working fine. I am using following method to convert NSString to NSURL:
NSURL *url = [NSURL URLWithString:urlString];
//urlString is my perfect string
I have also tried to encode my string first by using following
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// using this line my output string when I log url is "url%10%10%10%10%10%10..."
%10 becomes the suffix of my url with around 100+ repetition.
If any one has idea what %10 is or what can be done to overcome this problem.
here is my urlString:
Use below code to resolve your problem.
NSString *str = msgDetail[#"thumbnail"];
NSString* webStringURL = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:webStringURL];
yes what was wrong in my string were many unidentified characters due to copying from internet site, if anyone of the reader facing this issue can copy and paste your string as see the count. And confirm.
Thanks
Try below solution
NSURL *url = [NSURL URLWithString:[urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Check it..!

Retrieving Text from URL in iOS

Can someone please help me understand why my output is "myContent is equal to (null)" with this code:
NSURL *myURL = [NSURL URLWithString: #"http://moneyfromapps.com/wp-content/uploads/2015/01/codetest.txt"];
NSString *myContent = [NSString stringWithContentsOfURL: myURL encoding: NSStringEncodingConversionAllowLossy error: nil];
NSLog(#"myContent is equal to %#", myContent);
It seems to work with other URLs... Any idea why not with this URL?
It's those ...'s in the URL
http://moneyfromapps.com/wp-conte…/uploads/…/01/codetest.txt"];
Also, always check the NSError you get back from the call. The website might be down.

IDE error: Variable undefined but defined in an if statement

I am defining the variable NSURL *url in a series of else if's where there cannot be an error so it will always be defined (I can add error catching later just in case).
This is a horrible way of doing this but im fairly new to the language.
if (whatTime == #"1000") {
NSURL *url = [NSURL URLWithString:#"http://yyy/json.php?f=1&time=1000"];
}else if(whatTime == #"1100"){
NSURL *url = [NSURL URLWithString:#"http://yyy/json.php?f=1"];
}else if(whatTime == #"1100"){
NSURL *url = [NSURL URLWithString:#"http://yyy/json.php?f=1"];
}
However the debugger is telling me url is undefined. Is there a way to get around this?
When I do (above the hell-pit of ifs) NSURL *url; it runs but says it is not connected to the internet.
For the undefined problem, you have to declare NSUrl *url outside the if/elses:
NSURL *url = nil;
if (whatTime == #"1000") {
url = [NSURL URLWithString:#"http://yyy/json.php?f=1&time=1000"];
}else if(whatTime == #"1100"){
url = [NSURL URLWithString:#"http://yyy/json.php?f=1"];
}else if(whatTime == #"1100"){
url = [NSURL URLWithString:#"http://yyy/json.php?f=1"];
}
I think the not connected problem is not related to that code.
The local variables declared inside the if and else if clauses are only visible inside those scopes and therefore you can't use them after the last else if. By declaring NSURL *url; before the if it will be visible after the last else if since it is in the same scope.
The "not connected to the internet" error is probably not related to this code. Probably your device/simulator has no connection.

How does one append something like "?x=123" to an NSURL?

I have an NSURL of the form
"http://abc.def.com:1234/stuff/"
and I want to append to it so the final url is like this
"http://abc.def.com:1234/stuff/?x=123".
But if I do this
url = [NSURL URLWithString:#"http://abc.def.com:1234/stuff/?"];
url = [url URLByAppendingPathComponent:#"x=123"];
Then the result is
http://abc.def.com:1234/stuff/x=123?
(its the same result if URLByAppendingPathExtension is used).
But if I do this
url = [NSURL URLWithString:#"http://abc.def.com:1234/stuff/?"];
url = [url URLByAppendingPathComponent:#"x=123"];
Then the result is
http://abc.def.com:1234/stuff/%3Fx=123
(also the same result if URLByAppendingPathExtension is used).
Neither of which is what I am after. How do I get a final result of "http://abc.def.com:1234/stuff/?x=123" ?
I think the simplest answer here is to create a new URL using the old one as the base URL.
url = [NSURL URLWithString:#"?x=123" relativeToURL:url]
Here is a unit test that checks the logic
- (void)testCreatingQueryURLfromBaseURL
{
NSURL *url = [NSURL URLWithString:#"http://foo.com/bar/baz"];
url = [NSURL URLWithString:#"?x=1&y=2&z=3" relativeToURL:url];
STAssertEqualObjects([url absoluteString], #"http://foo.com/bar/baz?x=1&y=2&z=3", nil);
}
Create your actual NSURL object last, from an NSString that you build up as needed:
NSString *urlString = #"http://www.site.com/";
if (some condition)
urlString = [urlString stringByAppendingString:#"?x=123"];
NSURL *url = [NSURL URLWithString:urlString];

NSURL URLWithString the url is always nil

My code is like this :
NSString *urlStr = [NSString stringWithFormat:#"https://xxx.xxx.xx.xx/Pages/Service/FMService.svc/FileAudit?user=%#&pass=%#&fileId=%d&isAudited=1&opinion=%#",appDelegate.user.userName,appDelegate.user.password,self.todo.fileId,self.opinion];
NSLog(#"URL=%#",urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
And the url = (NSURL *) 0*00000000#"< nil >"
how can I work it out? thank you in advance!
For debugging purposes, I would build up urlStr piece by piece instead of combining five NSStrings all at once. Put in an NSLog(#"%#", urlStr) statement after each addition to urlStr. This will reveal which piece of the puzzle is causing the problem.

Resources