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

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

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

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

Q&A

解決済

1回答

1530閲覧

URLから画像を表示したい

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

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

0グッド

0クリップ

投稿2021/07/13 05:39

編集2021/07/13 10:40

前提・実現したいこと

TwitterAPI経由で画像を取得しています。
画像のURLは取得出来たのでセルに渡して画像を表示しようとすると失敗します。

発生している問題・エラーメッセージ

.. App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file. .. Cannot start load of Task <..>.<0> since it does not conform to ATS policy .. NSURLConnection finished with error - code -1022

該当のソースコード

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "...TableViewCell", for: indexPath) as! ...TableViewCell cell.idLabel.text = "(indexPath.row + 1)" cell.titleLabel.text = followers[indexPath.row].name cell.name.text = followers[indexPath.row].screenName let urlString = followers[indexPath.row].profile_image_url let url = URL(string: urlString) do{ let data = try Data(contentsOf: url!) cell.userImage.image = UIImage(data: data) } catch { print("failed to get image") } return cell }

試したこと

上記のエラー内容をもとにinfo.plstに特定のドメインのみhttp許可を追記しました。

<dict> <key>NSExceptionDomains</key> <dict> <key>twimg.com</key> <dict> <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> <true/> </dict> </dict> </dict> </dict> </plist>

イメージ説明

それでもエラー内容は変わらずです。

  • ドメインの指定の仕方が間違っているのでしょうか?

追加情報

AlamofireImage を追加して

cell.userImage.af.setImage(withURL: URL(string: urlString)!)

としても画像は表示されず同じようなエラーが出ます

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2021/07/13 09:25

ありがとうございます 早速 `AlamofireImage` 使ってみたのですが画像は相変らず表示されず.. エラー内容が Task <...>.<1> finished with error [-1022] Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection., NSErrorFailingURLStringKey=http://pbs.twimg.com/profile_images/1037549892531572736/uZFclc7I_normal.jpg, NSErrorFailingURLKey=http://pbs.twimg.com/profile_images/1037549892531572736/uZFclc7I_normal.jpg, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <...>.<1>" ), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <...>.<1>, NSUnderlyingError=0x600003546f10 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}} と出ます
退会済みユーザー

退会済みユーザー

2021/07/13 09:25

info.plstは <key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>http://pbs.twimg.com</key> <dict> <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> <true/> </dict> </dict> </dict>  に変更しました。
guest

回答1

0

ベストアンサー

解決しました。
ローカル環境で読み込むには各種設定が必要で色々な記事見ましたがこの記事のinfo.plstの記述が一番正確でした。

https://qiita.com/uhooi/items/68939999c2c31e5f5557

投稿2021/07/14 09:43

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問