@rediads/react-native-ads 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,40 @@
1
+ import { NativeModule } from 'expo';
2
+ import type { RediadsAdEventType, RediadsAdFormat } from './types';
3
+ /** An event of an interstitial or rewarded ad, as the native module sends it. */
4
+ export interface NativeFullScreenEvent {
5
+ adId: number;
6
+ type: RediadsAdEventType;
7
+ placement: string;
8
+ error?: string | null;
9
+ rewardType?: string | null;
10
+ rewardAmount?: number | null;
11
+ }
12
+ type NativeEvents = {
13
+ onFullScreenAdEvent: (event: NativeFullScreenEvent) => void;
14
+ };
15
+ export interface NativeStartOptions {
16
+ accountId: string;
17
+ serverUrl: string;
18
+ testMode: boolean;
19
+ debugLogging: boolean;
20
+ childDirected: boolean;
21
+ appStoreId: string | null;
22
+ }
23
+ /** ios/RediadsAdsModule.swift and android/.../RediadsAdsModule.kt. */
24
+ declare class RediadsAdsNativeModule extends NativeModule<NativeEvents> {
25
+ /** The native Rediads SDK's version. */
26
+ readonly sdkVersion: string;
27
+ /** Resolves with null once started, or with the reason the server check failed. */
28
+ start(options: NativeStartOptions): Promise<string | null>;
29
+ setChildDirected(value: boolean): Promise<void>;
30
+ /** Creates a new SDK ad under `adId` (replacing an earlier one) and loads it. */
31
+ loadInterstitial(adId: number, placement: string, format: RediadsAdFormat): Promise<string | null>;
32
+ loadRewarded(adId: number, placement: string, rewardType: string, rewardAmount: number, format: RediadsAdFormat, displaySeconds: number): Promise<string | null>;
33
+ isReady(adId: number): Promise<boolean>;
34
+ show(adId: number): Promise<void>;
35
+ destroy(adId: number): Promise<void>;
36
+ }
37
+ /** Null in Expo Go, on web, and in builds made before this package was added. */
38
+ export declare const native: RediadsAdsNativeModule | null;
39
+ export declare const NOT_AVAILABLE = "The Rediads native module is not in this build (Expo Go, web, or an old build).";
40
+ export {};
@@ -0,0 +1,4 @@
1
+ import { requireOptionalNativeModule } from 'expo';
2
+ /** Null in Expo Go, on web, and in builds made before this package was added. */
3
+ export const native = requireOptionalNativeModule('RediadsAds');
4
+ export const NOT_AVAILABLE = 'The Rediads native module is not in this build (Expo Go, web, or an old build).';
@@ -0,0 +1,48 @@
1
+ /** What happened to an ad. */
2
+ export type RediadsAdEventType =
3
+ /** An ad was received and rendered (banner), or is ready to show (interstitial, rewarded). */
4
+ 'loaded'
5
+ /** The banner ad is on screen. */
6
+ | 'displayed'
7
+ /** Loading or showing failed; see `error`. */
8
+ | 'failed'
9
+ /** The user tapped the ad. */
10
+ | 'clicked'
11
+ /** A full-screen view opened: the ad itself, or the landing page after a click. */
12
+ | 'presented'
13
+ /** That full-screen view closed. */
14
+ | 'dismissed'
15
+ /** A click is taking the user out of the app (browser, app store). */
16
+ | 'leftApplication'
17
+ /** The user earned the reward of a rewarded ad; see `reward`. */
18
+ | 'rewarded';
19
+ /** What the user gets for watching a rewarded ad, for example 10 "coins". The app defines it. */
20
+ export interface RediadsReward {
21
+ type: string;
22
+ amount: number;
23
+ }
24
+ /** Something that happened to one ad. */
25
+ export interface RediadsAdEvent {
26
+ type: RediadsAdEventType;
27
+ /** The placement name the ad was created with. */
28
+ placement: string;
29
+ /** Why loading or showing failed; only on `failed`. */
30
+ error?: string;
31
+ /** The reward the user earned; only on `rewarded`. */
32
+ reward?: RediadsReward;
33
+ }
34
+ /** Which creatives an ad slot accepts. */
35
+ export type RediadsAdFormat =
36
+ /** HTML and image ads only (the default). */
37
+ 'display'
38
+ /** Video (VAST) only. Banners: 300x250. */
39
+ | 'video'
40
+ /** Either, whichever wins the auction. */
41
+ | 'displayAndVideo';
42
+ /** Banner sizes, in points (iOS) and dp (Android). */
43
+ export type RediadsBannerSize = '320x50' | '300x250';
44
+ export type RediadsAdListener = (event: RediadsAdEvent) => void;
45
+ /** Returned by `addListener`; call `remove()` to stop listening. */
46
+ export interface RediadsSubscription {
47
+ remove(): void;
48
+ }
package/build/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ {
2
+ "platforms": ["apple", "android"],
3
+ "apple": {
4
+ "modules": ["RediadsAdsModule"]
5
+ },
6
+ "android": {
7
+ "modules": ["com.rediads.reactnative.RediadsAdsModule"]
8
+ }
9
+ }
@@ -0,0 +1,31 @@
1
+ require 'json'
2
+
3
+ package = JSON.parse(File.read(File.join(__dir__, '..', 'package.json')))
4
+
5
+ Pod::Spec.new do |s|
6
+ s.name = 'RediadsAds'
7
+ s.version = package['version']
8
+ s.summary = package['description']
9
+ s.description = package['description']
10
+ s.license = package['license']
11
+ s.author = 'Rediads'
12
+ s.homepage = package['homepage']
13
+ s.platforms = { :ios => '16.4' }
14
+ s.swift_version = '5.9'
15
+ s.source = { git: '' }
16
+ s.static_framework = true
17
+
18
+ s.dependency 'ExpoModulesCore'
19
+ # The Rediads SDK, a binary pod from Rediads' pod list (github.com/rediads-hq/rediads-ios). The
20
+ # config plugin (app.plugin.js) adds that list as a Podfile source; see README.md for bare React
21
+ # Native. With the plugin's iosSdkPath option, the same versions come from a local SDK checkout.
22
+ s.dependency 'RediadsDisplay', '0.1.4'
23
+ # Viewability measurement (Open Measurement), found by the SDK at run time.
24
+ s.dependency 'RediadsOM', '0.1.4'
25
+
26
+ s.pod_target_xcconfig = {
27
+ 'DEFINES_MODULE' => 'YES',
28
+ }
29
+
30
+ s.source_files = '**/*.{h,m,mm,swift}'
31
+ end
@@ -0,0 +1,211 @@
1
+ import ExpoModulesCore
2
+ import RediadsDisplay
3
+
4
+ /// Options of `start`, from `RediadsAds.start` in src/RediadsAds.ts.
5
+ struct StartOptions: Record {
6
+ @Field var accountId: String = ""
7
+ @Field var serverUrl: String = RediadsAds.defaultServerURL
8
+ @Field var testMode: Bool = false
9
+ @Field var debugLogging: Bool = false
10
+ @Field var childDirected: Bool = false
11
+ @Field var appStoreId: String?
12
+ }
13
+
14
+ /// Starts the Rediads SDK, hosts its banners as React Native views and runs interstitial and rewarded
15
+ /// ads by the ID JavaScript gives them (src/RediadsFullScreenAd.ts). Everything that touches the SDK
16
+ /// runs on the main queue, where the SDK also calls back.
17
+ public class RediadsAdsModule: Module {
18
+ /// Interstitials and rewarded ads by ID. Main queue only.
19
+ private var ads: [Int: NSObject] = [:]
20
+
21
+ public func definition() -> ModuleDefinition {
22
+ Name("RediadsAds")
23
+
24
+ Constant("sdkVersion") {
25
+ RediadsAds.sdkVersion
26
+ }
27
+
28
+ Events("onFullScreenAdEvent")
29
+
30
+ AsyncFunction("start") { (options: StartOptions, promise: Promise) in
31
+ RediadsAds.testMode = options.testMode
32
+ RediadsAds.debugLogging = options.debugLogging
33
+ RediadsAds.childDirected = options.childDirected
34
+ if let appStoreId = options.appStoreId {
35
+ RediadsAds.appStoreID = appStoreId
36
+ }
37
+ // The SDK sends the IDFA only if the user has allowed tracking (App Tracking Transparency).
38
+ // Whether to ask is the app's choice, made outside this package.
39
+ RediadsAds.start(accountId: options.accountId, serverURL: options.serverUrl) { error in
40
+ promise.resolve(error?.localizedDescription)
41
+ }
42
+ }.runOnQueue(.main)
43
+
44
+ AsyncFunction("setChildDirected") { (value: Bool) in
45
+ RediadsAds.childDirected = value
46
+ }.runOnQueue(.main)
47
+
48
+ AsyncFunction("loadInterstitial") { (adId: Int, placement: String, format: String, promise: Promise) in
49
+ self.discard(adId)
50
+ let ad = RediadsInterstitial(placement: placement, format: Self.format(format))
51
+ ad.onEvent = self.forward(adId)
52
+ self.ads[adId] = ad
53
+ ad.load { error in
54
+ promise.resolve(error?.localizedDescription)
55
+ }
56
+ }.runOnQueue(.main)
57
+
58
+ AsyncFunction("loadRewarded") {
59
+ (adId: Int, placement: String, rewardType: String, rewardAmount: Int, format: String,
60
+ displaySeconds: Int, promise: Promise) in
61
+ self.discard(adId)
62
+ let ad = RediadsRewarded(
63
+ placement: placement,
64
+ reward: RediadsReward(type: rewardType, amount: rewardAmount),
65
+ format: Self.format(format),
66
+ displaySeconds: displaySeconds > 0 ? displaySeconds : RediadsRewarded.defaultDisplaySeconds
67
+ )
68
+ ad.onEvent = self.forward(adId)
69
+ self.ads[adId] = ad
70
+ ad.load { error in
71
+ promise.resolve(error?.localizedDescription)
72
+ }
73
+ }.runOnQueue(.main)
74
+
75
+ AsyncFunction("isReady") { (adId: Int) -> Bool in
76
+ switch self.ads[adId] {
77
+ case let ad as RediadsInterstitial: return ad.isReady
78
+ case let ad as RediadsRewarded: return ad.isReady
79
+ default: return false
80
+ }
81
+ }.runOnQueue(.main)
82
+
83
+ AsyncFunction("show") { (adId: Int) in
84
+ guard let ad = self.ads[adId] else { return }
85
+ // The top view controller, above any modal React Native has presented.
86
+ guard let host = self.appContext?.utilities?.currentViewController() else {
87
+ let placement = (ad as? RediadsInterstitial)?.placement ?? (ad as? RediadsRewarded)?.placement ?? ""
88
+ self.sendEvent("onFullScreenAdEvent", [
89
+ "adId": adId, "type": "failed", "placement": placement,
90
+ "error": "There is no view controller to show the ad from.",
91
+ ])
92
+ return
93
+ }
94
+ if let ad = ad as? RediadsInterstitial {
95
+ ad.show(from: host)
96
+ } else if let ad = ad as? RediadsRewarded {
97
+ ad.show(from: host)
98
+ }
99
+ }.runOnQueue(.main)
100
+
101
+ AsyncFunction("destroy") { (adId: Int) in
102
+ self.discard(adId)
103
+ }.runOnQueue(.main)
104
+
105
+ View(RediadsBannerSlot.self) {
106
+ Events("onAdEvent")
107
+
108
+ Prop("placement") { (view: RediadsBannerSlot, placement: String) in
109
+ view.placement = placement
110
+ }
111
+
112
+ Prop("size") { (view: RediadsBannerSlot, size: String) in
113
+ view.size = size
114
+ }
115
+
116
+ Prop("format") { (view: RediadsBannerSlot, format: String) in
117
+ view.format = format
118
+ }
119
+
120
+ OnViewDidUpdateProps { (view: RediadsBannerSlot) in
121
+ view.loadIfReady()
122
+ }
123
+ }
124
+ }
125
+
126
+ /// Lets go of the ad under this ID, as Android's `discard` does: a load under an ID in use (JavaScript
127
+ /// reuses an ad's ID when it loads again) replaces the old ad, and none of the old ad's events, such as
128
+ /// a late `loaded` or the `dismissed` of one still on screen, reach JavaScript as the new ad's.
129
+ private func discard(_ adId: Int) {
130
+ switch self.ads.removeValue(forKey: adId) {
131
+ case let ad as RediadsInterstitial: ad.onEvent = nil
132
+ case let ad as RediadsRewarded: ad.onEvent = nil
133
+ default: break
134
+ }
135
+ }
136
+
137
+ /// Sends an ad's events to JavaScript under its ID.
138
+ private func forward(_ adId: Int) -> (RediadsAdEvent) -> Void {
139
+ return { [weak self] event in
140
+ var payload: [String: Any?] = [
141
+ "adId": adId,
142
+ "type": Self.name(of: event.type),
143
+ "placement": event.placement,
144
+ ]
145
+ if let error = event.error { payload["error"] = error.localizedDescription }
146
+ if let reward = event.reward {
147
+ payload["rewardType"] = reward.type
148
+ payload["rewardAmount"] = reward.amount
149
+ }
150
+ self?.sendEvent("onFullScreenAdEvent", payload)
151
+ }
152
+ }
153
+
154
+ /// The JavaScript format names: display, video, displayAndVideo.
155
+ static func format(_ name: String) -> RediadsAdFormat {
156
+ switch name {
157
+ case "video": return .video
158
+ case "displayAndVideo": return .displayAndVideo
159
+ default: return .display
160
+ }
161
+ }
162
+
163
+ static func name(of type: RediadsAdEventType) -> String {
164
+ switch type {
165
+ case .loaded: return "loaded"
166
+ case .displayed: return "displayed"
167
+ case .failed: return "failed"
168
+ case .clicked: return "clicked"
169
+ case .presented: return "presented"
170
+ case .dismissed: return "dismissed"
171
+ case .leftApplication: return "leftApplication"
172
+ case .rewarded: return "rewarded"
173
+ @unknown default: return "unknown"
174
+ }
175
+ }
176
+ }
177
+
178
+ /// One banner slot. The banner is made once React has set the props and is kept for the life of the
179
+ /// view (JavaScript gives the view a new key when they change); the SDK refreshes it while on screen.
180
+ final class RediadsBannerSlot: ExpoView {
181
+ let onAdEvent = EventDispatcher()
182
+ var placement = ""
183
+ var size = "320x50"
184
+ var format = "display"
185
+ private var banner: RediadsBannerView?
186
+
187
+ func loadIfReady() {
188
+ guard banner == nil, !placement.isEmpty else { return }
189
+ let adSize =
190
+ size == "300x250" ? RediadsBannerView.mediumRectangle300x250 : RediadsBannerView.banner320x50
191
+ let banner = RediadsBannerView(
192
+ placement: placement, size: adSize, format: RediadsAdsModule.format(format))
193
+ banner.onEvent = { [weak self] event in
194
+ var payload: [String: Any] = [
195
+ "type": RediadsAdsModule.name(of: event.type),
196
+ "placement": event.placement,
197
+ ]
198
+ if let error = event.error { payload["error"] = error.localizedDescription }
199
+ self?.onAdEvent(payload)
200
+ }
201
+ banner.frame = CGRect(origin: .zero, size: adSize)
202
+ addSubview(banner)
203
+ self.banner = banner
204
+ banner.load()
205
+ }
206
+
207
+ override func layoutSubviews() {
208
+ super.layoutSubviews()
209
+ banner?.center = CGPoint(x: bounds.midX, y: bounds.midY)
210
+ }
211
+ }
package/package.json CHANGED
@@ -1,6 +1,53 @@
1
1
  {
2
2
  "name": "@rediads/react-native-ads",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Banner, interstitial and rewarded ads from the Rediads SDK for React Native apps on iOS and Android.",
5
+ "main": "build/index.js",
6
+ "types": "build/index.d.ts",
7
+ "files": [
8
+ "build/**/*.js",
9
+ "build/**/*.d.ts",
10
+ "ios/*.podspec",
11
+ "ios/*.swift",
12
+ "android/build.gradle",
13
+ "android/src/main/",
14
+ "plugin/",
15
+ "app.plugin.js",
16
+ "expo-module.config.json",
17
+ "README.md",
18
+ "LICENSE",
19
+ "CHANGELOG.md"
20
+ ],
21
+ "scripts": {
22
+ "build": "node internal/module_scripts/build.js",
23
+ "clean": "node internal/module_scripts/clean.js",
24
+ "lint": "eslint src/ plugin/ app.plugin.js",
25
+ "prepare": "node internal/module_scripts/prepare.js"
26
+ },
27
+ "keywords": [
28
+ "react-native",
29
+ "expo",
30
+ "ads",
31
+ "advertising",
32
+ "rediads",
33
+ "prebid"
34
+ ],
35
+ "author": "Rediads (https://rediads.com)",
36
+ "license": "Apache-2.0",
37
+ "homepage": "https://rediads.com",
38
+ "devDependencies": {
39
+ "@types/react": "~19.3.0",
40
+ "eslint": "~9.39.4",
41
+ "eslint-config-universe": "^16.1.2",
42
+ "expo": "^58.0.21",
43
+ "prettier": "^3.0.0",
44
+ "react": "19.3.0",
45
+ "react-native": "0.88.1",
46
+ "typescript": "~6.0.3"
47
+ },
48
+ "peerDependencies": {
49
+ "expo": ">=58.0.0",
50
+ "react": "*",
51
+ "react-native": "*"
52
+ }
53
+ }