I have problem with NSURL and Thailand language for example
"http://www.xyp.com?var=ไทย"
it does't work but if you use
"http://www.xyp.com?var=Thai"
it ok. I don't know how to solve this problem anyone please help me.
R – NSURL with Thai language
nsurlthai
Related Solutions
Xcode 8 or later • Swift 3 or later
Synchronously:
if let filePath = Bundle.main.path(forResource: "imageName", ofType: "jpg"), let image = UIImage(contentsOfFile: filePath) {
imageView.contentMode = .scaleAspectFit
imageView.image = image
}
Asynchronously:
Create a method with a completion handler to get the image data from your url
func getData(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
}
Create a method to download the image (start the task)
func downloadImage(from url: URL) {
print("Download Started")
getData(from: url) { data, response, error in
guard let data = data, error == nil else { return }
print(response?.suggestedFilename ?? url.lastPathComponent)
print("Download Finished")
// always update the UI from the main thread
DispatchQueue.main.async() { [weak self] in
self?.imageView.image = UIImage(data: data)
}
}
}
Usage:
override func viewDidLoad() {
super.viewDidLoad()
print("Begin of code")
let url = URL(string: "https://cdn.arstechnica.net/wp-content/uploads/2018/06/macOS-Mojave-Dynamic-Wallpaper-transition.jpg")!
downloadImage(from: url)
print("End of code. The image will continue downloading in the background and it will be loaded when it ends.")
}
Extension:
extension UIImageView {
func downloaded(from url: URL, contentMode mode: ContentMode = .scaleAspectFit) {
contentMode = mode
URLSession.shared.dataTask(with: url) { data, response, error in
guard
let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() { [weak self] in
self?.image = image
}
}.resume()
}
func downloaded(from link: String, contentMode mode: ContentMode = .scaleAspectFit) {
guard let url = URL(string: link) else { return }
downloaded(from: url, contentMode: mode)
}
}
Usage:
imageView.downloaded(from: "https://cdn.arstechnica.net/wp-content/uploads/2018/06/macOS-Mojave-Dynamic-Wallpaper-transition.jpg")
You are trying to load a file from your file system, not from web.
For creating the NSURL
you need to use fileURLWithPath: class method.
Change your method like:
Swift 2
override func viewDidLoad()
{
super.viewDidLoad()
var urlpath = NSBundle.mainBundle().pathForResource("bpreg", ofType: "xml")
let url:NSURL = NSURL.fileURLWithPath(urlpath!)!
parser = NSXMLParser(contentsOfURL: url)!
parser.delegate = self
parser.parse()
}
Swift 3
override func viewDidLoad()
{
super.viewDidLoad()
let urlpath = Bundle.main.path(forResource: "bpreg", ofType: "xml")
let url = NSURL.fileURL(withPath: urlpath!)
parser = XMLParser(contentsOf: url)!
parser.delegate = self
parser.parse()
}
Note: In Swift 3 you can also use the URL class to construct the url instead of NSURL
class. So the above code for constructing url changes to:
let url = URL(fileURLWithPath: urlpath!)
Best Answer
i used the method stringByAddingPercentEscapesUsingEncoding on the URL string, and it solved the problem, i was using Danish Characters