Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noppoMan/8bdfe7dd0ef6081e6588 to your computer and use it in GitHub Desktop.
Save noppoMan/8bdfe7dd0ef6081e6588 to your computer and use it in GitHub Desktop.
Very Small Sample Code for UIWebView With NSURLConnectionDelegate and Basic Authorizaion
//
// 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