11/* eslint-disable no-console */
22import {
3- ConfigPlugin ,
4- WarningAggregator ,
3+ type ConfigPlugin ,
54 withAppDelegate ,
65 withInfoPlist ,
76 withXcodeProject ,
87} from '@expo/config-plugins' ;
9- import { KakaoLoginPluginProps } from '..' ;
8+ import { insertContentsInsideSwiftFunctionBlock } from '@expo/config-plugins/build/ios/codeMod' ;
9+ import type { KakaoLoginPluginProps } from '..' ;
1010import { readFile , writeFile } from 'node:fs' ;
1111
1212const KAKAO_SCHEMES = [ 'kakaokompassauth' , 'storykompassauth' , 'kakaolink' ] ;
@@ -17,6 +17,11 @@ const KAKAO_LINKING_STRING = `if([RNKakaoLogins isKakaoTalkLoginUrl:url]) {
1717 return [RNKakaoLogins handleOpenUrl: url];
1818}` ;
1919
20+ const SWIFT_KAKAO_HEADER_IMPORT_STRING = 'import kakao_login' ;
21+
22+ const SWIFT_KAKAO_LINKING_STRING =
23+ 'if kakao_login.RNKakaoLogins.isKakaoTalkLoginUrl(url) { return kakao_login.RNKakaoLogins.handleOpen(url) }' ;
24+
2025const KAKAO_SDK_VERSION_STRING = '$KakaoSDKVersion' ;
2126
2227const KAKAO_SDK_VERSION_REGEX = / \$ K a k a o S D K V e r s i o n \= .* ( \r \n | \r | \n ) / g;
@@ -95,7 +100,7 @@ const modifyAppDelegate: ConfigPlugin = (config) => {
95100 if ( ! contents . includes ( KAKAO_HEADER_IMPORT_STRING ) ) {
96101 contents = contents . replace (
97102 '#import <React/RCTLinkingManager.h>' ,
98- ' #import <React/RCTLinkingManager.h>\n' + KAKAO_HEADER_IMPORT_STRING ,
103+ ` #import <React/RCTLinkingManager.h>\n${ KAKAO_HEADER_IMPORT_STRING } ` ,
99104 ) ;
100105 }
101106
@@ -110,13 +115,32 @@ const modifyAppDelegate: ConfigPlugin = (config) => {
110115 return contents ;
111116 } ;
112117
118+ const modifySwiftContents = ( contents : string ) : string => {
119+ if ( ! contents . includes ( SWIFT_KAKAO_HEADER_IMPORT_STRING ) ) {
120+ contents = contents . replace (
121+ 'import Expo' ,
122+ `import Expo\n${ SWIFT_KAKAO_HEADER_IMPORT_STRING } ` ,
123+ ) ;
124+ }
125+
126+ if ( ! contents . includes ( SWIFT_KAKAO_LINKING_STRING ) ) {
127+ contents = insertContentsInsideSwiftFunctionBlock (
128+ contents ,
129+ 'application(_:open:options:)' ,
130+ SWIFT_KAKAO_LINKING_STRING ,
131+ { position : 'head' } ,
132+ ) ;
133+ }
134+
135+ return contents ;
136+ } ;
137+
113138 return withAppDelegate ( config , ( props ) => {
114139 if ( [ 'objc' , 'objcpp' ] . includes ( props . modResults . language ) ) {
115140 props . modResults . contents = modifyContents ( props . modResults . contents ) ;
116141 } else {
117- WarningAggregator . addWarningIOS (
118- 'withFacebook' ,
119- 'Swift AppDelegate files are not supported yet.' ,
142+ props . modResults . contents = modifySwiftContents (
143+ props . modResults . contents ,
120144 ) ;
121145 }
122146
0 commit comments