ios - Basic authentication with UIWebView -


i read lot of post on over how able apply basic authentication.

i've produced code not show log on page, white page displayed. credentials use works in browser, not problem. delegates ok.

i can't figure out code fails:

override func viewdidload() {     super.viewdidload()     webview.delegate = self     self.loadpage() }  func loadpage() {     let url = "mypage.com/auht/logon.do"     let request = nsmutableurlrequest(url: nsurl(string: url)!, cachepolicy: nsurlrequestcachepolicy.reloadignoringlocalcachedata, timeoutinterval: 12)     webview.loadrequest(request) }  // mark: nsurlconnectiondelegate delegates  func connection(connection: nsurlconnection, willsendrequestforauthenticationchallenge challenge: nsurlauthenticationchallenge) {     if challenge.previousfailurecount == 0 {         authenticated = true         let credential = nsurlcredential(user: "m.rinaldi13", password: "299792,458km/s", persistence: nsurlcredentialpersistence.forsession)         challenge.sender.usecredential(credential, forauthenticationchallenge: challenge)     } else {         challenge.sender.cancelauthenticationchallenge(challenge)     } }  // mark: web view delegates  func webview(webview: uiwebview, shouldstartloadwithrequest request: nsurlrequest, navigationtype: uiwebviewnavigationtype) -> bool {     if authenticated == nil {         authenticated = false         nsurlconnection(request: request, delegate: self)         return false     }     return true } 

any help/tip appreciated!

i find solution self, excluding boring passeges.

func dorequestwithbasicauth(completion : (success : bool, html: string?, error : nserror?) -> void) {     if let user = self.user {         let loginstring = nsstring(format: "%@:%@", user.login!, user.pwd!)         let logindata: nsdata = loginstring.datausingencoding(nsutf8stringencoding)!         let base64loginstring = logindata.base64encodedstringwithoptions(nil)         let url = nsurl(string: user.service!.geturl())         let request = nsmutableurlrequest(url: url!)         request.httpmethod = "post"         request.setvalue("basic \(base64loginstring)", forhttpheaderfield: "authorization")         nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue.mainqueue()) {(response, data, error) in             if error == nil {                 let htmlstring = nsstring(data: data, encoding: nsutf8stringencoding)                 completion(success: true, html: htmlstring as? string, error: nil)             } else {                 completion(success: false, html: nil, error: error)             }         }     } else {         completion(success: false, html: nil, error: nserror())     } } 

then can evenly display page on web view in way:

self.dorequestwithbasicauth({ (success, html, error) -> void in     if error == nil {        self.webview.loadhtmlstring(string: html, baseurl: <yournsurl>)     } }) 

obviously can (had) beautify code, creating class model user:

class user {     var login: string?     var pwd: string?      func valueforheaderfieldauthorization() -> string {         let loginstring = nsstring(format: "%@:%@", user.login!, user.pwd!)         let logindata: nsdata = loginstring.datausingencoding(nsutf8stringencoding)!         let base64loginstring = logindata.base64encodedstringwithoptions(nil)         return "basic \(base64loginstring)"     } }  

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -