NSURL object value is null - ios

I am generating a URL as given by string parameters but url gives null value.
For generating URL I have implemented the following code .
NSURL *url = [[NSURL alloc] init];
NSString *strURL = #"ftp://Administrat:ABC(R%-#TRDOP#xx.xx.xx.xx/arrows.png";
url = [NSURL URLWithString:[NSString stringWithString:strURL]];
NSLog(#"URL :: %#",url);
Thanks

You need to escape the special characters in the url query string.
Use:
NSString *strURL = #"ftp://www.jerox.com/Administrator:#123#TRDOP#%$/arrows.png";
strURL =[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strURL];
NSLog(#"URL :: %#",url);
Also I need to mention some mistakes in your code:
No need to allocate and init the NSURL object, because you are again assigning another object to that pointer
No need of using stringWithString: there

Make use of the following code.
NSString *sURL = #"ftp://www.jerox.com/Administrator:#123#TRDOP#%$/arrows.png";
NSURL *url = [[NSURL alloc] initWithString:[sURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"URL :: %#",url);

From your posted code sample, here is the problem:
NSString *strURL = #"ftp://Administrat:ABC(R%-#TRDOP#xx.xx.xx.xx/arrows.png";
Look carefully at the authority component (username, password and host). An # symbol is used in URLs to separate username & password from the host. Because your password contains an # character, it must be percent escaped. The percent encoding of # is %40, giving you instead the code:
NSString *strURL = #"ftp://Administrat:ABC(R%-%40TRDOP#xx.xx.xx.xx/arrows.png";
You really ought to be escaping other URL-specific characters in there too, like the lone % symbol.

Related

NSString to NSURL returns false

I have a URL which is in a string: http://bs.serving-sys.com/BurstingPipe/adServer.bs?cn=is&c=23&pl=VAST&pli=10226041&PluID=0&pos=6670&ord=%5B1423164382.84%5D&cim=1
I am trying to convert that string to NSURL.
I've tried several things, which are:
NSString *encodedString = [#"http://bs.serving-sys.com/BurstingPipe/adServer.bs?cn=is&c=23&pl=VAST&pli=10226041&PluID=0&pos=6670&ord=%5B1423164382.84%5D&cim=1" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:encodedString];
When I follow the URL object with a breakpoint it assigns 0 => false.
What should I do to convert that string to NSURL?
The parameters in your given URL string are already URL encoded. So:
NSString *string = #"http://bs.serving-sys.com/BurstingPipe/adServer.bs?cn=is&c=23&pl=VAST&pli=10226041&PluID=0&pos=6670&ord=%5B1423164382.84%5D&cim=1";
NSURL *url = [NSURL URLWithString:string];
// url is not nil

How to decode NSURL in iOS?

i am NewBie in iOS Development. I want to Decode my NSURL With Pagination Here my Url Like as
http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%B8%E0%AB%8D%E0%AA%B5%E0%AA%BE%E0%AA%B8%E0%AB%8D%E0%AA%A5%E0%AA%AF&page=0
And i Decode this URL Like as
NSURL *urls = [NSURL URLWithString:#"http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%B8%E0%AB%8D%E0%AA%B5%E0%AA%BE%E0%AA%B8%E0%AB%8D%E0%AA%A5%E0%AA%AF&page=0"];
NSString *urlString = [urls absoluteString];
NSLog(#"UrlString %#",urlString);
NSURL * url=[NSURL URLWithString:urlString];
But it Give me Only 0 Page Url i want to Like as make my url as
NSURL *urls=[NSURL URLWithString:#"My Url &page=%d",pagenum];
Here i want at place of My Url as Decoding of my Original Url If it is Possible then Please Give me Solution for it.
you are doing it in wrong way.
this is the right way
NSString *urlString = #"http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%B8%E0%AB%8D%E0%AA%B5%E0%AA%BE%E0%AA%B8%E0%AB%8D%E0%AA%A5%E0%AA%AF&page=";
NSURL *targetURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#%d",urlString,pageNumber]];
NSLog(#"targetURL is : %#",targetURL);
Hope this will help you..
NSString *str=#"http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%B8%E0%AB%8D%E0%AA%B5%E0%AA%BE%E0%AA%B8%E0%AB%8D%E0%AA%A5%E0%AA%AF&page=0";
NSString *result =[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *urls = [NSURL URLWithString:result] ;

Fetching JSON with %20 in the link

I have variable date=#"2014-06-18 12:59:46"
I do the following:
NSString* formated_date = [[Constants shared].date stringByReplacingOccurrencesOfString:#" " withString:#"%%20"];
which gives me the wanted output of the new-formed variable: "2014-06-18%2012:59:46".
Now, when I put all that into string, which represents the url, I cannot fetch JSON from within the app.
BUT, when i NSLog the generated url, and copy paste it in browser - it works.
Also, if i take, c/p the Logged url, and hardcode it into the json request, I get the demanded json just as intended.
This is my code so far:
NSString* formated_date = [[Constants shared].date stringByReplacingOccurrencesOfString:#" " withString:#"%%20"];
NSString* url = [NSString stringWithFormat:
#"http://api.fessor.da.kristoffer.office/homework?rest&_rp[date]=%#&_rp[uuid]=%#&_rp[workspace]=parents&_rp[token]=%#&_rp[user_id]=%#&child_id=%#&type=parents&start_date=%#&end_date=%#",
formated_date,[Constants shared].uuid,[Constants shared].token,[Constants shared].user_id, [Constants shared].user_id,[Constants shared].start_date, [Constants shared].end_date
];
NSLog(url);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
//Load the json on another thread
[Constants shared].jsonDataHomework = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:url"]];
dispatch_async(dispatch_get_main_queue(), ^{
[self getPresentHomework];
});
});
[self getPresentHomework] then returns an error, since the data from the json is nil.
I repeat once again, i get nil data when the code for fetching json is like this:
[Constants shared].jsonDataHomework = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:url]];
but if i hardcode the url, then i get the desired result
[Constants shared].jsonDataHomework = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:#"http://api.fessor.da.kristoffer.office/homework?rest&_rp[date]=2014-06-18%2012:59:46&_rp[uuid]=289A6F6F-BB71-444A-B16B-DCAF0070E1D3&_rp[workspace]=parents&_rp[token]=7fe3768108445570f11bf333cb821b0bee9213d2cced91a1f63f8c648fbc3e6a&_rp[user_id]=22066&child_id=22066&type=parents&start_date=2014-06-18&end_date=2014-06-18"]];
What am I doing wrong?
Should I change the order of threads or something like that?
Try to append your parameters to the URL string without adding percentage and then use stringByAddingPercentEscapesUsingEncoding before assigning it to your NSURL.
NSString *urlString = [NSString stringWithFormat:
#"format your url"];
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
The URL string contain special character so use
NSString *strUrl=[#"http://api.fessor.da.kristoffer.office/homework?rest&_rp[date]=2014-06-18%2012:59:46&_rp[uuid]=289A6F6F-BB71-444A-B16B-DCAF0070E1D3&_rp[workspace]=parents&_rp[token]=7fe3768108445570f11bf333cb821b0bee9213d2cced91a1f63f8c648fbc3e6a&_rp[user_id]=22066&child_id=22066&type=parents&start_date=2014-06-18&end_date=2014-06-18" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

too many arguments to method call expected 1 have 3

Please help me to find the error, my code is
NSURL *url = [NSURL URLWithString: #"http://www.xxxx.com/xxxx_webservice/login.php?user=%#&pass=%#&format=json",Username,Password];
Do it like this. First make a proper string of your URL and then encode it and then finally make it a NSURL. Make sure your passing the correct and right set of parameters.
NSString *link = [NSString stringWithFormat:#"http://www.example.com/xx_webservice/login.php?user=%#&pass=%#&format=json",Username,Password];
NSString *encdLink = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:encdLink];

NSURL - Set modify path

I need to use a NSURL object to reach different ressources on the same host.
Here is what I do:
#define MY_HOST #"my.server.eu"
NSURL *url = [[NSURL alloc] initWithScheme:#"http" host:MY_HOST path:#"/"];
Now I need to deal with
http://my.server.eu/path1
http://my.server.eu/path2
http://my.server.eu/path3
How can I modify the path of my NSURL object ?
Why can't we simply do url.path = #"path1" ?
How can I modify the path of my NSURL object ?
Why can't we simply do url.path = #"path1" ?
Because NSURL is an immutable object, and you can't change its properties afterwards. NSMutableURL does not exist, but is on the wish list of many.
In order to achieve what you're after, you're going to have to make 3 separate NSURL objects I'm afraid. To do so, you can convenience the paths within an array:
NSString *host = #"http://my.server.eu/";
NSArray *paths = #[#"path1", #"path2", #"path3"];
NSURL *path1 = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#", host, path[0]]];
NSURL *path2 = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#", host, path[1]]];
NSURL *path3 = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#", host, path[2]]];
You should make the base URL as you're doing and then build the others relative to it using +[NSURL URLWithString:relativeToURL:].

Resources