How to decode NSURL in iOS? - 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] ;

Related

Pinterest Invalid Image URL format

Hi everyone I am trying to integrate pinterest into my app. I am giving the below url for image url but pinterest throwing error saying invalid image url format.
This is my image url.
http://www.healthandglow.com:8080/osafe_theme/images/catalog/products/large/503277_1.jpg
- (void)pinIt:(id)sender
{
NSURL *imageURL = [NSURL URLWithString:#"http://www.healthandglow.com:8080/osafe_theme/images/catalog/products/large/503277_1.jpg"];
NSURL *sourceURL = [NSURL URLWithString:#"http://www.healthandglow.com"];
[pinterest createPinWithImageURL:imageURL
sourceURL:sourceURL
description:#"Pinning from Pin It Demo"];
}
As it was showing invalid image url,but was working when I tried it on browser. I thought of shortening the image url and give a try and that trick worked out for me.And I am using tinyurl api which I have used in code.
Here my code goes
NSString *yourURL = [NSString stringWithFormat:#"%#%#",kBaseImageUrl,product.SKU_ImageUrls];
NSURL *sourceURL = [NSURL URLWithString:#"http://healthandglow.in"];
NSString *apiEndpoint = [NSString stringWithFormat:#"http://tinyurl.com/api-create.php?url=%#",urlstr];
NSLog(#"\n\n APIEndPoint : %#",apiEndpoint);
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiEndpoint]
encoding:NSASCIIStringEncoding
error:nil];
NSLog(#"Long: %# - Short: %#",urlstr,shortURL);
NSURL *imageURL = [NSURL URLWithString:shortURL];
[pinterest createPinWithImageURL:imageURL
sourceURL:sourceURL
description:[NSString stringWithFormat:#"%#",product.SKU_Name]];

While setting NSString to NSURL, NSURL is returning nil value

I have to parse the return of a JSON API for which I've taken the URL into a string and after that I passed it NSURL but in NSLog it is showing nil.
Here is my code,
NSString *urlstring=[NSString stringWithFormat:#"http://api.v3.factual.com/t/places?q=%#&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[self.strurl lowercaseString]];
NSURL *url=[[NSURL alloc]initWithString:urlstring ];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
Thanks in advance.
You must encode your url as it contains special characters, Try something like this
NSString *paramString=[NSString stringWithFormat:#"/t/places?q=%#&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[self.strurl lowercaseString]];
NSString *encodedSearchString = [paramString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"http://api.v3.factual.com%#", encodedSearchString];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(#"urlstring is %#",url)
Hope this helps
Try this, Tested and Working
-(void) sample
{
// NSString *strurl = #"hello";
NSString *urlstring=[NSString stringWithFormat:#"http://api.v3.factual.com/t/places?q=%#&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[strurl lowercaseString]];
NSURL *url=[NSURL URLWithString:[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSLog(#"%#",req);
}

webview won't load with string

In my webview viewdidload i'm implementing the following code. the problem is the webview won't appear when I include the NSString *encodedString. I have searchString with spaces and sometimes &. Any ideas on what's going on here?
NSString *urlString = [NSString stringWithFormat:#"http://google.com?q=%#", searchString];
NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,(CFStringRef)urlString,
NULL,(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8 ));
NSLog(#"searchString is %#", searchString);
NSURL *myURL =[NSURL URLWithString:encodedString];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[searchWebView loadRequest:myRequest];
What would it take to generate the first page results of google for this searchString?
Try This
NSString *urlString = [NSString stringWithFormat:#"http://www.google.com/search?q=%#",searchString];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *myURL =[NSURL URLWithString:urlString];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[searchWebView loadRequest:myRequest];
Use this to encode Your SearchString
SearchString=[[NSString stringWithFormat:#"%#", SearchString] stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];
and then append it to your URL

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