我正在使用 NSURLComponents 并设置用户名和密码:
let urlComponents = NSURLComponents(string: webservice);
urlComponents?.user = username;
urlComponents?.password = password;
我使用的密码中有一个特殊字符(正斜杠/)
我已经确认输入的密码是正确的,并且我正在尝试的所有其他密码都有效,但带有/的密码除外。
我必须转义特殊字符吗?
这是完整的方法:
func loginUser(_ username: String, password: String, completion: @escaping (_ result: Bool) -> Void)
{
let urlComponents = NSURLComponents(string: webservice);
urlComponents?.user = username;
urlComponents?.password = password
let url = urlComponents?.url;
URLSession.shared.dataTask(with: url!, completionHandler: {
(data, response, error) in
if(error != nil){
completion(false)
}else{
do{
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
OperationQueue.main.addOperation({
self.appDelegate.communityArray = json
//Get Community Descrtiptions and add them to appDelegates communityDescriptionArray
self.getCommunities() { (result: Dictionary<String, Dictionary<String, Any>>) in
self.appDelegate.communityList = result
completion(true)
}
})
}catch let error as NSError{
print(error)
completion(false)
}
}
}).resume()
}
当我尝试时出现错误
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
我得到的错误:
Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}
最佳答案
您永远不应将密码作为 URL 的一部分传递。 URL 以明文形式发送到上游 DNS 服务器。
另外,不清楚您要做什么。 “@”符号和“/”字符不是 URL 中的合法字符。好吧,“/”是合法的,但它是一个路径分隔符。
关于ios - Swift 4 - NSURLComponents 设置带有正斜杠的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58509661/