React Native Deep Linking

React Native Deep Linking 연결

2023-10-17

딥링크

yarn add @react-native-firebase/dynamic-links
import { Linking } from 'react-native';
import dynamicLinks from '@react-native-firebase/dynamic-links';

ios

ios > [프로젝트] > AppDelegate.mm

#import <React/RCTLinkingManager.h>

{...}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
  return [RCTLinkingManager application:application openURL:url options:options];
}

xcode로 가 Identifier 프로젝트에 Bundle ID를 넣고, URL Schemes에 프로토콜을 정의한다.

android

android > app > src > main > AndroidManifest.xml

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

  <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="example.com"
      android:pathPrefix="/"
      android:scheme="https" />
  </intent-filter>

URL 처리

const handleDynamicLink = async () => {
  const initialLink = await dynamicLinks().getInitialLink();
  if (initialLink) {
    console.log('initialLink', initialLink);
  }
};

useEffect(() => {
  handleDynamicLink();
}, []);

WebView 이슈

앱이 죽어있는 상태로 외부에서 딥링크를 클릭했을 때 앱이 켜지고 해당 페이지로 이동하는데, 이때 WebView가 렌더링 되지 않는 이슈가 있다.

// 앱이 처음 시작시 초기화
useEffect(() => {
  const getInitURL = async () => {
    const initURL = await Linking.getInitialURL();
    if (initURL) {
      ...process
    }
  };
  getInitURL();
}, []);

// 앱이 열려있는 상태
useEffect(() => {
  Linking.addEventListener('url', e => {
      if(e.url){
          ...process
      }
  });
  return () => Linking.removeAllListeners('url');
}, [])