Skip to content

Commit d7a1f24

Browse files
committed
ZLPConstants: Convert to Swift
Soon, we'd like to expose a URL to pass to Linking.openURL to bring the user to the system-level notification settings for the Zulip app. I think I was caught by a red herring when getting such a URL on a simulator running iOS 16. I was using the URL given by the global variable UIApplicationOpenNotificationSettingsURLString, new in iOS 15.4 -- https://developer.apple.com/documentation/uikit/uiapplicationopennotificationsettingsurlstring?language=objc -- and openURL was bringing me to the home page of the Settings app instead of the Zulip-specific notification settings. At first, I thought it was failing because I wasn't using the Swift-only UIApplication.openNotificationSettingsURLString -- https://developer.apple.com/documentation/uikit/uiapplication/4013180-opennotificationsettingsurlstrin -- new in iOS 16.0 to replace that UIApplicationOpenNotificationSettingsURLString, which in the Swift doc is marked as deprecated: https://developer.apple.com/documentation/uikit/uiapplicationopennotificationsettingsurlstring But then I did this Swift conversion and tried out the iOS 16+ thing, and it gave the same URL with the same bad result. 🤷 Now my theory is that something is busted with how Linking.openURL works on the simulator. I tried with my iPhone and it opened to the notification settings, as expected. Anyway, might as well keep the work I did to convert this small file, since Swift is the future. After reading docs and forum discussions, including RN's official doc https://reactnative.dev/docs/0.68/native-modules-ios#exporting-swift , here's what I did: - Translate the Objective-C implementation, in ZLPConstants.m, to Swift, and rename the file from .m to .swift. Make sure it still has a PBXFileReference that appears in the ZulipMobile PBXGroup and in the "Compile Sources" build phase of the ZulipMobile target. - Remove the .h file, its PBXFileReference, and the one place its PBXFileReference appeared: as a member of the ZulipMobile PBXGroup. - Add a ZLPConstantsBridge.m, to register the Swift implementation with React Native. Make sure this new file has a PBXFileReference that appears in the ZulipMobile PBXGroup and in "Compile Sources". - We already have a "bridging header", ios/ZulipMobile-Bridging-Header.h. Possibly we only need its first line, but I'm not sure, and it seems to work as-is.
1 parent 77b7e9c commit d7a1f24

File tree

6 files changed

+39
-36
lines changed

6 files changed

+39
-36
lines changed

