前提・実現したいこと
ローカルのMySQL内のデータを引っ張ってきて
tableViewで一覧表示をしたいのですが、シミュレータだと表示までできるのですが
実機で確認すると、以下のエラーメッセージが表示されてしまいます。
以下のリンク先を参考に
info.plistを編集してみたのですが、解消されませんでした。
手詰まりなためご教示頂けると幸いです。
宜しくお願い致します。
発生している問題・エラーメッセージ
2018-10-29 09:48:51.732365+0900 WebAPITest[6741:2290709] TIC TCP Conn Failed [1:0x1c4169f00]: 1:61 Err(61) 2018-10-29 09:48:51.732444+0900 WebAPITest[6741:2290709] Task <9BF294B7-56E1-4854-8C45-B5D6FEDF570B>.<1> HTTP load failed (error code: -1004 [1:61]) 2018-10-29 09:48:51.732647+0900 WebAPITest[6741:2290710] Task <9BF294B7-56E1-4854-8C45-B5D6FEDF570B>.<1> finished with error - code: -1004
該当のソースコード
Swift
1import UIKit 2import Alamofire 3import Foundation 4 5class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { 6 @IBOutlet weak var dbTableView: UITableView! 7 8 var dbItems:[JsonModel] = [] 9 let refreshControl = UIRefreshControl() 10 let url = "http://127.0.0.1/test.php" 11 12 @IBAction func addData(_ sender: UIBarButtonItem) { 13 //print("押された") 14 showAlert() 15 } 16 17 private func showAlert(){ 18 let alert = UIAlertController(title: "test", message: "aaaaa", preferredStyle: .alert) 19 let okAction = UIAlertAction(title: "OK", style: .default, handler: { 20 (action:UIAlertAction!) -> Void in 21 // OKを押した時入力されていたテキストを表示 22 if let textFields = alert.textFields { 23 // アラートに含まれるすべてのテキストフィールドを調べる 24 for textField in textFields { 25 print(textField.text!) 26 } 27 28 self.post() 29 } 30 }) 31 let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 32 33 alert.addAction(okAction) 34 alert.addAction(cancelAction) 35 alert.addTextField(configurationHandler: {(textField: UITextField!) -> Void in 36 textField.placeholder = "テキスト" 37 }) 38 39 self.present(alert, animated: true, completion: nil) 40 } 41 42 //DBにデータを追加する 43 private func post(){ 44 let headers:HTTPHeaders = [ 45 "Contenttype":"application/json" 46 ] 47 48 let parameters:[String:String] = [ 49 "name":"testName" 50 ] 51 52 Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response in 53 if let result = response.result.value as? [String: String] { 54 print(result) 55 } 56 } 57 } 58 59 override func viewDidLoad() { 60 super.viewDidLoad() 61 62 dbTableView.delegate = self 63 dbTableView.dataSource = self 64 dbTableView.tableFooterView = UIView(frame: .zero) 65 dbTableView.refreshControl = self.refreshControl 66 self.refreshControl.addTarget(self, action: #selector(ViewController.refresh(sender:)), for: .valueChanged) 67 68 self.request() 69 } 70 71 override func didReceiveMemoryWarning() { 72 super.didReceiveMemoryWarning() 73 } 74 75 private func request(){ 76 //tableViewCell表示用の配列を一度空にする 77 self.dbItems = [] 78 79 /*ここで通信失敗になる*/ 80 Alamofire.request(url).responseString { response in 81 if let value = response.result.value{ 82 let data = value.data(using: .utf8)! 83 self.decodeJson(data: data) 84 } 85 } 86 } 87 88 private func decodeJson(data:Data){ 89 let decoder:JSONDecoder = JSONDecoder() 90 do{ 91 let json:[JsonModel] = try decoder.decode([JsonModel].self, from: data) 92 for item in json{ 93 self.dbItems.append(item) 94 } 95 //データ追加後の再読込 96 self.dbTableView.reloadData() 97 }catch{ 98 print("json convert failed in JSONDecoder", error.localizedDescription) 99 } 100 } 101 102 @objc func refresh(sender:UIRefreshControl){ 103 request() 104 sender.endRefreshing() 105 } 106 107 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 108 return dbItems.count 109 } 110 111 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 112 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 113 cell.textLabel?.text = dbItems[indexPath.row].id 114 cell.detailTextLabel?.text = dbItems[indexPath.row].name 115 116 return cell 117 } 118}
Infoplist
1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 3<plist version="1.0"> 4<dict> 5 <!--ここを追加した--> 6 <key>NSAppTransportSecurity</key> 7 <dict> 8 <key>NSAllowsArbitraryLoads</key> 9 <true/> 10 </dict> 11 <!----> 12 <key>CFBundleDevelopmentRegion</key> 13 <string>$(DEVELOPMENT_LANGUAGE)</string> 14 <key>CFBundleExecutable</key> 15 <string>$(EXECUTABLE_NAME)</string> 16 <key>CFBundleIdentifier</key> 17 <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> 18 <key>CFBundleInfoDictionaryVersion</key> 19 <string>6.0</string> 20 <key>CFBundleName</key> 21 <string>$(PRODUCT_NAME)</string> 22 <key>CFBundlePackageType</key> 23 <string>APPL</string> 24 <key>CFBundleShortVersionString</key> 25 <string>1.0</string> 26 <key>CFBundleVersion</key> 27 <string>1</string> 28 <key>LSRequiresIPhoneOS</key> 29 <true/> 30 <key>UILaunchStoryboardName</key> 31 <string>LaunchScreen</string> 32 <key>UIMainStoryboardFile</key> 33 <string>Main</string> 34 <key>UIRequiredDeviceCapabilities</key> 35 <array> 36 <string>armv7</string> 37 </array> 38 <key>UISupportedInterfaceOrientations</key> 39 <array> 40 <string>UIInterfaceOrientationPortrait</string> 41 <string>UIInterfaceOrientationLandscapeLeft</string> 42 <string>UIInterfaceOrientationLandscapeRight</string> 43 </array> 44 <key>UISupportedInterfaceOrientations~ipad</key> 45 <array> 46 <string>UIInterfaceOrientationPortrait</string> 47 <string>UIInterfaceOrientationPortraitUpsideDown</string> 48 <string>UIInterfaceOrientationLandscapeLeft</string> 49 <string>UIInterfaceOrientationLandscapeRight</string> 50 </array> 51</dict> 52</plist> 53
試したこと
こちらを参考にinfo.plistを編集しました。
回答3件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。