iOS適配https詳解

2022-08-09 16:45:13 字數 4227 閱讀 8462

首先是對nsurlconnection的適配,不管是原生的還是af的,歸根結底都是要用它去連線伺服器。

1.如果使用af進行網路資料請求,那麼使用如下方法即可:

1 - (afsecuritypolicy*)customsecuritypolicy23

12//

afsslpinningmodecertificate 使用證書驗證模式

1314 afsecuritypolicy *securitypolicy =[afsecuritypolicy policywithpinningmode:afsslpinningmodecertificate];

1516

//allowinvalidcertificates 是否允許無效證書(也就是自建的證書),預設為no

1718

//如果是需要驗證自建證書,需要設定為yes

1920 securitypolicy.allowinvalidcertificates =yes;

2122

//validatesdomainname 是否需要驗證網域名稱,預設為yes;

2324

//假如證書的網域名稱與你請求的網域名稱不一致,需把該項設定為no;如設成no的話,即伺服器使用其他可信任機構頒發的證書,也可以建立連線,這個非常危險,建議開啟。

2526

//置為no,主要用於這種情況:客戶端請求的是子網域名稱,而證書上的是另外乙個網域名稱。因為ssl證書上的網域名稱是獨立的,假如證書上註冊的網域名稱是www.google.com,那麼mail.google.com是無法驗證通過的;當然,有錢可以註冊萬用字元的網域名稱*.google.com,但這個還是比較貴的。

2728

//如置為no,建議自己新增對應網域名稱的校驗邏輯。

2930 securitypolicy.validatesdomainname =no;

3132 securitypolicy.pinnedcertificates =@[certdata];

3334

return

securitypolicy;

3536 }

其實,在這裡不需要使用伺服器的證書,本人親測過。不過為了保險起見還是加上,還需要注意一點,af3.0需要用到der格式的證書。

然後加上下面的**:

2.如果是原生的nsurlconnection,那麼需要在nsurlconnectiondelegate的乙個方法裡面加**,如下:

1 - (void)connection:(nsurlconnection *)connection

2 willsendrequestforauthenticationchallenge:(nsurlauthenticationchallenge *)challenge3;

17//

cfarrayref certarray = cfarraycreate(kcfallocatordefault, certs, 1, null);

18//

create a credential from the certificate and ideneity, then reply to the challenge with the credential

19//

nslog(@"identity*****====%@",identity);

20 nsurlcredential *credential =[nsurlcredential credentialwithidentity:identity certificates:nil persistence:nsurlcredentialpersistencepermanent];

2122

//credential = [nsurlcredential credentialwithidentity:identity certificates:(__bridge nsarray*)certarray persistence:nsurlcredentialpersistencepermanent];

2324

[challenge.sender usecredential:credential forauthenticationchallenge:challenge];

25 }

1 - (void)urlsession:(nsurlsession *)session

2 didreceivechallenge:(nsurlauthenticationchallenge *)challenge

3 completionhandler:(void (^)(nsurlsessionauthchallengedisposition disposition, nsurlcredential *credential))completionhandler

411 nsstring *thepath = [[nsbundle mainbundle] pathforresource:@"

client

" oftype:@"

p12"

];12 nsdata *pkcs12data =[[nsdata alloc] initwithcontentsoffile:thepath];

13 cfdataref inpkcs12data =(cfdataref)cfbridgingretain(pkcs12data);

14secidentityref identity;

15//

讀取p12證書中的內容

16 osstatus result = [self extractp12data:inpkcs12data toidentity:&identity];

17if(result !=errsecsuccess)

21 seccertificateref certificate =null;

22 secidentitycopycertificate (identity, &certificate);

23const

void *certs =;

24 cfarrayref certarray = cfarraycreate(kcfallocatordefault, certs, 1

, null);

25 nsurlcredential *credential1 = [nsurlcredential credentialwithidentity:identity certificates:(nsarray*)cfbridgingrelease(certarray) persistence:nsurlcredentialpersistencepermanent];

26completionhandler(nsurlsessionauthchallengeusecredential, credential1);

27 }

1 - (osstatus) extractp12data:(cfdataref)inp12data toidentity:(secidentityref*)identity ;

5const

void *values =;

6 cfdictionaryref options = cfdictionarycreate(null, keys, values, 1

, null, null);

7 cfarrayref items = cfarraycreate(null, 0, 0

, null);

8 securityerror = secpkcs12import(inp12data, options, &items);

9if (securityerror == 0

) 15

if(options)

18return

securityerror;

19 }

其實適配https沒有我們想象的那麼複雜,你找對了地方可能幾分鐘就弄好了,找不到,可能花幾天。不管怎樣,最後適配成功還是要感謝網上的一些大神,雖然官網上面也有答案,但畢竟時間不等人,等我研究透徹,估計蘋果又會有新的東西出來吧。至此,希望能幫到正在為https適配而憂傷的小夥伴們。

關於ios適配

參考ios應用框架談view層的組織和呼叫方式 參考唐巧ios開發爭議 關於選用storyboard還是手寫code 開發團隊人數多,git 容易conflict,而storyboard衝突很難解決,手寫code容易解決。復用介面多的情況手寫code,而storyboard做不到復用view 複雜介...

iOS 螢幕適配

螢幕適配 1 螢幕適配的發展過程 計算frame autoreszing 父控制項和子控制項的關係 autolayout 任何控制項都可以產生關係 sizeclass 沒有螢幕適配,直接使用frame固定子控制項的大小 螢幕大小一樣,但是如果進行ipad開發,就需要考慮螢幕適配 螢幕大小不一樣,需要...

iOS螢幕適配

ios的螢幕適配從純frame到autoresizingmask再到autolayout,autoresizingmask autolayout sizeclassesautoresizingmask和autolayout不相容 sizeclasses依賴autolayout autoresizin...