Swift: string url encoding not working as expected - ios

So I have a url like this:
let remoetURL = "http://xxx-test.img-cn-hangzhou.aliyuncs.com/materials/talk_-XXXXX-XXXXXXX/STEM RULE.pdf"
As you can see, at then end of the url, there is a white space, so I need to get rid of it to have a valid encoded url.
After doing some research, I realized I might use
let escapedString = remoteURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet())
But this does not returned the expected working url, because it encodes the ":" after "http" too
http%3A//xiaobandeng-staging.img-cn-hangzhou.aliyuncs.com/talk_materials/talk_-K4yjX4-238Ku74WVIJk/STEM%20RULE.pdf
I have also tried URLHostAllowedCharacterSet, but no luck. So I wonder if it is because I don't have www here, so it does not recognise which part is the host correctly. If so, what might be the elegant solution? I know I could replace white spaces with %20 by calling stringByReplacingOccurrencesOfString, but that seems just a bit fragile.
Thank you in advance.

Try this used by SwiftyJSON
let urlString = remoteURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
Swift 3:
let urlString = remoteURL.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

Have you try this
let urlPath = NSString(format: remoetURL).stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
For iOS 9
let encodedHost = NSString(format: remoetURL).stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
Hope this will help you

Related

URL error char set? [duplicate]

