본문 바로가기
Study/Mobile

[Kotlin] 딥링크 (DeepLink)

by Becoming a Hacker 2022. 8. 7.
반응형

딥링크 (DeepLink)

딥링크란 단순하게 특정 페이지 또는 특정 콘텐츠에 직접적으로 도달할 수 모든 링크를 의미합니다.

 

그리고 Android에서 딥링크란 Custom Scheme와 같은 특정 주소를 통하여 앱을 실행하거나 앱의 특정 화면을 호출할 수 있는 기능을 의미합니다.

 

Android에서 DeepLink를 구현하기 위해서는 먼저 Custom Scheme과 host를 설정해야 합니다.

Scheme:host:port/path = https://hacksms.tistory.com:443/14

 

Manifest.xml

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

<activity
	android:name=".SecondActivity"
	android:exported="true"
	android:label="@string/app_name"
	android:theme="@style/Theme.VulnApp.NoActionBar">
	    <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:host="deeplink" android:scheme="hacksms"/>
	    </intent-filter>
</activity>

 

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
        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">

     <WebView
         android:id="@+id/webView"
         android:layout_width="match_parent"
         android:layout_height="match_parent"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

 

SecondActivity.kt

class SecondActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        intent?.let{
            if(Intent.ACTION_VIEW.equals(intent.getAction())){
                val uri = intent.data
                if(uri != null){
                    val url:String? = uri.getQueryParameter("url")
                    println(url)
                    if(url != null){
                        val webView = findViewById<WebView>(R.id.webView)
                        webView.settings.apply{
                            // Enable Javascript Via WebView, Default is disabled
                            javaScriptEnabled = true
                        }
                        // 2rd Argument is Selector Name
                        webView.addJavascriptInterface(WebAppInterface(this),"Hacksms")
                        webView.webViewClient = WebViewClient()
                        // webView.webChromeClient = WebChromeClient()
                        webView.loadUrl(url)

                    }
                }
            }
        }
    }

    class WebAppInterface(private val mContext:Context){
        @JavascriptInterface
        fun showAlert(){
            Toast.makeText(mContext, "HackSms", Toast.LENGTH_SHORT).show()
        }
    }
}

 

위 예제 코드로 App을 설치 후 "hacksms://deeplink?url=https://hacksms.tistory.com" URI로 접속하게 되면 SecondActivity가 호출되어 WebView에 https://hacksms.tistory.com URL을 로드하게 됩니다. 

DeepLink 동작 화면

 

참고로 adb의 am 명령어를 통하여 DeepLink에 대한 테스트를 진행할 수 있습니다.

am start -a android.intent.action.VIEW -d "hacksms://deeplink?url=https://hacksms.tistory.com"
반응형

'Study > Mobile' 카테고리의 다른 글

[Kotlin] WebView 사용 방법  (0) 2022.08.07
[Android] 4대 Component  (0) 2022.08.05
iOS Application Life Cycle  (0) 2022.03.18
[Kotlin] ROOM Database 사용 방법  (0) 2022.01.10
[Kotlin] Intellij에서 Anroid 세팅  (0) 2022.01.04

댓글