본문 바로가기
🖥 Programming/📱 Android (Kotlin)

[Andorid][kotlin] DeepLink 딥링크 알아보기 - 2편

by MinChan-Youn 2022. 2. 22.

안녕하세요! 챠니입니다~

오늘은 DeepLink(딥링크)에 대해서 한번 알아보겠습니다~

 

딥링크란 무엇인가?

딥링크란 모바일 어플리케이션의 특정 페이지에 도달 할 수 있는 링크를 말합니다.

여기서 알아야하는 점은 웹(www) 어플리케이션이 http:// 또는 https:// 프로토콜을 가지고 있는 것 처럼 모바일 어플리케이션에도 각자의 프로토콜을 가지고 있다는 점입니다.

 

그래서 우리는 이러한 각 앱의 딥링크를 설정함으로써 내가 설정한 앱으로 바로 이동할 수 있도록 도와주는 역할을 하게 되는 것입니다!

 

 

딥링크

왜 사람들은 딥링크에 대해서 주목을 할까요?

그것은 바로 빅데이터 시대에서 데이터는 곧 경쟁력이기 때문입니다. 하나하나 검색하면서 이동하는 경우도 있지만 이제는 그런 부분을 바로바로 제공 할 수 있는 편리한 서비스를 계획하다보니 딥링크도 나온 부분이 아닐까 싶어요~

 

URI 스킴 방식은 Scheme://Path라는 두개의 요소로 구성됩니다.
  • Scheme://Path
  • Scheme = 앱을 특정함 (트위터)
  • Path = 앱 내 페이지를 특정함 (트위터 내 회원가입 페이지)

 

딥링크에서는 가장크게 호출과 수신부분으로 나뉠 수 있는데요~

호출 및 수신 모두 간단한 앱을 만들어서 확인을 해보는 시간을 가져보도록 하겠습니다~

어렵지 않으니 하나하나 차근차근보면서 따라오세요~

 

 

이번 2편에서는 딥링크 수신하는 앱을 만들어 보면서 어떻게 딥링크 데이터를 수신하는지 알아보겠습니다.

1. DeepLink 수신

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.deeplink_receive">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.DeepLinkreceive">

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <!-- 시작 지점 -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <!-- DeepLink -->
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="deeplink" android:host="host"/>
            </intent-filter>
        </activity>

    </application>

</manifest>

중간에 있는 <intent-filter>부분이 중요합니다.

1편에서 설정한 것처럼 우리는 scheme를 "deeplink"로 host를 "host"로 설정하여 다음과 해당되는 딥링크를 바로 수신할 수 있는 설정을 해줍니다.

위에 코드를 작성하지 않으면 앱에서 딥링크를 수신할 수 없으니 빠트리는 일이 발생하지 않도록 주의해 주세요!

 

MainActivity.kt

class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initDeepLink()
    }

    /** DeepLink */
    private fun initDeepLink() {
        if (Intent.ACTION_VIEW.equals(intent.action)) {
            var uri = intent.data
            if (uri != null) {
                //TODO: "deeplink://host?data1=DeepLinkData1&data2=DeepLinkData2"
                var dl_data1 = uri.getQueryParameter("data1")
                var dl_data2 = uri.getQueryParameter("data2")

                binding.tvDeeplinkReceive.text = "딥링크 수신받은 값\n" +
                        "dl_data1: $dl_data1 \n" +
                        "dl_data2: $dl_data2"
            }
        }
    }
}

딥링크 수신하는 코드입니다.

해당되는 URL은 다음으로 지정하여 테스트를 진행했습니다.

 

deeplink://host?data1=DeepLinkData1&data2=DeepLinkData2

 

위에서도 알 수 있듯이 Scheme는 deeplink / Path는 host로 지정하고 data1과 data2에 각각 데이터를 담은 링크를 호출한 값을 수신하는 코드라고 생각하시면 됩니다.

 

 

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_deeplink_receive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:padding="20dp"
        android:background="#F0F0F0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

build.gradle(:app)

...

	// ViewBinding 설정
    buildFeatures {
        viewBinding true
    }
    
...

 

 

 

앱 화면

여기까지 따라오셨다면 여러분은 딥링크에 대해서 조금 더 이해를 하셨다고 볼 수 있습니다.

완성된 앱을 사진을 통해서 보도록 하겠습니다.

 

딥링크 수신 전
딥링크 수신 후

 

 

 

 

글 정리 & 소스코드

[정리]

딥링크 호출: https://minchanyoun.tistory.com/96

딥링크 수신: https://minchanyoun.tistory.com/97

 

[Andorid][kotlin] DeepLink 딥링크 알아보기 - 1편

안녕하세요! 챠니입니다~ 오늘은 DeepLink(딥링크)에 대해서 한번 알아보겠습니다~ 딥링크란 무엇인가? 딥링크란 모바일 어플리케이션의 특정 페이지에 도달 할 수 있는 링크를 말합니다. 여기서

minchanyoun.tistory.com

 

[소스코드]
호출: https://github.com/younminchan/kotlin-study/tree/main/DeepLinkCall_kotlin

수신: https://github.com/younminchan/kotlin-study/tree/main/DeepLinkreceive_kotlin

 

GitHub - younminchan/kotlin-study: kotlin-example

kotlin-example. Contribute to younminchan/kotlin-study development by creating an account on GitHub.

github.com

 

 

 

질문 또는 궁굼한 부분은 댓글을 남겨주세요! 친절하게 답변드리겠습니다!

응원의 댓글은 저에게 큰 힘이 된답니다! :)

즐거운 하루되세요!

 

깃허브 보러 놀러오세요 👇 (맞팔환영)

https://github.com/younminchan

 

younminchan - Overview

Android Developer. younminchan has 6 repositories available. Follow their code on GitHub.

github.com