1
votes

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:
comment says too many character but it is obvious that there are not much characters

3
What is the exact value of urlString? - rmaddy
%10 is a hex code. That's 16 in decimal which is a strange control character. Show where urlString comes from and again, show its exact value. - rmaddy
please see my edited answer, i have posted an image which showing my url string in comment and site is saying too many characters - Syed Ali Salman
Add the following code: NSLog(@"urlString = \"%@\"", urlString); then update your question with the output from that log statement. - rmaddy
%10 = "data link escape" according to w3schools.com/tags/ref_urlencode.asp. Did you try using stringByTrimmingCharactersInSet:? Btw, if you've already found a solution (based on your comments) I think you could post your solution to help others in the future. - Islam Q.

3 Answers

3
votes

Use below code to resolve your problem.

NSString *str = msgDetail[@"thumbnail"];

NSString* webStringURL = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];

NSURL* url = [NSURL URLWithString:webStringURL];
0
votes

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

-2
votes

Try below solution

NSURL *url = [NSURL URLWithString:[urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Check it..!