How to use special character in NSURL?
Swift 2
let original = "http://www.geonames.org/search.html?q=Aïn+Béïda+Algeria&country="
if let encodedString = original.stringByAddingPercentEncodingWithAllowedCharacters(
NSCharacterSet.URLFragmentAllowedCharacterSet()),
url = NSURL(string: encodedString)
{
print(url)
}
Encoded URL is now:
"http://www.geonames.org/search.html?q=A%C3%AFn+B%C3%A9%C3%AFda+Algeria&country="
and is compatible with NSURLSession
.
Swift 3
let original = "http://www.geonames.org/search.html?q=Aïn+Béïda+Algeria&country="
if let encoded = original.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
let url = URL(string: encoded)
{
print(url)
}
How to use special characters \n in URL
strUrl = strUrl.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
let url = URL.init(string: strUrl)
let requestURL = URLRequest(url: url!)
- You need to add addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) method to allow URL Query.
deal with special characters in NSURL
Your URL is malformed. Right now you have:
http:\\www.school-link.net\\uploads\\%@
Those backslashes should instead be forward slashes. Ex:
http://www.school-link.net/uploads/%@
Now you can append image_url
to the URL, and presuming image_url
doesn't cause the URL to be malformed, your request will go through.
NSUrl swift 2 with greek and other special characters
For encode α
in your url
let str = "α"
let url = str.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
Now decode url string like this
let orgStr = url?.stringByRemovingPercentEncoding
print(orgStr)
How to handle special characters in url as parameter values?
Use URLEncoder to encode your URL string with special characters.When encoding a String, the following rules apply:
- The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.
- The special characters ".", "-", "*", and "_" remain the same.
- The space character " " is converted into a plus sign "+".
- All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented
by the 3-character string "%xy", where xy is the two-digit
hexadecimal representation of the byte. The recommended encoding
scheme to use is UTF-8. However, for compatibility reasons, if an
encoding is not specified, then the default encoding of the platform
is used.
For example using UTF-8 as the encoding scheme the string The string ü@foo-bar
would get converted to The+string+%C3%BC%40foo-bar
because in UTF-8 the character ü is encoded as two bytes C3 (hex) and BC (hex), and the character @ is encoded as one byte 40 (hex).
Related Topics
What Does an Exclamation Mark Mean in the Swift Language
How to Easily Duplicate/Copy an Existing Realm Object
Swift Structs to Nsdata and Back
Using "If Let..." With Many Expressions
Why Is Equatable Not Defined For Optional Arrays
How to Convert Array of Bytes [Uint8] into Hexa String in Swift
In Swift 4, How to Remove a Block-Based Kvo Observer
Swiftui 2: the Way to Open View in New Window
Trapping Signals in a Swift Command Line Application
How to Lock Orientation of One View Controller to Portrait Mode Only in Swift
How Does One Generate a Random Number in Swift
How to Get the Current Date in Short Format in Swift
Difference Between 'Let' and 'Var' in Swift
Swift - Sort Array of Objects With Multiple Criteria
How to Create a Reference Cycle Using Dispatchqueues
How to Parse Json With Decodable Protocol When Property Types Might Change from Int to String