国产探花免费观看_亚洲丰满少妇自慰呻吟_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的更是不好找!

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂伦春自治旗| 江安县| 宁武县| 偃师市| 陕西省| 阿合奇县| 龙川县| 木兰县| 集安市| 长春市| 手游| 仙居县| 仁布县| 青田县| 黔东| 安岳县| 灵宝市| 鄂尔多斯市| 突泉县| 安新县| 沅陵县| 郯城县| 大理市| 曲松县| 桦南县| 聊城市| 泰州市| 霍邱县| 玛沁县| 庆阳市| 天柱县| 名山县| 罗田县| 即墨市| 阿城市| 固镇县| 阳东县| 百色市| 安宁市| 临清市| 出国|