How to decode URL in objective c iphone? - objective-c

I was wondering if there is a method to decode a string that is in URL
encoded format?
this is my URL i want to decode it but i dont understand how to do this i tried multiple
ways
http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2%2C7
but didn't get the right response
Please anyone can help me out of this,
how can i decode this url i dont need % sign.
Thanks

By using the NSString method -(NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
Ex:
NSString *unencodedUrlString = [#"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2%2C7" stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Here's the not deprecated method : string​By​Removing​Percent​Encoding.
NSString *unencodedUrlString = [yourString string​By​Removing​Percent​Encoding];

Related

stringByAddingPercentEscapesUsingEncoding was deprecated in 9.0 .How to do this?

I'm a junior developer, and I got a code for this:
(NSString *)encodedStringFromObject:(id)object {
return [[object description] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
But in 9.0 have to use stringByAddingPercentEncodingWithAllowedCharacters.
How Could I transfer this code ?
I need help, Thanks!
If you want just fast example look at this code:
NSString * encodedString = [#"string to encode" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
Also check List of predefined characters sets
If you want explanation read the documents or at least this topic: How to encode a URL in Swift
URL = [[NSString stringWithFormat:#"%#XYZ",API_PATH]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
You'd read the spec for both functions. Then you would find out for what purpose characters are replaced with escape sequences. From there you would find out the set of allowed characters, which must be documented somewhere.
That's an essential part of changing from junior to normal to senior developer: Find out exactly what your code is supposed to do, which should be defined somewhere, and then make it do what it should do.
stringByAddingPercentEscapesUsingEncoding is probably deprecated because it doesn't know which characters are allowed and sometimes gives the wrong results.
You can use stringByRemovingPercentEncoding instead of stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding
[#"string" stringByRemovingPercentEncoding];
Here is solution to do in Swift
var encodedString = "Hello, playground".addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)

Required character in NSString is replaced with encoded variable

I have an NSString that contains a value "\U2212" instead of "-" which is coming from API. When I tried to replace this string with needed character using subString occurrence method it shown error. So how do I replace my NSString that contains "\U2212" with "-". I tried the following code. Please help me. I searched many things but nothing helped. Thanks in advance.
input:"(UTC\U221206:00) Canada/Central"
Desired output:"(UTC-06:00) Canada/Central"
code:
NSString *timezoneDisplayValue = [timezone valueForKey:#"tomeZoneDisplayValue"];
timezoneDisplayValue = [timezoneDisplayValue stringByReplacingOccurrencesOfString:#"\U2212" withString:#"-"];
below code returns YES or NO, whether your string has any encoding...
[apiString canBeConvertedToEncoding:NSASCIIStringEncoding];
If NO is returned, then convert with the correct encoding type :
[apiString dataUsingEncoding:NSUTF16BigEndianStringEncoding];

How do you pass in a String variable value to another string

Basically I did this and I got an error:
NSString *searchWord = #"Lilwayne";
NSString *resourceURL = (#"https://api.soundcloud.com/tracks?client_id=546952635e22cc0182d85daceff34381&q=%#&format=json", searchWord);
Error is:
reason: 'Resource 'Lilwayne' is invalid because the scheme is not 'https'.'
I don't understand why this doesn't work. However if I remove the "%#" and replace it with "Lilwayne" it works.
The reason why I am doing it this way is because I have a search feature in my app to search for songs using the soundcloud sdk and I want to dynamically change the value of the variable "searchword" to whatever the user typed in.
Try to use stringWithFormat
NSString *resourceURL = [NSString stringWithFormat:#"https://api.soundcloud.com/tracks?client_id=546952635e22cc0182d85daceff34381&q=%#&format=json", searchWord];
I suggest a trip to the NSString class reference in the Xcode help system. In addition to stringWithFormat, as suggested by Basheer, There is a section on combining strings.

Create request from URL with queryparams like NSDictionary

http://mobile.gunsel.ua/index.php?r=Rupages&jsonRequest={"id_page":"2"}
So,how can I create a NSURL(and after request) from this URL.(I use NSJSONSerilization).
I'm new to iOS development please help me. Thank You!
Don't offer links on apple documentation.
Not sure I get what you are asking, but you can use a string, then format it with your JSON (I.e concatenate the params), then create an NSUrl from that string.
stringWithFormat
Should do the trick.
I'm not sure if that answered your question, but you need to elaborate if that's not the case.
i'm found out!! This is was associated with Encoding)))
NSString sampleUrl = #"mobile.gunsel.ua/index.php?r=Rupages&jsonRequest={\"id_page\":\"2\"}";
NSString encodedUrl = [sampleUrl stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];

Removing unicode and backslash escapes from NSString converted from NSData

I am converting the response data from a web request to an NSString in the following manner:
NSData *data = self.responseData;
if (!data) {
return nil;
}
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((__bridge CFStringRef)[self.response textEncodingName]));
NSString *responseString = [[NSString alloc] initWithData:data encoding:encoding];
However the resulting string looks like this:
"birthday":"04\/01\/1990",
"email":"some.address\u0040some.domain.com"
What I would like is
"birthday":"04/01/1990",
"email":"some.address#some.domain.com"
without the backslash escapes and unicode. What is the cleanest way to do this?
The response seems to be JSON-encoded. So simply decode the response string using a JSON library (SBJson, JsonKit etc.) to get the correct form.
You can replace (or remove) characters using NSString's stringByReplacingCharactersInRange:withString: or stringByReplacingOccurrencesOfString:withString:.
To remove (convert) unicode characters, use dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES (from this answer).
I'm sorry if the following has nothing to do with your case: Personally, I would ask myself where did that back-slashes come from in the first place. For example, for JSON, I'd know that some sort of JSON serializer on the other side escapes some characters (so the slashes are really there, in the response, and that is not a some weird bug in Cocoa). That way I'd able to tell for sure which characters I have to handle and how. Or maybe I'd use some kind of library to do that for me.