@bacons/apple-targets 0.1.6 → 0.1.7
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.
- package/README.md +79 -0
- package/app.plugin.js +1 -1
- package/build/ExtensionStorage.d.ts +6 -0
- package/build/ExtensionStorage.js +48 -0
- package/expo-module.config.json +6 -0
- package/ios/ExtensionStorage.podspec +21 -0
- package/ios/ExtensionStorageModule.swift +53 -0
- package/package.json +5 -2
- /package/build/{index.d.ts → config-plugin.d.ts} +0 -0
- /package/build/{index.js → config-plugin.js} +0 -0
package/README.md
CHANGED
|
@@ -312,3 +312,82 @@ Some workarounds:
|
|
|
312
312
|
|
|
313
313
|
- Prebuild without React Native: `npx expo prebuild --template node_modules/@bacons/apple-targets/prebuild-blank.tgz --clean`
|
|
314
314
|
- If the widget doesn't show on the home screen when building the app, use iOS 18. You can long press the app icon and select the widget display options to transform the app icon into the widget.
|
|
315
|
+
|
|
316
|
+
## Sharing data between targets
|
|
317
|
+
|
|
318
|
+
To share values between the app and the target, you must use App Groups and NSUserDefaults. I've added a native module to make the React Native API a bit easier.
|
|
319
|
+
|
|
320
|
+
### Configuring App Groups
|
|
321
|
+
|
|
322
|
+
Start by defining an App Group, a good default is `group.<bundle identifier>`. App Groups can be used across apps so you may want something more generic or less generic if you plan on having multiple extensions.
|
|
323
|
+
|
|
324
|
+
First, define your main App Group entitlement in your `app.json`:
|
|
325
|
+
|
|
326
|
+
```json
|
|
327
|
+
{
|
|
328
|
+
"expo": {
|
|
329
|
+
"ios": {
|
|
330
|
+
"entitlements": {
|
|
331
|
+
"com.apple.security.application-groups": ["group.bacon.data"]
|
|
332
|
+
}
|
|
333
|
+
},
|
|
334
|
+
"plugins": ["@bacons/apple-targets"]
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
Second, define the same App Group in your target's `expo-target.config.js`:
|
|
340
|
+
|
|
341
|
+
```js
|
|
342
|
+
/** @type {import('@bacons/apple-targets').ConfigFunction} */
|
|
343
|
+
module.exports = (config) => ({
|
|
344
|
+
type: "widget",
|
|
345
|
+
entitlements: {
|
|
346
|
+
// Use the same app groups:
|
|
347
|
+
"com.apple.security.application-groups":
|
|
348
|
+
config.ios.entitlements["com.apple.security.application-groups"],
|
|
349
|
+
},
|
|
350
|
+
});
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
Now you can prebuild to generate the entitlements. You may need to create an EAS Build or open Xcode to sync the entitlements.
|
|
354
|
+
|
|
355
|
+
### Setting shared data
|
|
356
|
+
|
|
357
|
+
To define shared data, we'll use a native module (`ExtensionStorage`) that interacts with `NSUserDefaults`.
|
|
358
|
+
|
|
359
|
+
Somewhere in your Expo app, you can set a value:
|
|
360
|
+
|
|
361
|
+
```js
|
|
362
|
+
import { ExtensionStorage } from "@bacons/apple-targets";
|
|
363
|
+
|
|
364
|
+
// Create a storage object with the App Group.
|
|
365
|
+
const storage = new ExtensionStorage(
|
|
366
|
+
// Your app group identifier. Should match the values in the app.json and expo-target.config.json.
|
|
367
|
+
"group.bacon.data"
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
// Then you can set data:
|
|
371
|
+
storage.set("myKey", "myValue");
|
|
372
|
+
|
|
373
|
+
// Finally, you can reload the widget:
|
|
374
|
+
ExtensionStorage.reloadWidget();
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
`ExtensionStorage` has the following API:
|
|
378
|
+
|
|
379
|
+
- `set(key: string, value: string | number | Record<string, string | number> | Array<Record<string, string | number>> | undefined): void` - Sets a value in the shared storage for a given key. Setting `undefined` will remove the key.
|
|
380
|
+
- `ExtensionStorage.reloadWidget(name?: string): void` - A static method for reloading the widget. Behind the scenes, this calls `WidgetCenter.shared.reloadAllTimelines()`. If given a name, it will reload a specific widget using `WidgetCenter.shared.reloadTimelines(ofKind: timeline)`.
|
|
381
|
+
|
|
382
|
+
### Accessing shared data
|
|
383
|
+
|
|
384
|
+
Assuming this is done using Swift code, you'll access data using `NSUserDefaults` directly. Here's an example of how you might access the data in a widget:
|
|
385
|
+
|
|
386
|
+
```swift
|
|
387
|
+
let defaults = UserDefaults(suiteName:
|
|
388
|
+
// Use the App Group from earlier.
|
|
389
|
+
"group.bacon.data"
|
|
390
|
+
)
|
|
391
|
+
// Access the value you set:
|
|
392
|
+
let index = defaults?.string(forKey: "myKey")
|
|
393
|
+
```
|
package/app.plugin.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports = require(
|
|
1
|
+
module.exports = require("./build/config-plugin");
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare class ExtensionStorage {
|
|
2
|
+
private readonly appGroup;
|
|
3
|
+
static reloadWidget(name?: string): void;
|
|
4
|
+
constructor(appGroup: string);
|
|
5
|
+
set(key: string, value?: string | number | Record<string, string | number> | Array<Record<string, string | number>>): void;
|
|
6
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.ExtensionStorage = void 0;
|
|
5
|
+
// @ts-expect-error
|
|
6
|
+
const ExtensionStorageModule = (_a = expo === null || expo === void 0 ? void 0 : expo.modules) === null || _a === void 0 ? void 0 : _a.ExtensionStorage;
|
|
7
|
+
const nativeModule = ExtensionStorageModule !== null && ExtensionStorageModule !== void 0 ? ExtensionStorageModule : {
|
|
8
|
+
setInt() { },
|
|
9
|
+
setString() { },
|
|
10
|
+
reloadWidget() { },
|
|
11
|
+
setObject() { },
|
|
12
|
+
remove() { },
|
|
13
|
+
setArray() { },
|
|
14
|
+
};
|
|
15
|
+
const originalSetObject = nativeModule.setObject;
|
|
16
|
+
// Sweet API doesn't support doing this natively.
|
|
17
|
+
nativeModule.setObject = (key, value, suite) => {
|
|
18
|
+
if (Array.isArray(value)) {
|
|
19
|
+
return nativeModule.setArray(key, value, suite);
|
|
20
|
+
}
|
|
21
|
+
return originalSetObject(key, value, suite);
|
|
22
|
+
};
|
|
23
|
+
class ExtensionStorage {
|
|
24
|
+
static reloadWidget(name) {
|
|
25
|
+
nativeModule.reloadWidget(name);
|
|
26
|
+
}
|
|
27
|
+
constructor(appGroup) {
|
|
28
|
+
this.appGroup = appGroup;
|
|
29
|
+
}
|
|
30
|
+
set(key, value) {
|
|
31
|
+
if (typeof value === "number") {
|
|
32
|
+
nativeModule.setInt(key, value, this.appGroup);
|
|
33
|
+
}
|
|
34
|
+
else if (Array.isArray(value)) {
|
|
35
|
+
nativeModule.setArray(key, value, this.appGroup);
|
|
36
|
+
}
|
|
37
|
+
else if (typeof value === "string") {
|
|
38
|
+
nativeModule.setString(key, value, this.appGroup);
|
|
39
|
+
}
|
|
40
|
+
else if (value == null) {
|
|
41
|
+
nativeModule.remove(key, this.appGroup);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
nativeModule.setObject(key, value, this.appGroup);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.ExtensionStorage = ExtensionStorage;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Pod::Spec.new do |s|
|
|
2
|
+
s.name = 'ExtensionStorage'
|
|
3
|
+
s.version = '1.0.0'
|
|
4
|
+
s.summary = 'A sample project summary'
|
|
5
|
+
s.description = 'A sample project description'
|
|
6
|
+
s.author = ''
|
|
7
|
+
s.homepage = 'https://docs.expo.dev/modules/'
|
|
8
|
+
s.platform = :ios, '15.1'
|
|
9
|
+
s.source = { git: '' }
|
|
10
|
+
s.static_framework = true
|
|
11
|
+
|
|
12
|
+
s.dependency 'ExpoModulesCore'
|
|
13
|
+
|
|
14
|
+
# Swift/Objective-C compatibility
|
|
15
|
+
s.pod_target_xcconfig = {
|
|
16
|
+
'DEFINES_MODULE' => 'YES',
|
|
17
|
+
'SWIFT_COMPILATION_MODE' => 'wholemodule'
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
s.source_files = "**/*.{h,m,mm,swift,hpp,cpp}"
|
|
21
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import ExpoModulesCore
|
|
2
|
+
import WidgetKit
|
|
3
|
+
|
|
4
|
+
public class ExtensionStorageModule: Module {
|
|
5
|
+
public func definition() -> ModuleDefinition {
|
|
6
|
+
Name("ExtensionStorage")
|
|
7
|
+
|
|
8
|
+
Function("remove") { (forKey: String, suiteName: String?) in
|
|
9
|
+
UserDefaults(suiteName: suiteName)?.removeObject(forKey: forKey)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
Function("reloadWidget") { (timeline: String?) in
|
|
13
|
+
if let timeline = timeline {
|
|
14
|
+
WidgetCenter.shared.reloadTimelines(ofKind: timeline)
|
|
15
|
+
} else {
|
|
16
|
+
WidgetCenter.shared.reloadAllTimelines()
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
Function("setArray") { (forKey: String, data: [[String: Any]], suiteName: String?) -> Bool in
|
|
21
|
+
// Convert the incoming array of dictionaries directly to JSON data
|
|
22
|
+
do {
|
|
23
|
+
let jsonData = try JSONSerialization.data(withJSONObject: data, options: [])
|
|
24
|
+
UserDefaults(suiteName: suiteName)?.set(jsonData, forKey: forKey)
|
|
25
|
+
return true
|
|
26
|
+
} catch {
|
|
27
|
+
// If encoding fails for some reason, return false
|
|
28
|
+
return false
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
Function("setObject") { (forKey: String, data: [String: Any], suiteName: String?) -> Bool in
|
|
33
|
+
do {
|
|
34
|
+
let jsonData = try JSONSerialization.data(withJSONObject: data, options: [])
|
|
35
|
+
UserDefaults(suiteName: suiteName)?.set(jsonData, forKey: forKey)
|
|
36
|
+
return true
|
|
37
|
+
} catch {
|
|
38
|
+
// If encoding fails for some reason, return false
|
|
39
|
+
return false
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
Function("setInt") { (key: String, value: Int, group: String?) in
|
|
44
|
+
let userDefaults = UserDefaults(suiteName: group)
|
|
45
|
+
userDefaults?.set(value, forKey: key)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
Function("setString") { (key: String, value: String, group: String?) in
|
|
49
|
+
let userDefaults = UserDefaults(suiteName: group)
|
|
50
|
+
userDefaults?.set(value, forKey: key)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bacons/apple-targets",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Generate Apple Targets with Expo Prebuild",
|
|
5
|
-
"main": "build/
|
|
5
|
+
"main": "build/ExtensionStorage.js",
|
|
6
|
+
"types": "build/ExtensionStorage.d.ts",
|
|
6
7
|
"files": [
|
|
7
8
|
"app.plugin.js",
|
|
8
9
|
"build",
|
|
10
|
+
"ios",
|
|
11
|
+
"expo-module.config.json",
|
|
9
12
|
"prebuild-blank.tgz"
|
|
10
13
|
],
|
|
11
14
|
"scripts": {
|
|
File without changes
|
|
File without changes
|