Skip to content

Commit 72bc1bb

Browse files
committed
feat(iOS): RN-77 Swift support
1 parent 81aa788 commit 72bc1bb

3 files changed

Lines changed: 71 additions & 23 deletions

File tree

ReactNativeNavigation.podspec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ Pod::Spec.new do |s|
2121
s.source = { :git => "https://github.com/wix/react-native-navigation.git", :tag => "#{s.version}" }
2222
s.source_files = 'lib/ios/**/*.{h,m,mm,cpp}'
2323
s.exclude_files = "lib/ios/ReactNativeNavigationTests/**/*.*", "lib/ios/OCMock/**/*.*"
24+
s.public_header_files = [
25+
'lib/ios/RNNAppDelegate.h'
26+
]
2427
end
2528

2629
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32 -DFOLLY_CFG_NO_COROUTINES=1'
2730
s.pod_target_xcconfig = {
2831
'HEADER_SEARCH_PATHS' => '"$(PODS_ROOT)/boost" "$(PODS_ROOT)/boost-for-react-native" "$(PODS_ROOT)/RCT-Folly" "$(PODS_ROOT)/Headers/Private/React-Core" "$(PODS_ROOT)/Headers/Private/Yoga"',
2932
"CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
3033
"OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1",
34+
"DEFINES_MODULE" => "YES"
3135
}
3236

3337
if fabric_enabled

autolink/postlink/appDelegateLinker.js

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,46 @@ class AppDelegateLinker {
2121

2222
logn('Linking AppDelegate...');
2323

24-
var appDelegateContents = fs.readFileSync(this.appDelegatePath, 'utf8');
24+
// New flow for Swift
25+
if (nodePath.extname(this.appDelegatePath) === '.swift') {
26+
debugn('Entering Swift flow ...');
27+
var appDelegateContents = fs.readFileSync(this.appDelegatePath, 'utf8');
28+
appDelegateContents = this._extendRNNAppDelegateSwift(appDelegateContents);
29+
fs.writeFileSync(this.appDelegatePath, appDelegateContents);
30+
this.removeUnneededImportsSuccess = true
31+
this.removeApplicationLaunchContentSuccess = true
32+
33+
} else { // Old flow for Objective-C
34+
debugn('Entering Objective-C flow ...');
35+
var appDelegateContents = fs.readFileSync(this.appDelegatePath, 'utf8');
36+
37+
if (this.appDelegateHeaderPath) {
38+
var appDelegateHeaderContents = fs.readFileSync(this.appDelegateHeaderPath, 'utf8');
39+
appDelegateHeaderContents = this._extendRNNAppDelegate(appDelegateHeaderContents);
40+
fs.writeFileSync(this.appDelegateHeaderPath, appDelegateHeaderContents);
41+
}
2542

26-
if (this.appDelegateHeaderPath) {
27-
var appDelegateHeaderContents = fs.readFileSync(this.appDelegateHeaderPath, 'utf8');
28-
appDelegateHeaderContents = this._extendRNNAppDelegate(appDelegateHeaderContents);
29-
fs.writeFileSync(this.appDelegateHeaderPath, appDelegateHeaderContents);
30-
}
43+
try {
44+
appDelegateContents = this._removeUnneededImports(appDelegateContents);
45+
this.removeUnneededImportsSuccess = true;
46+
} catch (e) {
47+
errorn(' ' + e.message);
48+
}
3149

32-
try {
33-
appDelegateContents = this._removeUnneededImports(appDelegateContents);
34-
this.removeUnneededImportsSuccess = true;
35-
} catch (e) {
36-
errorn(' ' + e.message);
37-
}
50+
appDelegateContents = this._importNavigation(appDelegateContents);
3851

39-
appDelegateContents = this._importNavigation(appDelegateContents);
52+
appDelegateContents = this._bootstrapNavigation(appDelegateContents);
4053

41-
appDelegateContents = this._bootstrapNavigation(appDelegateContents);
54+
try {
55+
appDelegateContents = this._removeApplicationLaunchContent(appDelegateContents);
56+
this.removeApplicationLaunchContentSuccess = true;
57+
} catch (e) {
58+
errorn(' ' + e.message);
59+
}
4260

43-
try {
44-
appDelegateContents = this._removeApplicationLaunchContent(appDelegateContents);
45-
this.removeApplicationLaunchContentSuccess = true;
46-
} catch (e) {
47-
errorn(' ' + e.message);
61+
fs.writeFileSync(this.appDelegatePath, appDelegateContents);
4862
}
4963

50-
fs.writeFileSync(this.appDelegatePath, appDelegateContents);
51-
5264
if (this.removeUnneededImportsSuccess && this.removeApplicationLaunchContentSuccess) {
5365
infon('AppDelegate linked successfully!\n');
5466
} else {
@@ -178,6 +190,19 @@ class AppDelegateLinker {
178190
_doesImportNavigation(content) {
179191
return /#import\s+\<ReactNativeNavigation\/ReactNativeNavigation.h>/.test(content);
180192
}
193+
194+
// SWIFT implementation
195+
_extendRNNAppDelegateSwift(content) {
196+
return content
197+
.replace(
198+
/import React_RCTAppDelegate/,
199+
'import ReactNativeNavigation'
200+
)
201+
.replace(
202+
/class AppDelegate: RCTAppDelegate/,
203+
'class AppDelegate: RNNAppDelegate'
204+
)
205+
}
181206
}
182207

183208
module.exports = AppDelegateLinker;

autolink/postlink/path.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var glob = require('glob');
22
var ignoreFolders = {
33
ignore: ['node_modules/**', '**/build/**', '**/Build/**', '**/DerivedData/**', '**/*-tvOS*/**'],
44
};
5+
var { warnn, infon, debugn } = require('./log');
56

67
exports.mainActivityJava = glob.sync('**/MainActivity.java', ignoreFolders)[0];
78
exports.mainActivityKotlin = glob.sync('**/MainActivity.kt', ignoreFolders)[0];
@@ -10,10 +11,28 @@ exports.mainApplicationJava = mainApplicationJava;
1011
exports.rootGradle = mainApplicationJava.replace(/android\/app\/.*\.java/, 'android/build.gradle');
1112

1213
var reactNativeVersion = require('../../../react-native/package.json').version;
14+
15+
infon('Locating the AppDelegate.mm file ...');
1316
exports.appDelegate = glob.sync(
14-
reactNativeVersion < '0.68.0' ? '**/AppDelegate.m' : '**/AppDelegate.mm',
17+
'**/AppDelegate.mm',
1518
ignoreFolders
1619
)[0];
17-
exports.appDelegateHeader = glob.sync('**/AppDelegate.h', ignoreFolders)[0];
20+
21+
if (exports.appDelegate === undefined) {
22+
warnn('AppDelegate.mm file not found, looking for AppDelegate.swift ...');
23+
exports.appDelegate = glob.sync(
24+
'**/AppDelegate.swift',
25+
ignoreFolders
26+
)[0];
27+
28+
if (exports.appDelegate !== undefined) {
29+
debugn('Found AppDelegate.swift');
30+
}
31+
32+
} else {
33+
debugn('Found AppDelegate.mm');
34+
exports.appDelegateHeader = glob.sync('**/AppDelegate.h', ignoreFolders)[0];
35+
}
36+
1837
exports.podFile = glob.sync('**/Podfile', ignoreFolders)[0];
1938
exports.plist = glob.sync('**/info.plist', ignoreFolders)[0];

0 commit comments

Comments
 (0)