Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
14
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

SwiftでBasic認証

以下は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認証

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
14
Help us understand the problem. What are the problem?