1

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Syed Ali Salman
  • 2,894
  • 4
  • 33
  • 48
  • What is the exact value of `urlString`? – rmaddy May 19 '15 at 05:08
  • 1
    `%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 May 19 '15 at 05:10
  • 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 May 19 '15 at 05:17
  • 1
    Add the following code: `NSLog(@"urlString = \"%@\"", urlString);` then update your question with the output from that log statement. – rmaddy May 19 '15 at 05:20
  • And where does this value come from? You obviously have a lot of extra strange characters in the value. Clean the value up. – rmaddy May 19 '15 at 05:21
  • yes, i have re Written my link, i do not know what actually happened to my string but this comment gave me idea that there is something invisible. rewriting solved my problem. was stuck in it for last 4 hours – Syed Ali Salman May 19 '15 at 05:23
  • 1
    %10 = "data link escape" according to http://www.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 May 19 '15 at 06:10
  • Your string is "too long by 12205 characters". Seems to contain loads of rubbish. I wouldn't worry about NSURL, but I would worry about the string. – gnasher729 May 19 '15 at 13:22
  • Yes, I figured out solution by this when I was updating my question post. Thanks all – Syed Ali Salman May 20 '15 at 05:01
  • how to remove %10 from url ? – Nik Dec 09 '15 at 15:31

3 Answers3

3

Use below code to resolve your problem.

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

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

NSURL* url = [NSURL URLWithString:webStringURL];
Bhavesh Patel
  • 596
  • 4
  • 17
0

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

Syed Ali Salman
  • 2,894
  • 4
  • 33
  • 48
  • So, it wasn't a _perfect string_ as stated in your question. You may use https://r12a.github.io/app-conversion/ to see invisible characters from a string. – Cœur Jan 17 '19 at 06:00
-2

Try below solution

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

Check it..!

Vidhyanand
  • 5,369
  • 4
  • 26
  • 59
  • No, `%10` is not "@", "@" is `%40`. You are reading the chart incorrectly. `%10` is the hex value 10 which is the decimal value 16. That is the "data link escape" control character. And the "solution" you posted in your answer is what the OP is already showing in their question. – rmaddy May 19 '15 at 16:07
  • @rmaddy See properly the question..It is having stringByAddingPercentEscapesUsingEncoding But in my answer I applied different i.e; stringByReplacingPercentEscapesUsingEncoding. Check properly before down vote... – Vidhyanand May 20 '15 at 06:29
  • @SyedAliSalman.. Check properly what you have tried.. U tried with stringByAddingPercentEscapesUsingEncoding but In my answer I used stringByReplacingPercentEscapesUsingEncoding.. Check properly and down vote...! – Vidhyanand May 20 '15 at 06:31
  • Using stringByReplacingPercentEscapesUsingEncoding is even more incorrect. The original string doesn't have any percent escapes to replace. – rmaddy May 20 '15 at 06:42