How can I find out specific character from URL String variable using Swift 3?
i want to get the value out of the URLlink(image is given) user_lati,user_long,destination_lat,destinaton_long and save it.
Can any one give me the solution for this?
I have given a couple of examples below. You should really check the documentation for String, URL and Collections
let urlString = "https://www.example.com/search?q=test&user=john"
let url = URL(string: urlString)!
print(url.query) // q=test&user=john
print(urlString.index(of: "?")?.encodedOffset) // 30 - index of ?
print(url.absoluteString.index(of: "?")?.encodedOffset) // same as above
Related
I can't find any way to extract a certain string value from another string in SwiftUi.
It is the following link:
"http://media.site.com/videos/3070/0003C305B74F77.mp4"
How would you go about extracting the numbers 0003C305B74F77?
It would be much easier to treat it as an URL. That's what it is. All you need it to get its last path component after deleting its path extension.
let link = "http://media.site.com/videos/3070/0003C305B74F77.mp4"
if let url = URL(string: link) {
let lastPathComponent = url.deletingPathExtension().lastPathComponent
print(lastPathComponent) // "0003C305B74F77"
}
This question already has an answer here:
Why can't I convert this String to a URL?
(1 answer)
Closed 3 years ago.
if let url = URL(string: "https://omsoftware.org/sorora/public/profile_images/kapil borkar_199.jpeg"){}
is always getting fail when the file extension is .jpeg . i have tried with .png it works fine only .
URL(string:) is not giving url object when extension is .jpeg. please help.
You need to encode the urlString to handle the whitespaces. Use addingPercentEncoding(withAllowedCharacters:) method on the urlString, i.e.
let str = "https://omsoftware.org/sorora/public/profile_images/kapil borkar_199.jpeg"
if let urlString = str.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let url = URL(string: urlString) {
//add your code here...
}
addingPercentEncoding(withAllowedCharacters:)
Returns a new string made from the receiver by replacing all
characters not in the specified set with percent-encoded characters.
Refer this to know more about addingPercentEncoding(withAllowedCharacters:) method.
As you can see there is whitespace in your url
so you can use like
let urlStr = "your Url Strting".replacingOccurrences(of: " ", with: "%20")
if let url = URL(string: urlStr){}
I have a url in which I add URL components to it to form a query string. For example the url is https://example.com/test, and with the url components the final url is https://example.com/test?urlcomponent1=1&urlcomponent2=1234.
I need to keep the final url with the urlcomponents, but I need to remove the ?. How do I do that? So the final url would be https://example.com/testurlcomponent1=1&urlcomponent2=1234.
I have looked into removing artifacts, but haven't found a solution.
If you know you only have one ? in your url, you can remove it using replacingOccurrencesOf
let newURL = URL(string: url.absoluteString.replacingOccurrences(of: "?", with: ""))
Here is an example of my URL:
let urlString = "https://example.com/img/list/mobile/7156-292.jpg"
when I pass it to NSURL(string: urlString ) it returns nil.
Any idea what I am doing wrong?
Your updated output explains your issue. There is a newline character at the end of urlString. You need to cleanup the string from wherever you are obtaining those URLs.
let cleanURL = badURL.trimmingCharacters(in: . whitespacesAndNewlines)
Based on your edit, it appears that all of your URL strings end in a newline. A newline is not a valid character to have in a URL.
In other words, your URL actually looks like
let urlString = "https://new.domain.com/img/list/mobile/7156-292.jpg\n"
EDIT
https://www.someurl.com/search?&access_token=1,84,848473938;848483,83&_json={"key1":"value1","key2":"value2"}
When declaring a URL that has a JSON string, I obviously need to use braces _json={ } and qoutes \"key1\":\"value1\"
NSURL(string: String), however, magically becomes nil if either of these characters are included in the string.
So as answered correctly here: NSURL is returning nil for a valid URL, I tried using:
let url = NSURL(string: url.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!)
But I believe that's deprecated since it was before Swift 2 was released and I am getting the error: cannot convert value of Type NSCharacterSet to expected argument type NSStringEncoding (aka UInt)
So I tried using
let url = NSURL(string: url.stringByAddingPercentEncodingWithAllowedCharacters(NSUTF8StringEncoding)!)!
and while that did allow NSURL to have a value instead of nil, it did not return the expected results, so something is still wrong.
I know that the format is correct, because if I type the URL string manually in a browser, I get the expected result. If i copy/paste the encoded version from Xcode, it gives me the wrong result as did Swift when encoding as shown above.
Any insight would be much appreciated.
You can modify a mutable character set to remove an allowed character: since you want the commas to be encoded, remove the comma from the URLQueryAllowedCharacterSet before using it.
In Swift 2, we need to dance with NSMutableCharacterSet like this:
let sourceURL = "https://www.someurl.com/search?&access_token=1,84,848473938;848483,83&_json={\"key1\":\"value1\",\"key2\":\"value2\"}"
let charSet = NSMutableCharacterSet()
charSet.formUnionWithCharacterSet(NSCharacterSet.URLQueryAllowedCharacterSet())
charSet.removeCharactersInString(",")
let url = NSURL(string: sourceURL.stringByAddingPercentEncodingWithAllowedCharacters(charSet)!)
print(url!)
Prints:
https://www.someurl.com/search?&access_token=1%2C84%2C848473938;848483%2C83&_json=%7B%22key1%22:%22value1%22%2C%22key2%22:%22value2%22%7D
To do the same thing with Swift 3 we're using the CharacterSet struct instead of NSMutableCharacterSet but it's the same idea:
var charSet = CharacterSet()
charSet.formUnion(.urlQueryAllowed)
charSet.remove(",")
if let encoded = sourceURL.addingPercentEncoding(withAllowedCharacters: charSet) {
if let url = URL(string: encoded) {
print(url)
}
}