-2

I have tried using StringByAddingPercent but get the same results as shown.

Here is the error message I am getting: Error message

I have also tried string by adding percent, but it returns something like %2A and saves it in an SQL database instead of as Arabic words.

NSString* source = [NSString stringWithFormat:@"http://192.168.227.1/student/Service1.svc/insertTasbeeh/سُبْحَانَ اللّهِ/1000"];

NSURL *url = [NSURL URLWithString:source];
brimstone
  • 3,370
  • 3
  • 28
  • 49
Awais
  • 3
  • 3
  • 1
    see this once https://stackoverflow.com/questions/1981390/urlwithstring-returns-nil – Anbu.Karthik May 22 '17 at 13:09
  • Add percentencoding. – Cy-4AH May 22 '17 at 13:12
  • it gives me warning like... stringByAddingPercentEscapesUsingEncoding was deprecated in ios 9.0....even that i have do it but it returns me %2 etc like and saves (??) into sql database – Awais May 22 '17 at 13:18
  • @Awais - for your updated querry see this https://stackoverflow.com/questions/34015750/stringbyaddingpercentescapesusingencoding-was-deprecated-in-9-0-how-to-do-this – Anbu.Karthik May 22 '17 at 13:23
  • Printing description of url: http://192.168.227.1/student/Service1.svc/insertTasbeeh/%D8%B3%D9%8F%D8%A8%D9%92%D8%AD%D9%8E%D8%A7%D9%86%D9%8E%20%D8%A7%D9%84%D9%84%D9%91%D9%87%D9%90/1000 – Awais May 22 '17 at 13:44
  • i have do it but the results are same...url gives....Printing description of url: http://192.168.227.1/student/Service1.svc/insertTasbeeh/%D8%B3%D9%8F%D8%A8%D9%92%D8%AD%D9%8E%D8%A7%D9%86%D9%8E%20%D8%A7%D9%84%D9%84%D9%91%D9%87%D9%90/1000 – Awais May 22 '17 at 13:44
  • Possible duplicate of [URLWithString: returns nil](https://stackoverflow.com/questions/1981390/urlwithstring-returns-nil) – GilZ May 22 '17 at 14:22
  • Fixed a lot of strange formatting, but not exactly sure what he's saying – brimstone May 24 '17 at 16:27
  • @Anbu.Karthik : [need you here](https://stackoverflow.com/questions/44630350/touch-dont-respond-if-phone-in-locked-and-unlocked-again) – Fahim Parkar Jun 19 '17 at 12:50
  • @Anbu.Karthik ...its done thanks for your help...it was done with UTF8stringEncoding – Awais Jun 21 '17 at 06:35

4 Answers4

1
NSURL *url = [NSURL URLWithString:[@"http://192.168.227.1/student/Service1.svc/insertTasbeeh/سُبْحَانَ اللّهِ/1000" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

This should work.

iSaalis
  • 622
  • 1
  • 8
  • 14
  • Thank you. Swift version: self.url = "http://192.168.227.1/student/Service1.svc/insertTasbeeh/سُبْحَانَ اللّهِ/1000".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) – Maryoomi1 Jul 22 '19 at 07:36
1

Try this:

NSString *string = @"http://192.168.227.1/student/Service1.svc/insertTasbeeh/سُبْحَانَ اللّهِ/1000";

NSLog(@"url with string! %@", [NSURL URLWithString:string]);
NSLog(@"url with escaped string! %@", [NSURL URLWithString:
[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]); 

Hope this helps you !

Update for decoding url:

NSURL *url = [NSURL URLWithString:
              [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *myString = url.absoluteString; // or url.relativeString;

NSString* str = [myString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Sid Mhatre
  • 3,272
  • 1
  • 19
  • 38
  • Sir Thanks for help , but now kindly tell me how to decode it, e.g = url with escaped string! http://192.168.227.1/student/Service1.svc/insertTasbeeh/%D8%B3%D9%8F%D8%A8%D9%92%D8%AD%D9%8E%D8%A7%D9%86%D9%8E%20%D8%A7%D9%84%D9%84%D9%91%D9%87%D9%90/1000, Now what should i do, can i save that data in data base? or decode it and then save it, @siddheshMhatre – Awais May 22 '17 at 15:40
  • @Awais check for Update for decoding string – Sid Mhatre May 22 '17 at 16:17
0

Below line will do the magic

NSURL *url = [NSURL URLWithString:
    [source stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
];

You need to make encoding before making URL.

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • @Awais : don't post same link again and again... your path is `c:/` how come `c:` will open in iPhone? – Fahim Parkar May 22 '17 at 13:53
  • how can i show/upload my screenshot to show u plzzz help – Awais May 22 '17 at 13:53
  • @Awais : [chck this](https://www.google.com.kw/search?q=upload+png+files&oq=upload+png+files&aqs=chrome..69i57j0l5.6636j0j7&sourceid=chrome&ie=UTF-8) – Fahim Parkar May 22 '17 at 13:54
  • Sir Thanks for help , but now kindly tell me how to decode it, e.g = url with escaped string! 192.168.227.1/student/Service1.svc/insertTasbeeh/…, Now what should i do, can i save that data in data base? or decode it and then save it, @Fahim Parkar – Awais May 22 '17 at 15:43
0

Swift Version:

self.url = "http://192.168.227.1/student/Service1.svc/insertTasbeeh/سُبْحَانَ اللّهِ/1000".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

Maryoomi1
  • 113
  • 1
  • 3
  • 16