Skip to content

Commit feaed51

Browse files
committed
feat: rewrite to Swift
1 parent c5ce25d commit feaed51

16 files changed

+178
-225
lines changed

ReactNativeBrownfield.podspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Pod::Spec.new do |spec|
2020
spec.source = { :path => "." }
2121
spec.source_files = "ios/**/*.{h,m,mm,swift}"
2222
spec.compiler_flags = new_arch_enabled_flag
23-
spec.pod_target_xcconfig = { 'OTHER_CPLUSPLUSFLAGS' => other_cflags }
23+
spec.pod_target_xcconfig = { 'OTHER_CPLUSPLUSFLAGS' => other_cflags, 'DEFINES_MODULE' => 'YES' }
2424

2525
install_modules_dependencies(spec)
26+
spec.dependency 'ReactAppDependencyProvider'
2627
end

example/swift/AppDelegate.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import UIKit
22
import SwiftUI
3+
import ReactNativeBrownfield
34

45
class AppDelegate: UIResponder, UIApplicationDelegate {
56
var window: UIWindow?
67

78
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
89
// Override point for customization after application launch.
9-
ReactNativeBrownfield.shared().entryFile = "index"
10-
ReactNativeBrownfield.shared().startReactNative {
10+
ReactNativeBrownfield.shared.entryFile = "index"
11+
ReactNativeBrownfield.shared.startReactNative {
1112
print("loaded")
1213
}
1314

@@ -45,6 +46,7 @@ struct ContentView: View {
4546
.font(.title)
4647
.bold()
4748
.padding()
49+
4850
NavigationLink("Push React Native Screen") {
4951
UIViewControllerWrapper(viewController: ReactNativeViewController(moduleName: "ReactNative"))
5052
}

example/swift/BridgingHeader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
// Copyright © 2019 Callstack. All rights reserved.
77
//
88

9-
#import <ReactNativeBrownfield/ReactNativeBrownfield.h>
10-
#import <ReactNativeBrownfield/ReactNativeViewController.h>
9+
//#import <ReactNativeBrownfield/ReactNativeBrownfield.h>
10+
//#import <ReactNativeBrownfield/ReactNativeViewController.h>

example/swift/Podfile.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,7 @@ PODS:
16081608
- React-RCTFabric
16091609
- React-rendererdebug
16101610
- React-utils
1611+
- ReactAppDependencyProvider
16111612
- ReactCodegen
16121613
- ReactCommon/turbomodule/bridging
16131614
- ReactCommon/turbomodule/core
@@ -1945,7 +1946,7 @@ SPEC CHECKSUMS:
19451946
ReactAppDependencyProvider: a1fb08dfdc7ebc387b2e54cfc9decd283ed821d8
19461947
ReactCodegen: 008c319179d681a6a00966edfc67fda68f9fbb2e
19471948
ReactCommon: 0c097b53f03d6bf166edbcd0915da32f3015dd90
1948-
ReactNativeBrownfield: f76cefbba610e3c1d2c48d07da33fc1ef52be259
1949+
ReactNativeBrownfield: 4cbb58bb8076bf4e4c4a9769ada50f6c6db1f6a7
19491950
RNScreens: 0d4cb9afe052607ad0aa71f645a88bb7c7f2e64c
19501951
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
19511952
Yoga: afd04ff05ebe0121a00c468a8a3c8080221cb14c

example/swift/SwiftExample.xcodeproj/project.pbxproj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,10 @@
346346
ONLY_ACTIVE_ARCH = YES;
347347
OTHER_CFLAGS = "$(inherited)";
348348
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
349-
OTHER_LDFLAGS = "$(inherited) ";
349+
OTHER_LDFLAGS = (
350+
"$(inherited)",
351+
" ",
352+
);
350353
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
351354
SDKROOT = iphoneos;
352355
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) DEBUG";
@@ -410,7 +413,10 @@
410413
MTL_FAST_MATH = YES;
411414
OTHER_CFLAGS = "$(inherited)";
412415
OTHER_CPLUSPLUSFLAGS = "$(inherited)";
413-
OTHER_LDFLAGS = "$(inherited) ";
416+
OTHER_LDFLAGS = (
417+
"$(inherited)",
418+
" ",
419+
);
414420
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
415421
SDKROOT = iphoneos;
416422
SWIFT_COMPILATION_MODE = wholemodule;
@@ -428,7 +434,7 @@
428434
CODE_SIGN_STYLE = Automatic;
429435
DEVELOPMENT_TEAM = PJL7DA2X5F;
430436
INFOPLIST_FILE = Info.plist;
431-
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
437+
IPHONEOS_DEPLOYMENT_TARGET = 15.6;
432438
LD_RUNPATH_SEARCH_PATHS = (
433439
"$(inherited)",
434440
"@executable_path/Frameworks",
@@ -449,7 +455,7 @@
449455
CODE_SIGN_STYLE = Automatic;
450456
DEVELOPMENT_TEAM = PJL7DA2X5F;
451457
INFOPLIST_FILE = Info.plist;
452-
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
458+
IPHONEOS_DEPLOYMENT_TARGET = 15.6;
453459
LD_RUNPATH_SEARCH_PATHS = (
454460
"$(inherited)",
455461
"@executable_path/Frameworks",

example/swift/ViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import UIKit
2+
import ReactNativeBrownfield
23

34
class ViewController: UIViewController {
45
override func viewDidLoad() {

ios/ReactNativeBrownfield.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

ios/ReactNativeBrownfield.mm

Lines changed: 0 additions & 87 deletions
This file was deleted.

ios/ReactNativeBrownfield.swift

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import React
2+
import React
3+
import React_RCTAppDelegate
4+
import ReactAppDependencyProvider
5+
6+
@objc public class ReactNativeBrownfield: RCTDefaultReactNativeFactoryDelegate {
7+
@objc public static let shared = ReactNativeBrownfield()
8+
9+
@objc public var entryFile: String
10+
@objc public var fallbackResource: String?
11+
@objc public var bundlePath: String
12+
@objc public var reactNativeFactory: RCTReactNativeFactory?
13+
private var onBundleLoaded: (() -> Void)?
14+
15+
private override init() {
16+
self.entryFile = "index"
17+
self.fallbackResource = nil
18+
self.bundlePath = "main.jsbundle"
19+
super.init()
20+
}
21+
22+
@objc public func startReactNative() {
23+
startReactNative(onBundleLoaded: nil)
24+
}
25+
26+
@objc public func startReactNative(onBundleLoaded: (() -> Void)?) {
27+
startReactNative(onBundleLoaded: onBundleLoaded, launchOptions: nil)
28+
}
29+
30+
@objc public func startReactNative(onBundleLoaded: (() -> Void)?, launchOptions: [AnyHashable: Any]?) {
31+
guard reactNativeFactory == nil else { return }
32+
33+
self.dependencyProvider = RCTAppDependencyProvider()
34+
self.reactNativeFactory = RCTReactNativeFactory(delegate: self)
35+
36+
if let onBundleLoaded = onBundleLoaded {
37+
self.onBundleLoaded = onBundleLoaded
38+
NotificationCenter.default.addObserver(self,
39+
selector: #selector(jsLoaded),
40+
name: NSNotification.Name("RCTJavaScriptDidLoadNotification"),
41+
object: nil)
42+
}
43+
}
44+
45+
@objc private func jsLoaded(_ notification: Notification) {
46+
onBundleLoaded?()
47+
onBundleLoaded = nil
48+
NotificationCenter.default.removeObserver(self)
49+
}
50+
51+
// MARK: - RCTBridgeDelegate Methods
52+
53+
@objc public override func sourceURL(for bridge: RCTBridge) -> URL? {
54+
return bundleURL()
55+
}
56+
57+
public override func bundleURL() -> URL? {
58+
#if DEBUG
59+
return RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: entryFile)
60+
#else
61+
let resourceURLComponents = bundlePath.components(separatedBy: ".")
62+
let withoutLast = resourceURLComponents[..<(resourceURLComponents.count - 1)]
63+
let resourceName = withoutLast.joined()
64+
let fileExtension = resourceURLComponents.last ?? ""
65+
66+
return Bundle.main.url(forResource: resourceName, withExtension: fileExtension)
67+
#endif
68+
}
69+
}
70+
71+
extension Notification.Name {
72+
public static let popToNative = Notification.Name("PopToNativeNotification")
73+
public static let togglePopGestureRecognizer = Notification.Name("TogglePopGestureRecognizerNotification")
74+
}

ios/ReactNativeBrownfieldModule.m

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
#import "ReactNativeBrownfieldModule.h"
2-
#import "ReactNativeBrownfieldNotifications.h"
2+
#import "ReactNativeBrownfield-Swift.h"
33

44
@implementation ReactNativeBrownfieldModule
55

66
RCT_EXPORT_MODULE(ReactNativeBrownfield);
77

88
RCT_EXPORT_METHOD(setPopGestureRecognizerEnabled:(BOOL)enabled) {
9-
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@(enabled) forKey:@"enabled"];
10-
11-
[[NSNotificationCenter defaultCenter]
12-
postNotificationName:TogglePopGestureRecognizerNotification
13-
object:nil userInfo:userInfo];
9+
[ReactNativeBrownfieldModuleImpl setPopGestureRecognizerEnabled:enabled];
1410
}
1511

1612
RCT_EXPORT_METHOD(popToNative:(BOOL)animated) {
17-
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@(animated) forKey:@"animated"];
18-
19-
[[NSNotificationCenter defaultCenter]
20-
postNotificationName:PopToNativeNotification
21-
object:nil userInfo:userInfo];
13+
[ReactNativeBrownfieldModuleImpl popToNativeWithAnimated:animated];
2214
}
2315

2416
@end

0 commit comments

Comments
 (0)