LoginSignup
9
10

More than 5 years have passed since last update.

Macに繋いだiPhoneから、Mac上の開発サーバにHTTP接続する方法

Last updated at Posted at 2016-02-22

iPhoneアプリをローカルで開発していて、Mac上で動かしている開発サーバにhttpで接続したい時がある。
その時のやり方を覚え書き。

手順

ソースコードの中にIP指定で書いてしまうと不便なのでコンピュータ名でアクセスするようにする
まずiPhoneからアクセスするMacのコンピュータ名ローカルホスト名を取得する

Macの「システム環境設定」→「共有」→「コンピュータ名」の下の「編集」

01.png

この時、デフォルトで設定されているコンピュータ名ローカルホスト名は

xxxxxx-no-MacBook-Air.local

というように"-no-MacBook-Air"部分が付いているのでこれを編集で削除する
(この部分があるとInfo.plistでATSを無効にする設定が効かない模様)

次に、開発しているiPhoneアプリのInfo.plistに以下の設定をする

Info.plist
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>xxxxxxxx.local</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
        </dict>
    </dict>

"xxxxxxxx.local"部分は設定したコンピュータ名ローカルホスト名を記述
(NSIncludesSubdomainsは不要かもね)

コンピュータ名ローカルホスト名に"-no-MacBook-Air"部分があるとATSに引っかかり、以下の様なエラーがログに出力されるので注意

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.

 

ちなみにSierraから(?)なのか、「共有」のサービスのどれか(例えばファイル共有)にチェックが入っていないとこのローカルホスト名でLAN内からアクセスできない模様

しばらくハマった・・・

9
10
0

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
9
10