NSURL returns nil - swift

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"

Related

if let url = URL(string:"") always getting fail for .JPEG images [duplicate]

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){}

URL constructor doesn't work with some characters

I'm trying to call a php-script from my app using URLRequest.
The Url path is generated in the String-Variable query and for the request I convert it like this
guard let url = URL(string: query) else {
print("error")
return
}
usually it works, but when the request contains characters like ä, ö, ü, ß the error is triggered. How can I make it work?
The URL(string:) initializer doesn't take care of encoding the String to be a valid URL String, it assumes that the String is already encoded to only contain characters that are valid in a URL. Hence, you have to do the encoding if your String contains non-valid URL characters. You can achieve this by calling String.addingPercentEncoding(withAllowedCharacters:).
let unencodedUrlString = "áűáeqw"
guard let encodedUrlString = unencodedUrlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let url = URL(string: encodedUrlString) else { return }
You can change the CharacterSet depending on what part of your URL contains the characters that need encoding, I just used urlQueryAllowed for presentation purposes.
Split your URL in two separate parts:
let baseURLString = "https://www.example.com"
let pathComponent = "áűáeqw"
let fullURL = URL(string: baseURLString)?.appendingPathComponent(pathComponent)

Delete parameter from json URL with swift3

I have a URL that looks like 123456_https://example.com.
I want to delete every thing before the _https: part. At the end, the URL should look like https://example.com.
How can I achieve that?
If the prefix is always a couple of characters ending with a underscore you can use this regular expression
let url = "123456_https://example.com"
let trimmedURL = url.replacingOccurrences(of: "^\\w+_", with: "", options: .regularExpression)
you can do something like this if you do not know how to use Regular Expressions
// url string
var url = "123456_https://example.com"
// seperate url by "_" and provides 123456 and https://example.com in urlArray
let urlArray = url.components(separatedBy: "_")
// now to be safe
if(urlArray.count == 2){
// here you get your desired string
let myUrl = urlArray[1]
}

Why won't NSURL accept a valid string that contains quotes or braces?

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)
}
}

How to encode special character like ’ (NSWindowsCP1250StringEncoding) in Swift2

As in Swift2 stringByAddingPercentEscapesUsingEncoding() is deprecated instead of stringByAddingPercentEncodingWithAllowedCharacters() is used.
But how to encode the especial character like ' % & in swift2
For example in iOS8(swift1.2) i used following code for encoding
NSURL(string: "myurl.php?deviceName=name’phone".stringByAddingPercentEscapesUsingEncoding(NSWindowsCP1250StringEncoding)!)
it work fine i.e. on server it decode correctly.
But in iOS9(Swift2.0) i used following code
NSURL(string: "myurl.php?deviceName=name ’phone".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!)
It will not decode properly.
please tell me how i can encode special charater in swift2.0 ?
EDIT :
Eric D answer is right but when i encode below stringURL it will not encode properly.
Why?
let stringURL = "https://my.server.com/login.php?e=email&dn=my’s iPad&d=5&er=D550772E-34BB-4DCB-89C9-E746FAD83D24&tz=330"
print(stringURL)
let charSet = NSCharacterSet.URLPathAllowedCharacterSet()
let encoded = stringURL.stringByAddingPercentEncodingWithAllowedCharacters(charSet)!
let url = NSURL(string: encoded.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!)!
print(url) //https%253A//my.server.com/login.php%253Fe=email&dn=my%25E2%2580%2599s%2520iPad&d=5&er=D550772E-34BB-4DCB-89C9-E746FAD83D ... 4&tz=330
EDIT 2:
How to encode NSWindowsCP1250StringEncoding in swift2.0 ?
Use URLPathAllowedCharacterSet() as character set:
let stringURL = "myurl.php?deviceName=name'phone"
let charSet = NSCharacterSet.URLPathAllowedCharacterSet()
let encoded = stringURL.stringByAddingPercentEncodingWithAllowedCharacters(charSet)!
let url = NSURL(string: encoded)!
print(url) // "myurl.php%3FdeviceName=name'phone"
Note that ' doesn't have to be encoded, it's a valid URL character.
Update: in the comments you state that it's still not working because your encoded URL is truncated and contains ..., but actually this is likely to be just a printed description truncated by NSURL; the actual URL contained by the NSURL object should be ok. Otherwise it would be nil. You should check on your server side for problems with very long but correct URLs.
let newURLEncodedString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())