无法使用 alamofire 和 DisabledTrustEvaluator 向服务器发出请求

Failing to make request to server using alamofire and DisabledTrustEvaluator

我正在尝试从我使用 asp.net 核心创建的服务器上获取一些数据;当试图在我的 Swift iOS 代码中获取数据时,它一直失败,没有显示任何错误。我已经在 info.plist.

中完成了所有 TSL 检查

model.swift

    import Foundation

struct User: Decodable {
    let name:String
    let userEmail: String
    let firebaseId: String
    let userMode: Int
    let profileImg: String
    let loginType: Int?
}

userController.swift

    import UIKit
import ProgressHUD

class LoginViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        removeNavBar()

        
//        UserApiService.shared.makeGetAPICall(user:{users, error in
//         //   ProgressHUD.dismiss()
//            guard let users = users else {return}
//            print(users)
//            guard let errors = error else {return}
//            print(errors)
//        })
    }
    
    @IBAction func googleLogin(_ sender: Any) {
        ProgressHUD.animationType = .circleSpinFade
        ProgressHUD.show()
        UserApiService.init().makeGetAPICall(user: {users, error in
               ProgressHUD.dismiss()
               guard let users = users else {return}
               print(users)
               guard let errors = error else {return}
               print(errors)
           })
    
    }
    private func removeNavBar(){
        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

apiService.swift

 import Foundation
import Alamofire

class UserApiService{
    
    static let shared =  UserApiService.init()
    
    let session: Session = {
        let manager = ServerTrustManager(allHostsMustBeEvaluated: false,evaluators: ["localhost:5001": DisabledTrustEvaluator()])
        let configuration = URLSessionConfiguration.af.default

        return Session(configuration: configuration, serverTrustManager: manager)
    }()

    //MARK:- GET
    func makeGetAPICall(user completionHandler: @escaping ([User]?,Error?) -> Void) {
        var urlComponent = URLComponents()
        urlComponent.scheme = "https"
        urlComponent.host = "localhost"
        urlComponent.port = 5001
        urlComponent.path = "/api/user"
        print(urlComponent.url!)
        guard let url = urlComponent.url else {
            return
        }
        
        session.request(url, method: .get).validate().responseDecodable(of: [User].self) {(response) in
            switch response.result{
            case .success:
                guard let users = response.value else {
                    return
                }
                print(users)
                completionHandler(users, nil)
                
            case .failure(let error):
                completionHandler(nil, error)
            }
        }
    }
}

info.plist

    <key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>localhost</key>
            <dict>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict>
    <key>NSAllowsArbitraryLoadsForMedia</key>
    <true/>
    <key>NSAllowsLocalNetworking</key>
    <true/>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

我用断点调试它,我得到 <unavailable; try printing with "VO" or "PO">。我无法理解这意味着什么以及我应该如何解决这个问题。

此外,我已经更改了我的 info.plist,但是它看起来像这样;我仍然遇到同样的问题

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoadsForMedia</key>
        <true/>
        <key>NSAllowsLocalNetworking</key>
        <true/>
        <key>NSAllowsArbitraryLoadsInWebContent</key>
        <true/>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

所以我设法解决了我的问题,万岁!!!

我所做的是,我将 evaluators: ["localhost:5001": DisabledTrustEvaluator() 更改为 evaluators: ["localhost": DisabledTrustEvaluator()

此外,在调用我的方法时,我通常从 class 引用,但之后,我不得不将其作为单例调用,然后,嘿,PRESTO,它成功了!

 @IBAction func googleLogin(_ sender: Any) {
    ProgressHUD.animationType = .circleSpinFade
    ProgressHUD.show()
    UserApiService.shared.makeGetAPICall(user: {users, error in
           ProgressHUD.dismiss()
           guard let users = users else {return}
           print(users)
           guard let errors = error else {return}
           print(errors)
       })

}

这个 link 帮助我找出了问题的部分原因 https://github.com/Alamofire/Alamofire/issues/3336