ios/ZulipMobile.xcodeproj/project.pbxproj

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
1313
42689E9C23466FF7007540AA /* webview in Resources */ = {isa = PBXBuildFile; fileRef = 42689E9B23466FF7007540AA /* webview */; };
1414
8B40B9F327F7AEEC00D33897 /* zulip-icons.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 3787526927F5503200B81DB8 /* zulip-icons.ttf */; };
15+
8B44AB3E295B7601003D41A8 /* ZLPConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B44AB3A295B7263003D41A8 /* ZLPConstants.swift */; };
16+
8B44AB40295B7D4D003D41A8 /* ZLPConstantsBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B44AB3F295B7D45003D41A8 /* ZLPConstantsBridge.m */; };
1517
8BE55043253A2B6600B0BC8A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 8BE55041253A2B6600B0BC8A /* LaunchScreen.storyboard */; };
16-
8BEA1EF024A6983D00674040 /* ZLPConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 8BEA1EEE24A6983D00674040 /* ZLPConstants.m */; };
1718
A148FEFC1E9D8CB900479280 /* zulip.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = A148FEFB1E9D8CB900479280 /* zulip.mp3 */; };
1819
A65C1ECEE03CA6692FC807AA /* ExpoModulesProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = F2C68E38F2AA306A2C56A4B3 /* ExpoModulesProvider.swift */; };
1920
C091D09FD11FDDB8E939257D /* libPods-ZulipMobile.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 74F3CD22CB932FA7EEE0BB66 /* libPods-ZulipMobile.a */; };
@@ -34,9 +35,9 @@
3435
42689E9B23466FF7007540AA /* webview */ = {isa = PBXFileReference; lastKnownFileType = folder; path = webview; sourceTree = "<group>"; };
3536
66E34CC6219226D10091B852 /* ZulipMobile-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ZulipMobile-Bridging-Header.h"; sourceTree = "<group>"; };
3637
74F3CD22CB932FA7EEE0BB66 /* libPods-ZulipMobile.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-ZulipMobile.a"; sourceTree = BUILT_PRODUCTS_DIR; };
38+
8B44AB3A295B7263003D41A8 /* ZLPConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ZLPConstants.swift; path = ZulipMobile/ZLPConstants.swift; sourceTree = "<group>"; };
39+
8B44AB3F295B7D45003D41A8 /* ZLPConstantsBridge.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = ZLPConstantsBridge.m; path = ZulipMobile/ZLPConstantsBridge.m; sourceTree = "<group>"; };
3740
8BE55042253A2B6600B0BC8A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = ZulipMobile/Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
38-
8BEA1EEE24A6983D00674040 /* ZLPConstants.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ZLPConstants.m; path = ZulipMobile/ZLPConstants.m; sourceTree = "<group>"; };
39-
8BEA1EEF24A6983D00674040 /* ZLPConstants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ZLPConstants.h; path = ZulipMobile/ZLPConstants.h; sourceTree = "<group>"; };
4041
A148FEFB1E9D8CB900479280 /* zulip.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = zulip.mp3; sourceTree = "<group>"; };
4142
CFA67D1F1EC23BCB0070048E /* UtilManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = UtilManager.m; path = ZulipMobile/UtilManager.m; sourceTree = "<group>"; };
4243
CFA67D211EC23BDD0070048E /* UtilManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = UtilManager.h; path = ZulipMobile/UtilManager.h; sourceTree = "<group>"; };
@@ -61,8 +62,8 @@
6162
13B07FAE1A68108700A75B9A /* ZulipMobile */ = {
6263
isa = PBXGroup;
6364
children = (
64-
8BEA1EEF24A6983D00674040 /* ZLPConstants.h */,
65-
8BEA1EEE24A6983D00674040 /* ZLPConstants.m */,
65+
8B44AB3F295B7D45003D41A8 /* ZLPConstantsBridge.m */,
66+
8B44AB3A295B7263003D41A8 /* ZLPConstants.swift */,
6667
3C4249EC1EF6E16500D245F1 /* ZulipMobile.entitlements */,
6768
13B07FAF1A68108700A75B9A /* AppDelegate.h */,
6869
13B07FB01A68108700A75B9A /* AppDelegate.mm */,
@@ -367,7 +368,8 @@
367368
buildActionMask = 2147483647;
368369
files = (
369370
CFA67D201EC23BCB0070048E /* UtilManager.m in Sources */,
370-
8BEA1EF024A6983D00674040 /* ZLPConstants.m in Sources */,
371+
8B44AB3E295B7601003D41A8 /* ZLPConstants.swift in Sources */,
372+
8B44AB40295B7D4D003D41A8 /* ZLPConstantsBridge.m in Sources */,
371373
13B07FBC1A68108700A75B9A /* AppDelegate.mm in Sources */,
372374
13B07FC11A68108700A75B9A /* main.m in Sources */,
373375
A65C1ECEE03CA6692FC807AA /* ExpoModulesProvider.swift in Sources */,

ios/ZulipMobile/ZLPConstants.h

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

ios/ZulipMobile/ZLPConstants.m

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

ios/ZulipMobile/ZLPConstants.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Foundation
2+
3+
@objc(ZLPConstants)
4+
class ZLPConstants: NSObject {
5+
6+
7+
// For why we include this, see
8+
// https://reactnative.dev/docs/0.68/native-modules-ios#exporting-constants
9+
@objc
10+
static func requiresMainQueueSetup() -> Bool {
11+
// Initialization may be done on any thread; we don't need access to
12+
// UIKit.
13+
return false
14+
}
15+
16+
@objc
17+
func constantsToExport() -> [String: Any]! {
18+
return ["resourceURL": Bundle.main.resourceURL!.absoluteString]
19+
}
20+
}

ios/ZulipMobile/ZLPConstantsBridge.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#import "React/RCTBridgeModule.h"
2+
3+
// Register the ZLPConstants implementation with React Native, needed
4+
// because ZLPConstants is in Swift:
5+
// https://reactnative.dev/docs/0.68/native-modules-ios#exporting-swift
6+
@interface RCT_EXTERN_MODULE(ZLPConstants, NSObject)
7+
@end

src/webview/MessageList.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ function useMessageListProps(props: OuterProps): Props {
180180
// URL instead, at least in development. We'd then just need to adjust
181181
// the `originWhitelist` we pass to `WebView`.
182182
//
183-
// - On iOS: We can't easily hardcode this because it includes UUIDs.
184-
// So we bring it over the React Native bridge in ZLPConstants.m.
185-
// It's a file URL because the app bundle's `resourceURL` is:
186-
// https://developer.apple.com/documentation/foundation/bundle/1414821-resourceurl
183+
// - On iOS: We can't easily hardcode this because it includes UUIDs. So we
184+
// bring it over the React Native bridge from our native module
185+
// ZLPConstants. It's a file URL because the app bundle's `resourceURL` is:
186+
// https://developer.apple.com/documentation/foundation/bundle/1414821-resourceurl
187187
//
188188
// - On Android: Different apps' WebViews see different (virtual) root
189189
// directories as `file:///`, and in particular the WebView provides

0 commit comments

Comments
 (0)