Created
January 18, 2015 18:39
-
-
Save noppoMan/8bdfe7dd0ef6081e6588 to your computer and use it in GitHub Desktop.
Very Small Sample Code for UIWebView With NSURLConnectionDelegate and Basic Authorizaion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// WebViewWithNSURLConnectionDelegateSampleController.swift | |
// SwiftSamples | |
// | |
// Created by Yuki Takei on 1/18/15. | |
// Copyright (c) 2015 noppoman. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
class WebViewWithNSURLConnectionDelegateSampleController: UIViewController, NSURLConnectionDelegate { | |
@IBOutlet var webView: UIWebView! | |
private var request : NSURLRequest { | |
let baseUrl = "http://miketokyo.com" | |
var URL = NSURL(string: baseUrl)! | |
return NSURLRequest(URL: URL) | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
var conn = NSURLConnection(request: request, delegate: self, startImmediately: true) | |
} | |
func connection(connection: NSURLConnection, | |
willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge){ | |
if challenge.protectionSpace.host == "miketokyo.com" { | |
let user = "user" | |
let password = "password" | |
let credential = NSURLCredential(user: user, password: password, persistence: NSURLCredentialPersistence.ForSession) | |
challenge.sender.useCredential(credential, forAuthenticationChallenge: challenge) | |
} | |
} | |
func connectionDidFinishLoading(connection: NSURLConnection!) { | |
self.webView.loadRequest(request) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment