StageUp
모바일 SDK딥링크

React Native

React Native 애플리케이션에서 NBase AdStage SDK를 사용한 딥링크 구현 가이드입니다.

1. 개요

  • 딥링크(Deep Link)는 사용자를 앱의 특정 화면으로 직접 이동시키는 URL입니다.
  • AdStage SDK는 딥링크 생성, 처리, 추적 기능을 제공합니다.

2. 빠른 시작

2.1 SDK 설치

npm install nbase-ad-react-native-sdk
# 또는
yarn add nbase-ad-react-native-sdk
 
# iOS 추가 설치
cd ios && pod install

2.2 기본 설정

Android 설정

android/app/src/main/java/.../MainActivity.java:

import android.content.Intent;
import android.os.Bundle;
import com.facebook.react.ReactActivity;
import io.nbase.adapter.adstage.AdStageSDK;
 
public class MainActivity extends ReactActivity {
		@Override
		protected void onCreate(Bundle savedInstanceState) {
				super.onCreate(savedInstanceState);
 
				// API 키 설정
				AdStageSDK.setApiKey("YOUR_API_KEY");
 
				// 앱 시작 시 딥링크 처리
				handleDeepLink(getIntent());
		}
 
		@Override
		protected void onNewIntent(Intent intent) {
				super.onNewIntent(intent);
				setIntent(intent);
				handleDeepLink(intent);
		}
 
		private void handleDeepLink(Intent intent) {
				if (intent != null) {
						AdStageSDK.handleDeepLink(intent);
				}
		}
 
		@Override
		protected String getMainComponentName() {
				return "AdStageSDKReactNativeSample";
		}
}

android/app/src/main/AndroidManifest.xml:

<activity
		android:name=".MainActivity"
		android:exported="true"
		android:launchMode="singleTop">
 
		<!-- 딥링크 인텐트 필터 -->
		<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="your-app-scheme" />
		</intent-filter>
</activity>

iOS 설정

ios/<Project>/Info.plist:

<key>CFBundleURLTypes</key>
<array>
		<dict>
				<key>CFBundleURLName</key>
				<string>nbase-adstage</string>
				<key>CFBundleURLSchemes</key>
				<array>
						<string>your-app-scheme</string>
				</array>
		</dict>
		<!-- 필요 시 추가 스킴 등록 가능 -->
</array>

ios/<Project>/AppDelegate.mm:

#import "AppDelegate.h"
#import "<Project>-Swift.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
	self.moduleName = @"AdStageSDKReactNativeSample";
	self.initialProps = @{};

	// AdStage 초기화
	[AdStageInitializer load];

	return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
	// AdStage 딥링크 처리
	[AdStageInitializer handleDeepLink:url];

	return [super application:application openURL:url options:options];
}

@end

3. React Native 구현

3.1 딥링크 리스너 설정

import { AdStageSDK } from 'nbase-ad-react-native-sdk';
 
class App extends Component {
	componentDidMount() {
		this.setupDeepLinkListener();
		this.checkPendingDeepLink?.();
	}
 
	setupDeepLinkListener() {
		// AdStage 딥링크 이벤트 리스너 설정
		AdStageSDK.addDeepLinkListener((event) => {
			console.log('딥링크 수신:', event.path);
			console.log('파라미터:', event.parameters);
			// TODO: 라우팅 처리
		});
	}
}

3.2 딥링크 생성

앱 내에서 공유나 마케팅용 딥링크를 생성할 수 있습니다.

import { AdStageSDK } from 'nbase-ad-react-native-sdk';
import { Share } from 'react-native';
 
const createAndShareDeepLink = async () => {
	try {
		const deepLinkResult = await AdStageSDK.createDeepLink({
			path: '/product',
			parameters: {
				productId: '12345',
				category: 'electronics',
				utm_source: 'app_share',
				utm_medium: 'social',
			},
		});
 
		if (deepLinkResult.success) {
			console.log('생성된 딥링크:', deepLinkResult.url);
			await Share.share({
				message: `이 상품을 확인해보세요! ${deepLinkResult.url}`,
				url: deepLinkResult.url,
				title: '상품 공유',
			});
		}
	} catch (error) {
		console.error('딥링크 생성 실패:', error);
	}
};
 
// 다양한 딥링크 생성 예시
const createUserProfileDeepLink = async (userId) => {
	const result = await AdStageSDK.createDeepLink({
		path: '/user',
		parameters: { userId, from: 'profile_share' },
	});
	return result.url;
};
 
const createPromotionDeepLink = async (promotionId) => {
	const result = await AdStageSDK.createDeepLink({
		path: '/promotion',
		parameters: {
			promotionId,
			utm_campaign: 'summer_sale_2024',
			utm_medium: 'deeplink',
		},
	});
	return result.url;
};

4. 테스트 방법

4.1 ADB(Android)

# 딥링크 테스트
adb shell am start \
	-W -a android.intent.action.VIEW \
	-d "your-app-scheme://product?id=123" \
	your.package.name
 
# 파라미터 포함 딥링크 테스트
adb shell am start \
	-W -a android.intent.action.VIEW \
	-d "your-app-scheme://user?userId=456&name=test" \
	your.package.name

4.2 iOS Simulator

# iOS 시뮬레이터에서 딥링크 테스트
xcrun simctl openurl booted "your-app-scheme://product?id=123"
 
# 사파리에서 테스트
xcrun simctl openurl booted "https://your-domain.com/redirect?deeplink=your-app-scheme://product?id=123"

5. 문제 해결

딥링크가 수신되지 않음

  • URL 스킴이 AndroidManifest.xml과 Info.plist에 올바르게 설정되었는지 확인
  • 네이티브 레벨 초기화(AdStageInitializer.load())가 완료되었는지 확인
  • 딥링크 리스너가 올바르게 설정되었는지 확인

Android에서 딥링크 처리 안됨

  • MainActivity에서 onNewIntent 구현 확인
  • android:launchMode="singleTop" 설정 확인
  • 백그라운드/종료 상태 모두 테스트

iOS에서 딥링크 처리 안됨

  • application:openURL:options: 메소드 구현 확인
  • AdStageInitializer.load() 호출 확인
  • URL Schemes 설정 확인

빌드 오류

  • npm install && cd ios && pod install 재실행
  • 캐시 클리어: npx react-native start --reset-cache

목차