国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

ios 配置HTTPS (單向驗證,工程不需要加證書)

2019-11-08 00:34:03
字體:
來源:轉載
供稿:網友

這個問題坑了我兩天啊!網上各種搜,也沒解決了。

后臺單向驗證,自簽名證書!

客戶端需要做的事情:

一、swift   Alamofire網絡請求

在請求方法前面加上以下代碼即可:
let manager: Alamofire.Manager = {            let manager = Alamofire.Manager.sharedInstance            manager.delegate.sessionDidReceiveChallenge = { session, challenge in                var disposition: NSURLSessionAuthChallengeDisposition = .PerformDefaultHandling                var credential: NSURLCredential?                                if challenge.PRotectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {                    disposition = NSURLSessionAuthChallengeDisposition.UseCredential                    credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)                } else {                    if challenge.previousFailureCount > 0 {                        disposition = .CancelAuthenticationChallenge                    } else {                        credential = manager.session.configuration.URLCredentialStorage?.defaultCredentialForProtectionSpace(challenge.protectionSpace)                                                if credential != nil {                            disposition = .UseCredential                        }                    }                }                return (disposition, credential)            }            return manager        }()

二、 UIWebView 配置https(因為我的工程全是swift寫的,貼上swift代碼,oc自己轉吧!)

主要實現NSURLConnectionDelegate兩個代理方法:

func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool {        return (protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust)    }    func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {        if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {            challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!), forAuthenticationChallenge: challenge)            challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge)        }            }這樣就搞定了 ,坑了我兩天啊,swift的更是不好找!

本人菜鳥,哪不對歡迎提出


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 隆安县| 诸暨市| 万州区| 本溪市| 辰溪县| 准格尔旗| 临西县| 江油市| 灵川县| 冷水江市| 湛江市| 凌云县| 涿州市| 永川市| 延吉市| 上高县| 玛纳斯县| 理塘县| 石家庄市| 大庆市| 吴川市| 高阳县| 沅江市| 博乐市| 长兴县| 明光市| 察隅县| 应用必备| 松阳县| 巴南区| 湖州市| 会泽县| 新兴县| 仙游县| 陇西县| 沂水县| 勐海县| 崇明县| 汝阳县| 德庆县| 九台市|