ios - Swift 4 - NSURLComponents 设置带有正斜杠的密码

标签 ios swift

我正在使用 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/

相关文章:

ios - Nativescript IOS : Textview not resizing after Input was cleared

ios - 如何返回 Root View Controller

ios - GCDAsyncSocketDelegate 的 Swift 示例

ios - 如何在 iOS 中查找所有线程的堆栈跟踪

ios - 在表格 View 单元格上无法访问detailTextLabel和imageView

ios - iOS:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“JSON写入(_NSInlineData)中的类型无效”

iphone - 如何通过 SOAP 传输类的实例?

ios - 如何在UICollectionView中获取选定的CollectionViewCell?

ios - UITableViewRowAction 和 UIAlertController

3秒后Swift 2执行功能