ios - How can I test an authentication challenge? -
dear fellow citizens of earth.
i'm requesting real life example can test authentication challenge code snippet found below. if it's not possible appreciate suggestions on can validate it.
i have feeling code working:
- (void)webview:(wkwebview *)webview didreceiveauthenticationchallenge:(nsurlauthenticationchallenge *)challenge completionhandler:(void (^)(nsurlsessionauthchallengedisposition, nsurlcredential * _nullable))completionhandler { nsstring *host = webview.url.host; nsstring *authenticationmethod = [[challenge protectionspace] authenticationmethod]; if ([authenticationmethod isequaltostring:nsurlauthenticationmethoddefault] || [authenticationmethod isequaltostring:nsurlauthenticationmethodhttpbasic] || [authenticationmethod isequaltostring:nsurlauthenticationmethodhttpdigest]) { nsstring *title = @"authentication challenge"; nsstring *message = [nsstring stringwithformat:@"%@ requires username , password.", host]; uialertcontroller *alertcontroller = [uialertcontroller alertcontrollerwithtitle:title message:message preferredstyle:uialertcontrollerstylealert]; [alertcontroller addtextfieldwithconfigurationhandler:^(uitextfield *textfield) { textfield.placeholder = @"username"; }]; [alertcontroller addtextfieldwithconfigurationhandler:^(uitextfield *textfield) { textfield.placeholder = @"password"; textfield.securetextentry = yes; }]; [alertcontroller addaction:[uialertaction actionwithtitle:@"authenticate" style:uialertactionstyledefault handler:^(uialertaction *action) { nsstring *username = ((uitextfield *)alertcontroller.textfields[0]).text; nsstring *password = ((uitextfield *)alertcontroller.textfields[1]).text; nsurlcredential *credential = [[nsurlcredential alloc] initwithuser:username password:password persistence:nsurlcredentialpersistencenone]; completionhandler(nsurlsessionauthchallengeusecredential, credential); }]]; [alertcontroller addaction:[uialertaction actionwithtitle:@"cancel" style:uialertactionstylecancel handler:^(uialertaction *action) { completionhandler(nsurlsessionauthchallengecancelauthenticationchallenge, nil); }]]; [_delegate webview:self didreceiveauthenticationchallengewithalertcontroller:alertcontroller]; } else if ([authenticationmethod isequaltostring:nsurlauthenticationmethodservertrust]) { completionhandler(nsurlsessionauthchallengeperformdefaulthandling, nil); } else { completionhandler(nsurlsessionauthchallengecancelauthenticationchallenge, nil); } }
love , peace all.
source: https://github.com/shingofukuyama/wkwebviewtips
older question: how can implement authentication challenge using wkwebview?
usually have set specific breakpoints in code , manipulate variables in debugger console statements expr varname=@"test"
test code this.
the interesting part here how trigger call webview:didreceiveauthenticationchallenge:
- use http testing service httpbin - e.g. this basic auth test
- write simple webserver python, ruby etc. , test against it
- configure osx-builtin apache httpd use basic auth access, test ios simulator against localhost. here's simple tutorial on how configure server. sure check comments, steps outdated (e.g. conf file located @ /etc/apache2/httpd.conf)
Comments
Post a Comment