Cyrillic symbols in URL
Try this:
let encodedUrl = jsonUrl.stringByAddingPercentEncodingWithAllowedCharacters(URLQueryAllowedCharacterSet)
Problem With Cyrillic Characters as URL Parameter
Thanks to this comment I have managed to recieve a properly formatted response.
The thing is that I'm not using two important parameters in the URL:
ie=UTF-8
oe=UTF-8
The URL should look like this:
https://translate.googleapis.com/translate_a/single?client=gtx&sl=BG&tl=EN&dt=t&q=Здравей%20Свят!&ie=UTF-8&oe=UTF-8
Fastest way to encode cyrillic letters for url
The function you're searching for is encodeURIComponent
.
encodeURIComponent("Беларусь");
// returns "%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%8C"
Its counterpart is decodeURIComponent
which reverses this process.
decodeURIComponent("%D0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D1%8C");
// returns "Беларусь"
Loading url with cyrillic symbols
I have a list of urls which contents cyrillic.
OK, if it contains raw (not %-encoded) Cyrillic characters that's not like the example, and in fact it isn't a URL at all.
An address with non-ASCII characters in it is known as an IRI. IRIs shouldn't be used in an HTML link, but browsers tend to fix up these mistakes.
To convert an IRI to a URI which you can then open with urllib
, you have to:
encode non-ASCII characters in the hostname part using Punycode (IDNA).
encode non-ASCII characters in rest of the IRI to UTF-8 bytes and URL-encode them (resulting in
%D0%BF...
like in the example URL).
an example implementation.
Using Russian characters in URL query
The reason of the runtime error is you unwrap an optional instance of the NSURL
, which is actually nil
.
The reason of urls
is nil
is searchurl
string contains invalid characters (outside of the 7-bit ASCII range). To be used in URL that characters should be percent-encoded.
Swift 2 (I guess you are using that version):
let encodedTexts = texts.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
if let encodedTexts = encodedTexts {
let searchurl = "http://sngpoisk.ru/search-location/?search_keywords=\(encodedTexts)&search_location=&place_location=&latitude=&longitude="
let urls = NSURL(string:searchurl)
if let urls = urls {
let ret = NSURLRequest(URL:urls)
Browser!.loadRequest(ret)
}
}
Swift 3:
let encodedTexts = texts.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
if let encodedTexts = encodedTexts {
let searchurl = "http://sngpoisk.ru/search-location/?search_keywords=\(encodedTexts)&search_location=&place_location=&latitude=&longitude="
let urls = URL(string:searchurl)
if let urls = urls {
let ret = URLRequest(url:urls)
Browser!.loadRequest(ret)
}
}
Problem with cyrillic characters in friendly url
The text is just being encoded to fit the specification for URLs.
Echo out the data to a log to see what you are actually trying to pass to the database.
You should be able to decode it with urldecode.
Related Topics
Map Object into 2D Array Swift for Tableview Sections
Why Wkwebview Doesn't Display Videos? - Swift 3
Swift 4.2 Make Bottom of Tableview Move Up When Keyboard Shows
Get All Ranges of a Substring in a String in Swift
How to Get Index of Xcuielement in Xcuielementquery
Cocoapods Page Redirecting to Github
Filemanager and Urlsfordirectory Error in Swift 3 Xcode 8
Using '!' Here Is Deprecated and Will Be Removed in a Future Release - Swift 4.2
Populating Input Text Field in Wkwebview
How to Sort Core Data Array Based on Date Entered
Using String.Characterview.Index.Successor() in for Statements
How to Print Name of The Day of The Week
Xcode UI Test Uikeyinput Typetext
Xcode6 Goldmaster Submitting Build Failed with Error
How to Get Path to a Subfolder in Main Bundle
Bluetooth Didconnect or Didfailtoconnect Not Called Xcode 9.2 with iOS 11.1.2
How to Change Brightness, Contrast and Saturation Using Cicolorcontrols via Uislider with Swift