質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2586閲覧

Alamofireでbasic認証

aaaaaachannel

総合スコア37

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/11/25 14:38

編集2017/11/25 15:06

ログインする必要のあるサイトをhtmlで取ってくるのにalamofireでbasic認証しようと思い、
以下のコードを書きましたが、Extra argument in callというエラーがAlamofire.request(.GET, "https://localhost.jp/basic-auth/")の引数のURLに対して出ます。
何が悪いのかよくわかりません。よろしくお願いいたします。

swift

1Alamofire.request(.GET, "https://localhost.jp/basic-auth/") 2 .authenticate(user: user, password: password) 3 .response {(request, response, _, error) in 4 print(response) 5 }

コード全文

swift

1// 2// ViewController.swift 3// url3 4// 5// Created by 東愛央衣 on 2017/11/21. 6// Copyright © 2017年 東愛央衣. All rights reserved. 7// 8 9import UIKit 10import Alamofire 11 12var days: Int = 0 13let weekArray = [" ", "月", "火", "水", "木", "金", "土"] 14let subjectArray = ["1", "月", "火", "水", "木", "金", "土", 15 "2", "月", "火", "水", "木", "金", "土", 16 "3", "月", "火", "水", "木", "金", "土", 17 "4", "月", "火", "水", "木", "金", "土", 18 "5", "月", "火", "水", "木", "金", "土", 19 "6", "月", "火", "水", "木", "金", "土", 20 "7", "月", "火", "水", "木", "金", "土"] 21 22let user = "blueeeast@keio.jp" 23let password = "B1UEast9280" 24 25 26 27 28class ViewController: UIViewController,UICollectionViewDataSource { 29 30 31 func numberOfSections(in collectionView: UICollectionView) -> Int { 32 return 2 33 } 34 35 36 37 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 38 if section == 0{ 39 return 7 40 } else { 41 return 49 42 43 } 44 } 45 46 47 48 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 49 //コレクションビューから識別子「TestCell」のセルを取得する。 50 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TestCell", for: indexPath) as! TestCollectionViewCell 51 if indexPath.section == 0 { 52 cell.backgroundColor = UIColor.gray 53 cell.testLabel.text = weekArray[indexPath.row] 54 } else { 55 cell.backgroundColor = UIColor.white 56 cell.testLabel.text = subjectArray[indexPath.row] 57 } 58 return cell 59 } 60 61 62 63 override func viewDidLoad() { 64 super.viewDidLoad() 65 // Do any additional setup after loading the view, typically from a nib. 66 let client = Client() 67 client.authenticate(user: user, password: password) 68 } 69 70 override func didReceiveMemoryWarning() { 71 super.didReceiveMemoryWarning() 72 // Dispose of any resources that can be recreated. 73 } 74 class Client { 75 76 77 78 79 80 public func authenticate(user: String, password: String) -> Self { 81 82 83 84 85 Alamofire.request(.GET, "https://localhost.jp/basic-auth/") 86 .authenticate(user: user, password: password) 87 .response {(request, response, _, error) in 88 print(response) 89 } 90 91 // var webContext:NSString! = nil 92 // //ダウンロード 93 // let url = URL(string: "https://www.edu.keio.jp/ess2/login?lang=jp")! 94 // let task = URLSession.shared.dataTask(with: url) { data, response, error in 95 // if let jsonData = data { 96 // print(jsonData) 97 // 98 // 99 // webContext = NSString(data:jsonData,encoding:String.Encoding.utf8.rawValue) 100 // print(webContext) 101 // 102 // } 103 // } 104 // task.resume() 105 // } 106 107 } 108 } 109} 110 111 112

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Udomomo

2017/11/25 14:57

そのエラーはどの行に対して出ていますか?エラーの文面だけ見ると、「余計な引数がある」という意味です。
guest

回答1

0

ベストアンサー

Alamofireは2016年9月にバージョン4にアップデートされ、requestが取る引数が変わったようです。
必須の引数はURLのみとなり(このときデフォルトでGETメソッドが適用される)、メソッドを指定したいときはmethod: <メソッド名>のように記載します。
今回はGETメソッドなので、該当の行はAlamofire.request("https://localhost.jp/basic-auth/")で良いと思います。

以下のプルリクエストの"Top-Level APIs"の部分に詳細が書いてあります。
https://github.com/Alamofire/Alamofire/pull/1462

投稿2017/11/25 15:36

編集2017/11/25 15:37
Udomomo

総合スコア1524

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問