본문 바로가기
App Development/Android Studio

[Android Studio / App] 안드로이드 스튜디오 http 통신 허용 하기(feat. iOS http 허용 / android http 허용)

by 감자맹고우 2022. 4. 18.
728x90
반응형

최근 개인 프로젝트를 진행하기 위해 안드로이드 스튜디오를 학습하고 있습니다. 학습 도중 다른 분들도 필요할 수 있는 내용이 있어 공유드리고자 합니다.

이제는 보안상의 이유로 https를 쓰는 것이 당연시 되고있지만, 이전에는 http로 통신하였습니다. 그러다보니 프로젝트 진행 도중 아직 http로 통신해야하는 경우가 간혹 발생하곤 합니다. 그러나 이제는 브라우저 뿐 아니라 각종 디바이스에서도 http 통신을 기본적으로 막고 있기 때문에 해결방법을 찾다가 애를 먹기 일쑤입니다.

 

이를 방지하기 위해 안드로이드 스튜디오에서 개발 시, iOS와 android에 각각 설정을 추가해 http 통신을 허용하는 방법을 알아보도록 하겠습니다.

 

[ 개발 환경 ]
- OS : WINDOWS 10
- FLUTTER : 2.10.4-stable
- Android Studio : 2021.1.1 Patch 3 for Windows 64-bit

 

[ 해결 방법 ]

 

1. iOS > NSAppTransportSecurity 설정

(경로 : ios > Runner > info.plist)

 

우선 iOS입니다. iOS에서는 info.plist 파일에 다음과 같은 설정을 추가하여 http 통신을 허용할 수 있습니다.

 

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsLocalNetworking</key>
    <true/>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
</dict>

 

 

◆ 전체 코드(내부 위치 참고)

 

<?xml version="1.0" encoding="UTF-8"?>
http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>CFBundleDevelopmentRegion</key>
   <string>$(DEVELOPMENT_LANGUAGE)</string>
   <key>CFBundleDisplayName</key>
   <string>Web View Android</string>
   <key>CFBundleExecutable</key>
   <string>$(EXECUTABLE_NAME)</string>
   <key>CFBundleIdentifier</key>
   <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
   <key>CFBundleInfoDictionaryVersion</key>
   <string>6.0</string>
   <key>CFBundleName</key>
   <string>web_view_android</string>
   <key>CFBundlePackageType</key>
   <string>APPL</string>
   <key>CFBundleShortVersionString</key>
   <string>$(FLUTTER_BUILD_NAME)</string>
   <key>CFBundleSignature</key>
   <string>????</string>
   <key>CFBundleVersion</key>
   <string>$(FLUTTER_BUILD_NUMBER)</string>
   <key>LSRequiresIPhoneOS</key>
   <true/>
   <key>UILaunchStoryboardName</key>
   <string>LaunchScreen</string>
   <key>UIMainStoryboardFile</key>
   <string>Main</string>
   <key>UISupportedInterfaceOrientations</key>
   <array>
      <string>UIInterfaceOrientationPortrait</string>
      <string>UIInterfaceOrientationLandscapeLeft</string>
      <string>UIInterfaceOrientationLandscapeRight</string>
   </array>
   <key>UISupportedInterfaceOrientations~ipad</key>
   <array>
      <string>UIInterfaceOrientationPortrait</string>
      <string>UIInterfaceOrientationPortraitUpsideDown</string>
      <string>UIInterfaceOrientationLandscapeLeft</string>
      <string>UIInterfaceOrientationLandscapeRight</string>
   </array>
   <key>UIViewControllerBasedStatusBarAppearance</key>
   <false/>
   <key>NSAppTransportSecurity</key>
   <dict>
       <key>NSAllowsLocalNetworking</key>
       <true/>
       <key>NSAllowsArbitraryLoadsInWebContent</key>
       <true/>
   </dict>
</dict>
</plist>

 

 

반응형

 

2.  android > usesCleartextTraffic 설정

(경로 : android > app > src > main > AndroidManifest.xml)

 

 

(1) INTERNET 권한 설정(HTTPS 통신도 마찬가지 설정 필요)

안드로이드는 기본적으로 인터넷을 사용하기 위해 인터넷 권한 설정을 해주어야 합니다.

<uses-permission android:name="android.permission.INTERNET"/>

 

(2) usesCleartextTraffic true 설정

이후 application 에 android:usesCleartextTraffic 속성 설정을 통해 http로 통신 설정할 수 있습니다.

<application android:usesCleartextTraffic="true">

 

◆ 전체 코드(내부 위치 참고)

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.web_view_android">
    <uses-permission android:name="android.permission.INTERNET"/>
   <application
        android:label="web_view_android"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher"
        android:usesCleartextTraffic="true">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

 

이렇게 설정만 조금 추가하면 http로도 이제 통신할 수 있습니다!

 

🤞 도움이 되셨기를 바랍니다. 한 번의 클릭과 댓글은 어딘가의 누군가에게 진실로 큰 힘이 됩니다. 🐱‍🏍

 

728x90
반응형

댓글