iOS에서 Universal 링크, Android에서 App 링크를 사용하면 사용자가 링크를 클릭하여 앱으로 바로 이동할 수 있습니다. 브라우저를 피하고 웹 페이지에서 앱으로의 컨텍스트를 유지하는 데 유용합니다. 이 안내서에서는 Next.js 앱에 Capacitor를 사용하여 이러한 깊은 링크를 설정하는 방법에 대해 설명합니다.
깊은 링크를 설정하는 데 code가 많이 필요하지 않지만, 설정을 위한 몇 가지 구성이 필요합니다. 이 안내서를 읽고 나면, 앱이 설치된 경우 링크를 클릭하여 올바른 페이지로 앱이 열리는 것을 확인할 수 있습니다. https://www.capgo.app/details/22 Next.js 깊은 링크 설정
첫 번째로, 새로운 Next.js 앱과 테스트를 위한 디테일 페이지를 생성합니다.
__CAPGO_KEEP_0__
npx create-next-app@latest capgoLinks
cd capgoLinks
mkdir pages/details
touch pages/details/[id].js
npm run build
npx cap add ios
npx cap add android
설정에 사용되는 bundle ID 가 __CAPGO_KEEP_0__.config.json capacitor.config.json 라우팅을 위해 Next.js는 파일 기반 라우팅을 사용합니다. 따라서
{
"appId": "com.capgo.deeplinks",
"appName": "capgoLinks",
"webDir": "out",
"bundledWebRuntime": false
}
파일을 생성하면 wildcard route를 이미 설정했습니다. pages/details/[id].js에서 URL에서 ID를 가져올 수 있습니다. Next.js의 내장 라우터를 사용하여:
이벤트를 처리하기 위해 pages/details/[id].js를 사용합니다. 이 이벤트는 앱이 커스텀 URL scheme를 통해 열릴 때 트리거됩니다.
import { useRouter } from 'next/router'
function DetailsPage() {
const router = useRouter()
const { id } = router.query
return (
<div>
<p>My ID: {id}</p>
</div>
)
}
export default DetailsPage
파일에 리스너를 추가하여: appUrlOpen event with Capacitor. This event is triggered when the app is opened via a custom URL scheme. Add a listener in the pages/_app.js __CAPGO_KEEP_0__
import { useEffect } from 'react'
import { App } from '@capacitor/app'
function MyApp({ Component, pageProps }) {
useEffect(() => {
App.addListener('appUrlOpen', (event) => {
const slug = event.url.split('.app').pop()
if (slug)
window.location.href = slug
})
}, [])
return <Component {...pageProps} />
}
export default MyApp
이 code은 이벤트를 감지하고 Next.js 앱 내에서 적절한 경로로 이동합니다. appUrlOpen iOS 설정
iOS에서 iOS 앱 ID에 Associated Domains 기능이 활성화되어야 합니다. Associated Domains 기능이 활성화된 앱 ID를 생성하세요. 다음 내용으로 apple-app-site-association 파일을 생성하세요. team ID와 bundle ID를 실제로 입력하세요.
이 파일을 도메인에 있는 directory에 업로드하세요. 이 파일은 도메인에서 접근할 수 있도록 하세요. Xcode에서 앱의 권한에 도메인을 추가하세요. 다음 형식으로 도메인을 추가하세요. Android 설정 YOURTEAMID For Android, you’ll need to add the following configuration to your app’s AndroidManifest.xml file, replacing __CAPGO_KEEP_0__ with your actual package name: com.your.bundleid Upload this file to the directory on your domain, making it accessible at
{
"applinks": {
"apps": [],
"details": [
{
"appID": "YOURTEAMID.com.your.bundleid",
"paths": ["*"]
}
]
}
}
In Android Studio, add the domain to your app’s manifest using the format .well-known In the Firebase console, add the domain to your app’s hosting settings using the format https://www.capgo.app/.well-known/apple-app-site-association.
In the Google Cloud Console, add the domain to your app’s hosting settings using the format applinks:capgo.app.
In the Vercel console, add the domain to your app’s hosting settings using the format
For Android App Links, follow these steps:
- Android 앱 링크를 위해 다음 단계를 따르세요:
- Generate a keystore file if you don’t have one.
- 키 스토어 파일을 생성하세요. (이미 파일이 있으면 생략하세요.) Obtain the SHA256 fingerprint from the keystore. 키 스토어에서 SHA256 지문 값을 얻으세요.
- Create an
.well-knownassetlinks.json
파일을 생성하세요. (package name과 SHA256 지문 값을 포함해야 합니다.) AndroidManifest.xmlUpload this file to the intent-filter 디렉토리에서 이 파일을 업로드하세요. activity In your
<activity ...>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="capgo.app" />
</intent-filter>
</activity>
파일을 업로드한 후, Google의 디지털 자산 링크 도구를 사용하여 확인할 수 있습니다. 모든 설정이 올바르게 구성되어 있다면 초록 체크 표시를 볼 수 있습니다. assetlinks.json 파일을 올바르게 업로드했다면 초록 체크 표시를 볼 수 있습니다.
앱을 빌드하고 서명하려면 다음 명령어를 사용하세요:
cd android
./gradlew assembleRelease
cd ..
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore android/app/build/outputs/apk/release/app-release-unsigned.apk alias_name
zipalign -v 4 android/app/build/outputs/apk/release/app-release-unsigned.apk capgo.apk
adb install capgo.apk
__CAPGO_KEEP_0__
Capacitor Configure for Native Project Settings
__CAPGO_KEEP_0__ Capacitor configure package__CAPGO_KEEP_0__
npm install @capacitor/configure
configure 패키지를 고려하세요. capacitor.config.yaml configure 패키지를 프로젝트에 설치하세요:
vars:
BUNDLE_ID: com.capgo.deeplinks
PACKAGE_NAME: com.capgo.deeplinks
platforms:
ios:
targets:
App:
bundleId: $BUNDLE_ID
entitlements:
- com.apple.developer.associated-domains: ['applinks:capgo.app']
android:
packageName: $PACKAGE_NAME
프로젝트의 루트 디렉토리에
npx cap-config run capacitor.config.yaml
파일을 생성하세요:
__CAPGO_KEEP_0__ 설정을 통해 Next.js 앱에 깊이 있는 링크를 설정하는 것은 iOS와 Android 모두에 대한 도메인 및 프로젝트 설정을 구성하는 것입니다. 이 과정을 수행하는 데는 주의가 필요하지만, 이전 방법에 비해 더 간소화되어 추가 플러그인을 설치할 필요가 없습니다. 도메인 인증 파일이 올바르게 제공되고, 각 플랫폼의 도구를 사용하여 확인하는 것을 확인하세요. 설정이 완료되면 앱은 웹 링크에서 무방비적으로 열리며, 사용자가 웹에서 앱으로의 전환을 위한 smooth한 경험을 제공합니다.
Capacitor