This question already has answers here:
Swift - encode URL
(19 answers)
Closed 5 years ago.
I tried to call a custom url using this code below:
let myurl = "https://myserver.com/call?a|b|c"
let converted = URL(string: myurl)
print(converted)
But what I'm getting as result in converted is just "nil".
I'm pretty sure this is because of the wrong characters set in relation to the URL() class.
After some research all I got so far is this outdated Swift code:
var myurl = "https://myserver.com/call?a|b|c"
var newurl = myurl.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())
print(newurl)
But it doesn't seems working this way.
How can I achieve to avoid the "nil" result using (in my case) Swift 4?
Found the solution on my own:
let myurl = "https://myserver.com/call?a|b|c"
let converted = URL(string: myurl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
print(converted!)
The new function is called addingPercentEncoding & I have to call it with the urlQueryAllowed property.
The urlQueryAllowed charset adds some characters like this to the url encoding: "!*'();:#&=+$,/?%#[]|"
VoliĆ !

Why can't I convert this String to a URL?

I have x 2 questions about urls and webViews.
Question 1:
I have a string which I am getting from an API that is supposed to be a url. The string is https://godochurch.blob.core.windows.net/sermons/1031/30-1-2017-Vision Sunday-Devotion.mp3
When trying to convert to a url I'm getting nil.
Here is the code:
if let sermonUrl = sermonUrl {
if let url = URL(string: sermonUrl) {
let requestObj = URLRequest(url: url)
webView.loadRequest(requestObj)
}
}
I have worked out that the space between 'Vision' and 'Sunday' is the problem.
Should I be encoding the string in some way before trying to convert it to a URL? What's confusing is that if I paste the string into my browser it works just fine, but I notice the browser is percent encoding the space.
If I am supposed to be encoding the string, how do I do that?
Question 2:
I see that URL(string: "urlStringHere") is only available from iOS 10. My app needs to work for iOS 9. How can I convert the above code so it works on iOS 9 and 10.
Thanks in advance for your time.
1: escape that space with %20 or +:
if let url = URL(string: "https://godochurch.blob.core.windows.net/sermons/1031/30-1-2017-Vision%20Sunday-Devotion.mp3") {
// ...
}
2: URL was introduced with Swift 3, which is compatible with iOS 8 and later. See this question on how to make it work with older versions of iOS.
Edit: the easiest way to percent-escape a URL is to use URLComponents:
var components = URLComponents(string: "https://godochurch.blob.core.windows.net")!
components.path = "/sermons/1031/30-1-2017-Vision Sunday-Devotion.mp3"
if let url = components.url {
// ...
}
If you happen to get you URL strings from a webservice and it contains a space, that service is bad. Space is not allowed in an URL. Web browsers relax that rule because they know there are bad URLs out there. Swift won't take it.

AEXMLDocument loadXMLData() not working in Swift

I'm working with AEXML to write and read xml documents in Swift. I have the writing working no problem. And I have everything setup for the reading, but I can't seem to turn the saved text xml into the document object. It only ever gets the first element and none of the children. I've tried removing all the lines and spaces but still nothing. The content is reading into the String just fine and I've tried converting the data back to a string and it isn't getting messed up in conversion. Is this even possible with AEXML or am I just doing it wrong?
let doc = AEXMLDocument()
let content = try String(contentsOf:NSURL(string:file) as! URL)
let data = content.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
let xml = NSString(data:data, encoding:String.Encoding.utf8.rawValue)
try doc.loadXMLData(data)
So I figured out that I was actually using an outdated version of AEXML which clearly wasn't working anymore. The updated code looks like this.
let content = try String(contentsOf:NSURL(string:file) as! URL)
let data = content.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
let options = AEXMLOptions()
let doc = try AEXMLDocument(xml:data,options:options)

Choose specific url without writing a lot of if statement

I'm working on an horoscope application now I have a problem after user selected is zodiac how can I know what php url to show him for example:
there is 12 zodiac's so how can I give him his horoscope for this url for example
NSURL: "blablabla_horscope.com/libra/today_horoscope"
I mean I don't want to write in Xcode twelve options of url, and start making if statements for example "if this user and then use here is NSUserDefaults with his choosed zodiac"
since then I will have to start making a list of a lot of if statement and it will make my app work slow and it's a bad idea so I have thinking about making some kinda of php file, that will handle all that after the user registered in the background but where to start for that?
I'm currently working on Swift, core-data, php.
let urlString = "http://www.blablabla/?sign=libra&time=today"
let url = NSURL (string: urlString)
let dataURL = try? NSData(contentsOfURL: url!, options: [])
let result: String = String(data: dataURL!, encoding: NSUTF8StringEncoding)!
print("the result is %#", result)
Set your constant for the sign type prior to setting your urlString.
let sign = "libra"
or
let sign = "cancer"
etc...
and then you can:
let urlString = "http://www.blablabla/?sign=\(sign)&time=today"
Late

'NSURLRequest?' does not have a member named 'URL' - Swift

Hi I am really new to coding in Swift, and am trying to follow the codes in this book: http://www.apress.com/9781484202098. Learn iOS 8 App Development 2nd Edition by James Bucanek
In particular, I am working through Chapter 3 - building a URL shortening app, but despite having copied the code exactly, I am getting an error on the code in Page 76:
if let toShorten = webView.request.URL.absoluteString {
which states 'NSURLRequest?' does not have a member named 'URL'.
I have tried googling an answer, but unfortunately have not come across anything. Any response I can find seems to suggest that my code ought to be working (e.g. How to get url which I hit on UIWebView?). This seems to have the closest answer SWIFT: Why I can't get the current URL loaded in UIWebView? but the solution does not appear to work for me. If I add a ? after the request, it will then at least build it, but I then have a nil variable returned.
I am using Xcode v6.1.1. Here is the piece of code that is coming up with the error in ViewController.swift:
let GoDaddyAccountKey = "0123456789abcdef0123456789abcdef" //this is replaced by my actual account key in my own code
var shortenURLConnection: NSURLConnection?
var shortURLData: NSMutableData?
#IBAction func shortenURL( AnyObject ) {
if let toShorten = webView.request?.URL.absoluteString { // ? now added
let encodedURL = toShorten.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let urlString = "http://api.x.co/Squeeze.svc/text/\(GoDaddyAccountKey)?url=\(encodedURL)"
shortURLData = NSMutableData()
if let firstrequest = NSURL(string: urlString) //added if here and removed !
let request = NSURLRequest(URL:firstrequest)
shortenURLConnection = NSURLConnection(request:request, delegate:self)
shortenButton.enabled = false
}
}
}
If you have any suggestions on how I can fix this, I would really appreciate it!
Update:
Following suggestions from Ashley below, I have amended my code so that it is no longer bringing up the error (see comments above). However, it is now no longer running. This appears to be because the urlString is being created as http://api.x.co/Squeeze.svc/text/d558979bb9b84eddb76d8c8dd9740ce3?url=Optional("http://www.apple.com/"). The problem is therefore the Optional() that is included and thus makes it an invalid URL. Does anyone have a suggestion on how to remove this please?
request is an optional property on UIWebView:
var request: NSURLRequest? { get }
also stringByAddingPercentEscapesUsingEncoding returns an optional:
func stringByAddingPercentEscapesUsingEncoding(_ encoding: UInt) -> String?
What you need is to make user of optional binding in a few places:
if let toShorten = webView.request?.URL.absoluteString {
if let encodedURL = toShorten.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
let urlString = "http://api.x.co/Squeeze.svc/text/\(GoDaddyAccountKey)?url=\(encodedURL)"
shortURLData = NSMutableData()
if let firstrequest = NSURL(string: urlString) { // If a method can return a nil, don't force unwrap it
let request = NSURLRequest(URL:first request)
shortenURLConnection = NSURLConnection(request:request, delegate:self)
shortenButton.enabled = false
}
}
}
See Apple's docs on optional chaining for details
See Apple's docs for NSURL class

Resources