Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/create-release-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ jobs:
fi
echo "✓ Updated react-native-onesignal.podspec with iOS SDK ${VERSION}"

sed -i '' -E "s/(let oneSignalXCFrameworkVersion: Version = \")[^\"]+(\")/\1$VERSION\2/" Package.swift
if ! grep -q "let oneSignalXCFrameworkVersion: Version = \"${VERSION}\"" Package.swift; then
echo "::error::Failed to update iOS SDK version in Package.swift to ${VERSION}"
exit 1
fi
echo "✓ Updated Package.swift with iOS SDK ${VERSION}"

PODFILE=examples/demo/ios/Podfile
sed -i '' -E "s/^([[:space:]]*pod 'OneSignalXCFramework', )'[^']+'.*$/\1'${VERSION}'/" "$PODFILE"
if [ "$(grep -c "pod 'OneSignalXCFramework', '${VERSION}'$" "$PODFILE")" -ne 2 ]; then
Expand Down
60 changes: 60 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// swift-tools-version: 6.0

import Foundation
import PackageDescription

func oneSignalEnvFlag(_ name: String) -> Bool {
let value = Context.environment[name]?.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
return value == "true" || value == "1"
}

let oneSignalDisableLocation = oneSignalEnvFlag("ONESIGNAL_DISABLE_LOCATION")

// Keep in sync with onesignal_xcframework_version in react-native-onesignal.podspec.
let oneSignalXCFrameworkVersion: Version = "5.8.1"

// InAppMessages and Location are separate library products in OneSignal-XCFramework
// and must be linked explicitly under SPM.
var oneSignalDependencies: [Target.Dependency] = [
.product(name: "OneSignalFramework", package: "OneSignal-XCFramework"),
.product(name: "OneSignalInAppMessages", package: "OneSignal-XCFramework"),
]

if !oneSignalDisableLocation {
oneSignalDependencies.append(.product(name: "OneSignalLocation", package: "OneSignal-XCFramework"))
}

let package = Package(
name: "ReactNativeOneSignal",
platforms: [.iOS("15.1")],
products: [
.library(name: "ReactNativeOneSignal", targets: ["ReactNativeOneSignal"]),
],
dependencies: [
// Local packages generated per app by React Native's SwiftPM autolinker,
// relative to where it places this package.
.package(name: "ReactNative", path: "../../../../xcframeworks"),
.package(name: "React-GeneratedCode", path: "../../../ios"),
.package(url: "https://github.com/OneSignal/OneSignal-XCFramework", exact: oneSignalXCFrameworkVersion),
],
targets: [
.target(
name: "ReactNativeOneSignal",
dependencies: [
.product(name: "ReactHeaders", package: "ReactNative"),
.product(name: "ReactNativeHeaders", package: "ReactNative"),
.product(name: "ReactNativeDependenciesHeaders", package: "ReactNative"),
.product(name: "ReactAppHeaders", package: "React-GeneratedCode"),
] + oneSignalDependencies,
path: "ios/RCTOneSignal",
publicHeadersPath: ".",
cSettings: [.define("RCT_NEW_ARCH_ENABLED", to: "1")],
cxxSettings: [
.define("RCT_NEW_ARCH_ENABLED", to: "1"),
.define("DEBUG", .when(configuration: .debug)),
.define("NDEBUG", .when(configuration: .release)),
]
),
],
cxxLanguageStandard: .cxx20
)
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ If you run into any challenges or have concerns, please contact our support team

See the [Setup Guide](https://documentation.onesignal.com/docs/react-native-sdk-setup) for setup instructions.

#### Swift Package Manager (iOS)

This package ships a `Package.swift` for React Native's Swift Package Manager mode (React Native `0.87` and later). In that mode it is the Swift package `ReactNativeOneSignal`, which brings in `OneSignal-XCFramework` at the same version the podspec uses. Run `npx react-native spm add` (or `npx react-native spm update` if SPM is already set up) after installing. CocoaPods apps are unaffected.

A Notification Service Extension target is not managed by React Native's autolinker. Add the `OneSignalExtension` product from `https://github.com/OneSignal/OneSignal-XCFramework` to it in Xcode, at the same version.

#### Disable Location Module

By default, `react-native-onesignal` includes OneSignal's native location module so `OneSignal.Location` works without extra setup. If your app does not use location features, you can exclude the native location module from iOS and Android builds.
Expand Down Expand Up @@ -71,8 +77,10 @@ ONESIGNAL_DISABLE_LOCATION=true pod install

Gradle re-reads the environment variable or `onesignal.disableLocation` property on each configuration, so a clean build after changing the flag is enough on Android.

With Swift Package Manager, `Package.swift` reads the variable when packages are resolved. Export it in the shell that runs `npx react-native spm update` and `xcodebuild` (or launches Xcode), then run File > Packages > Reset Package Caches in Xcode after changing it.

> [!IMPORTANT]
> When using Xcode or Android Studio, launch the IDE from a terminal that has `ONESIGNAL_DISABLE_LOCATION` exported. An IDE launched from the Dock/Finder does not inherit variables set only in your shell profile. On CI, key any CocoaPods / Gradle caches on the value of `ONESIGNAL_DISABLE_LOCATION` so a restored cache does not resurrect the location module.
> When using Xcode or Android Studio, launch the IDE from a terminal that has `ONESIGNAL_DISABLE_LOCATION` exported. An IDE launched from the Dock/Finder does not inherit variables set only in your shell profile. On CI, key any CocoaPods, SwiftPM, Xcode DerivedData, and Gradle caches on the value of `ONESIGNAL_DISABLE_LOCATION` so a restored cache does not resurrect the location module.

#### Change Log

Expand Down
30 changes: 30 additions & 0 deletions __tests__/packageSwift.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// <reference types="node" />

import { readFileSync } from 'node:fs';
import path from 'node:path';

import { expect, test } from 'vite-plus/test';

// CI does not build Package.swift, so keep its native dependency pinned to the podspec's.
const root = path.resolve(import.meta.dirname, '..');
const read = (file: string) => readFileSync(path.join(root, file), 'utf8');

test('Package.swift pins the same OneSignal-XCFramework version as the podspec', () => {
const podspecVersion = read('react-native-onesignal.podspec').match(
/onesignal_xcframework_version = '([^']+)'/,
)?.[1];
const packageSwiftVersion = read('Package.swift').match(
/let oneSignalXCFrameworkVersion: Version = "([^"]+)"/,
)?.[1];

expect(podspecVersion).toBeDefined();
expect(packageSwiftVersion).toBe(podspecVersion);
});

test('swiftpmConfig name matches the Package.swift product', () => {
const { swiftpmConfig } = JSON.parse(read('package.json')) as {
swiftpmConfig: { name: string };
};

expect(read('Package.swift')).toContain(`.library(name: "${swiftpmConfig.name}"`);
});
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"android/build.gradle",
"android/proguard-rules.pro",
"ios",
"react-native-onesignal.podspec"
"react-native-onesignal.podspec",
"Package.swift"
],
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -73,5 +74,8 @@
"OneSignal": "RCTOneSignalEventEmitter"
}
}
},
"swiftpmConfig": {
"name": "ReactNativeOneSignal"
}
}
Loading