Skip to content

Commit ed0289e

Browse files
authored
feat: add native swift option (#55)
This PR adds the option to create a native module with swift for iOS. Closes #54
1 parent 8d29c92 commit ed0289e

File tree

6 files changed

+58
-4
lines changed

6 files changed

+58
-4
lines changed

src/create.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const NATIVE_FILES = path.resolve(__dirname, '../templates/native-library');
1717
const EXPO_FILES = path.resolve(__dirname, '../templates/expo-library');
1818
const CPP_FILES = path.resolve(__dirname, '../templates/cpp-library');
1919
const OBJC_FILES = path.resolve(__dirname, '../templates/objc-library');
20+
const SWIFT_FILES = path.resolve(__dirname, '../templates/swift-library');
2021

2122
type ArgName =
2223
| 'slug'
@@ -34,7 +35,7 @@ type Answers = {
3435
authorEmail: string;
3536
authorUrl: string;
3637
repoUrl: string;
37-
type: 'native' | 'cpp' | 'expo';
38+
type: 'native' | 'native-swift' | 'cpp' | 'expo';
3839
};
3940

4041
export const args: Record<ArgName, yargs.Options> = {
@@ -64,7 +65,7 @@ export const args: Record<ArgName, yargs.Options> = {
6465
},
6566
'type': {
6667
description: 'Type package do you want to develop',
67-
choices: ['native', 'cpp', 'expo'],
68+
choices: ['native', 'native-swift', 'cpp', 'expo'],
6869
},
6970
};
7071

@@ -166,6 +167,7 @@ export default async function create(argv: yargs.Arguments<any>) {
166167
// @ts-ignore - seems types are wrong for inquirer
167168
choices: [
168169
{ name: 'Native module in Kotlin and Objective-C', value: 'native' },
170+
{ name: 'Native module in Kotlin and Swift', value: 'native-swift' },
169171
{ name: 'Native module with C++ code', value: 'cpp' },
170172
{
171173
name: 'JavaScript module with Web support using Expo',
@@ -218,8 +220,9 @@ export default async function create(argv: yargs.Arguments<any>) {
218220
.slice(1)}`,
219221
package: slug.replace(/[^a-z0-9]/g, '').toLowerCase(),
220222
podspec: slug.replace(/[^a-z0-9]+/g, '-').replace(/^-/, ''),
221-
native: type === 'native' || type === 'cpp',
223+
native: type === 'native' || type === 'cpp' || 'native-swift',
222224
cpp: type === 'cpp',
225+
swift: type === 'native-swift',
223226
},
224227
author: {
225228
name: authorName,
@@ -261,6 +264,8 @@ export default async function create(argv: yargs.Arguments<any>) {
261264

262265
if (type === 'cpp') {
263266
await copyDir(CPP_FILES, folder);
267+
} else if (type === 'native-swift') {
268+
await copyDir(SWIFT_FILES, folder);
264269
} else {
265270
await copyDir(OBJC_FILES, folder);
266271
}

templates/native-library/<%= project.podspec %>.podspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Pod::Spec.new do |s|
1515

1616
<% if(project.cpp){ %>
1717
s.source_files = "ios/**/*.{h,m,mm}", "cpp/**/*.{h,cpp}"
18-
<% } else{ %>
18+
<% } else if(project.swift){ %>
19+
s.source_files = "ios/**/*.{h,m,mm,swift}"
20+
<% } else {%>
1921
s.source_files = "ios/**/*.{h,m,mm}"
2022
<% } %>
2123

templates/native-library/ios/<%= project.name %>.xcodeproj/project.pbxproj

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
/* Begin PBXBuildFile section */
1010
<% if (project.cpp) {%>
1111
5E46D8CD2428F78900513E24 /* example.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5E46D8CB2428F78900513E24 /* example.cpp */; };
12+
<% } %>
13+
<% if (project.swift) {%>
14+
F4FF95D7245B92E800C19C63 /* <%= project.name %>.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4FF95D6245B92E800C19C63 /* <%= project.name %>.swift */; };
1215
<% } %>
1316
5E555C0D2413F4C50049A1A2 /* <%= project.name %>.mm in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* <%= project.name %>.mm */; };
1417
/* End PBXBuildFile section */
@@ -31,8 +34,14 @@
3134
5E46D8CB2428F78900513E24 /* example.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = example.cpp; path = ../cpp/example.cpp; sourceTree = "<group>"; };
3235
5E46D8CC2428F78900513E24 /* example.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = example.h; path = ../cpp/example.h; sourceTree = "<group>"; };
3336
<% } %>
37+
<% if (project.swift) {%>
38+
B3E7B5891CC2AC0600A0062D /* <%= project.name %>.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = <%= project.name %>.m; sourceTree = "<group>"; };
39+
F4FF95D5245B92E700C19C63 /* <%= project.name %>-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "<%= project.name %>-Bridging-Header.h"; sourceTree = "<group>"; };
40+
F4FF95D6245B92E800C19C63 /* <%= project.name %>.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = <%= project.name %>.swift; sourceTree = "<group>"; };
41+
<% } else { %>
3442
B3E7B5881CC2AC0600A0062D /* <%= project.name %>.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = <%= project.name %>.h; sourceTree = "<group>"; };
3543
B3E7B5891CC2AC0600A0062D /* <%= project.name %>.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = <%= project.name %>.mm; sourceTree = "<group>"; };
44+
<% } %>
3645
/* End PBXFileReference section */
3746

3847
/* Begin PBXFrameworksBuildPhase section */
@@ -61,8 +70,14 @@
6170
5E46D8CB2428F78900513E24 /* example.cpp */,
6271
5E46D8CC2428F78900513E24 /* example.h */,
6372
<% } %>
73+
<% if (project.swift) {%>
74+
F4FF95D6245B92E800C19C63 /* <%= project.name %>.swift */,
75+
B3E7B5891CC2AC0600A0062D /* <%= project.name %>.m */,
76+
F4FF95D5245B92E700C19C63 /* <%= project.name %>-Bridging-Header.h */,
77+
<% } else {%>
6478
B3E7B5881CC2AC0600A0062D /* <%= project.name %>.h */,
6579
B3E7B5891CC2AC0600A0062D /* <%= project.name %>.mm */,
80+
<% } %>
6681
134814211AA4EA7D00B7C361 /* Products */,
6782
);
6883
sourceTree = "<group>";
@@ -127,7 +142,12 @@
127142
<% if (project.cpp) {%>
128143
5E46D8CD2428F78900513E24 /* example.cpp in Sources */,
129144
<% } %>
145+
<% if (project.swift) {%>
146+
F4FF95D7245B92E800C19C63 /* <%= project.name %>.swift in Sources */,
147+
B3E7B58A1CC2AC0600A0062D /* <%= project.name %>.m in Sources */,
148+
<% } else { %>
130149
5E555C0D2413F4C50049A1A2 /* <%= project.name %>.mm in Sources */,
150+
<% } %>
131151
);
132152
runOnlyForDeploymentPostprocessing = 0;
133153
};
@@ -240,6 +260,11 @@
240260
OTHER_LDFLAGS = "-ObjC";
241261
PRODUCT_NAME = <%= project.name %>;
242262
SKIP_INSTALL = YES;
263+
<% if (project.swift) {%>
264+
SWIFT_OBJC_BRIDGING_HEADER = "<%= project.name %>-Bridging-Header.h";
265+
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
266+
SWIFT_VERSION = 5.0;
267+
<% } %>
243268
};
244269
name = Debug;
245270
};
@@ -256,6 +281,10 @@
256281
OTHER_LDFLAGS = "-ObjC";
257282
PRODUCT_NAME = <%= project.name %>;
258283
SKIP_INSTALL = YES;
284+
<% if (project.swift) {%>
285+
SWIFT_OBJC_BRIDGING_HEADER = "<%= project.name %>-Bridging-Header.h";
286+
SWIFT_VERSION = 5.0;
287+
<% } %>
259288
};
260289
name = Release;
261290
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@objc(<%= project.name %>)
2+
class <%= project.name %>: NSObject {
3+
4+
@objc(multiply:withB:withResolver:withRejecter:)
5+
func multiply(a: Float, b: Float, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
6+
resolve(a*b)
7+
}
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#import <React/RCTBridgeModule.h>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#import <React/RCTBridgeModule.h>
2+
3+
@interface RCT_EXTERN_MODULE(<%= project.name %>, NSObject)
4+
5+
RCT_EXTERN_METHOD(multiply:(float)a withB:(float)b
6+
withResolver:(RCTPromiseResolveBlock)resolve
7+
withRejecter:(RCTPromiseRejectBlock)reject)
8+
9+
@end

0 commit comments

Comments
 (0)