15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SwiftでBasic認証

Posted at

以下はBasic認証のためのヘッダー付き Requestオブジェクトの作成するサンプルコード.
NSURLSessionのデリゲートつかを使ってもっとかっこ良く書けるのだろうけど、iOS力が低くてまだやり方がわからない...

var url_with_basic_auth = "https://www.example.com/"
var url = NSURL(string: url_with_basic_auth)
var req = NSMutableURLRequest(URL: url!)

//Authorizationヘッダーの作成
var username = "username"
var password = "password"
var authStr = "\(username):\(password)"
var data = authStr.dataUsingEncoding(NSUTF8StringEncoding)
var authData = data!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
var authValue = "Basic \(authData)"

//作成したAuthorizationヘッダーの付与
req.setValue(authValue, forHTTPHeaderField: "Authorization")

Basic認証

15
14
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?