Skip to content
Open
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
1 change: 1 addition & 0 deletions apps/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"license": "SEE LICENSE IN <your-license-filename>",
"repository": "<fill-your-repository-here>",
"dependencies": {
"@capawesome/capacitor-live-update": "^8.8.0",
"@nativescript/core": "*",
"@nativescript/morph-modal-kit": "file:../../packages/morph-modal-kit",
"@nativescript/flutter": "file:../../packages/flutter",
Expand Down
11 changes: 3 additions & 8 deletions apps/demo/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import { Application } from '@nativescript/core';
// import { init } from '@nativescript/flutter';
// init();

// uncomment to test Ionic Portals
// uncomment to test Ionic Portals live updates with Capawesome Cloud
// import { IonicPortalManager } from '@nativescript/ionic-portals';
// IonicPortalManager.configureLiveUpdates('ionicWebPortalSample', {
// appId: 'e29e2c2e',
// appId: '<capawesome-cloud-app-id>',
// channel: 'production',
// syncOnAdd: true
// })

// Application.on(Application.launchEvent, () => {
// // Register IonicPortals
// IonicPortalManager.register('<portal-api-key>');
// });
// IonicPortalManager.syncLiveUpdates('ionicWebPortalSample');

Application.run({ moduleName: 'app-root' });
160 changes: 97 additions & 63 deletions packages/ionic-portals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@
- [@nativescript/ionic-portals](#nativescriptionic-portals)
- [Contents](#contents)
- [Intro](#intro)
- [Requirements](#requirements)
- [Installation](#installation)
- [Enable Ionic portals in your app](#enable-ionic-portals-in-your-app)
- [1. Get a Portal API Key](#1-get-a-portal-api-key)
- [2. Register portals](#2-register-portals)
- [3. Add portals view to markup](#3-add-portals-view-to-markup)
- [Enable Ionic Portals in your app](#enable-ionic-portals-in-your-app)
- [1. Add your web app to the app resources](#1-add-your-web-app-to-the-app-resources)
- [2. Add the portal view to markup](#2-add-the-portal-view-to-markup)
- [Core](#core)
- [Angular](#angular)
- [Vue](#vue)
- [Svelte](#svelte)
- [Send events from NativeScript to any web portal](#send-events-from-nativescript-to-any-web-portal)
- [Subscribe to events sent from web portals](#subscribe-to-events-sent-from-web-portals)
- [Unsubscribe from events sent from web portals](#unsubscribe-from-events-sent-from-web-portals)
- [Live Updates](#live-updates)
- [1. Create an app in Capawesome Cloud](#1-create-an-app-in-capawesome-cloud)
- [2. Configure live updates for a portal](#2-configure-live-updates-for-a-portal)
- [3. Sync on app start](#3-sync-on-app-start)
- [4. Publish an update](#4-publish-an-update)
- [Migrating from Ionic Appflow](#migrating-from-ionic-appflow)
- [IonicPortalManager API](#ionicportalmanager-api)
- [register()](#register)
- [setInitialContext()](#setinitialcontext)
- [setAndroidPlugins()](#setandroidplugins)
- [publishTopic()](#publishtopic)
- [subscribeToTopic()](#subscribetotopic)
- [unsubscribeFromTopic()](#unsubscribefromtopic)
- [configureLiveUpdates()](#configureliveupdates)
- [syncNow()](#syncnow)
- [getLastSync()](#getlastsync)
- [syncLiveUpdates()](#syncliveupdates)
- [Using Capacitor Plugins with Ionic Portals](#using-capacitor-plugins-with-ionic-portals)
- [Notes](#notes)
- [Additional Resources](#additional-resources)
Expand All @@ -40,39 +44,28 @@ A plugin that allows you to use [Ionic Portals](https://ionic.io/docs/portals) i

![Ionic Portal View](https://raw.githubusercontent.com/NativeScript/ui-kit/main/packages/ionic-portals/images/ionic-portal-ios.png)

## Installation

To install the plugin, run the following command from the root of your project:

```cli
npm install @nativescript/ionic-portals
```
## Requirements

## Enable Ionic portals in your app
The plugin ships Ionic Portals 0.14, which is built on Capacitor 8.

Below are the steps to enable Ionic Portal in your app.
* iOS: deployment target iOS 15 or later and Xcode 26 or later.
* Android: JDK 21 and `minSdkVersion` 24 or higher.

### 1. Get a Portal API Key
Ionic Portals no longer needs an API key or a registration call.

[Get a Portal API Key here](https://ionic.io/docs/portals/getting-started/guide).
## Installation

### 2. Register portals
To install the plugin, run the following command from the root of your project:

Register your Ionic Portals, by calling the [IonicPortalManager] class's [register()](#register) method with the [Portal API key](#1-get-a-portal-api-key).
```cli
npm install @nativescript/ionic-portals @capawesome/capacitor-live-update
```

```ts
import { Application } from '@nativescript/core';
`@capawesome/capacitor-live-update` is a peer dependency. On iOS the plugin compiles the Capawesome Live Update SDK from your `node_modules`. On Android it comes from Maven Central.

import { IonicPortalManager } from '@nativescript/ionic-portals';
## Enable Ionic Portals in your app

Application.on(Application.launchEvent, () => {
// Register IonicPortals
IonicPortalManager.register('<portal-api-key>');
});

// boot app here, for example:
Application.run({ moduleName: 'app-root' });
```
### 1. Add your web app to the app resources

Create as many Portals as you need to use in your app.

Expand All @@ -81,9 +74,9 @@ The app will look for folders within its resources where the folder name is equa
Given the following examples, ensure your web portal is built into the following folders:

* For iOS: `App_Resources/iOS/webPortal`
* For Android: `App_Resources/Android/src/main/asssets/webPortal`
* For Android: `App_Resources/Android/src/main/assets/webPortal`

### 3. Add portals view to markup
### 2. Add the portal view to markup

#### Core

Expand Down Expand Up @@ -122,7 +115,7 @@ registerElement('IonicPortal', () => IonicPortal);

2. Add `IonicPortal` to markup.
```html
<IonicPortal id="webPortal"></IonicPortal>;
<IonicPortal id="webPortal"></IonicPortal>
```

#### Vue
Expand Down Expand Up @@ -183,22 +176,63 @@ To unsubscribe from events sent from any web portal, call the [unsubscribeFromTo
IonicPortalManager.unsubscribeFromTopic('useful-web-event', subscriptionId);
```

## IonicPortalManager API
## Live Updates

Allows you to interact with and configure portals via the following APIs.
Live updates let you ship a new build of a web portal to installed apps without an app store release.

Ionic Portals 0.14 loads live updates through the [Ionic Live Update Provider SDK](https://github.com/ionic-team/live-update-provider-sdk). This plugin ships [Capawesome Cloud](https://capawesome.io/cloud/live-updates/) as the provider. Each web portal uses its own Capawesome Cloud app. See the [Capawesome Cloud guide for Ionic Portals](https://capawesome.io/docs/cloud/live-updates/ionic-portals/) for the full feature set, for example channels, rollbacks and code signing.

### 1. Create an app in Capawesome Cloud

### register()
Create an app in the [Capawesome Cloud Console](https://console.cloud.capawesome.io) for every web portal that you want to update and note its app id.

### 2. Configure live updates for a portal

Call [configureLiveUpdates()](#configureliveupdates) before the portal is shown, for example in the `Application.launchEvent` handler:

```ts
IonicPortalManager.register(apiKey)
import { Application } from '@nativescript/core';
import { IonicPortalManager } from '@nativescript/ionic-portals';

Application.on(Application.launchEvent, () => {
IonicPortalManager.configureLiveUpdates('webPortal', {
appId: '<capawesome-cloud-app-id>',
channel: 'production',
});
});

Application.run({ moduleName: 'app-root' });
```

Registers portals. Call it in the `main.ts` file, before the app boots, in the handler of the `Application.launchEvent` event.
### 3. Sync on app start

| Parameter | Type | Description
|:----------|:-----|:-----------
| `apiKey` | `string` | Your portal API key
A portal only downloads an update when you ask for it. Call [syncLiveUpdates()](#syncliveupdates) once at app start for every configured portal:

```ts
IonicPortalManager.syncLiveUpdates('webPortal').catch((error) => {
console.log('Live update sync failed:', error);
});
```

A portal that is already on screen keeps the current bundle and serves the new one on its next load, for example after `reload()` or an app restart.

### 4. Publish an update

Build your web portal and upload the output with the [Capawesome CLI](https://capawesome.io/cloud/cli/):

```cli
npx @capawesome/cli apps:liveupdates:upload --app-id <capawesome-cloud-app-id> --path ./dist --channel production
```

Ship the same build inside the app resources (see [step 1](#1-add-your-web-app-to-the-app-resources)), so the portal has content before the first sync.

### Migrating from Ionic Appflow

Ionic Appflow reaches end of life on December 31, 2027. Earlier versions of this plugin used Appflow for live updates. To migrate, replace the Appflow `appId` in `configureLiveUpdates()` with your Capawesome Cloud app id, drop `syncOnAdd`, and replace `syncNow()` with `syncLiveUpdates()`. The [migration guide](https://capawesome.io/blog/migrating-ionic-portals-live-updates-from-appflow-to-capawesome/) explains the differences in detail.

## IonicPortalManager API

Allows you to interact with and configure portals via the following APIs.

### setInitialContext()
```ts
Expand Down Expand Up @@ -261,41 +295,38 @@ IonicPortalManager.unsubscribeFromTopic(topic, subscriptionId)

### configureLiveUpdates()

Note: configure before displaying the portal.
Note: configure before displaying the portal. Throws when the configuration is invalid, for example when `appId` is missing.

```ts
IonicPortalManager.configureLiveUpdates('webPortal', {
appId: 'e2abc12',
appId: '<capawesome-cloud-app-id>',
channel: 'production',
syncOnAdd: true
publicKey: '-----BEGIN PUBLIC KEY-----MIGf...IDAQAB-----END PUBLIC KEY-----',
})
```
| Parameter | Type | Description
|:----------|:-----|:-----------
| `portalId` | ` string` | The portal id.
| `config` | `IonicPortalLiveUpdateConfig` | Live update configuration.

### syncNow()
| `config` | `IonicPortalLiveUpdateConfig` | Live update configuration, see below.

```ts
IonicPortalManager.syncNow(['e2abc12'], false, status => {
console.log('sync complete:', status)
})
```
| Parameter | Type | Description
|:----------|:-----|:-----------
| `appIds` | `Array<string>` | Portal app ids to sync.
| `isParallel` | `boolean` | Whether to sync in parallel or not.
| `complete` | `(status: string) => void` | Complete callback.
| Property | Type | Description
|:---------|:-----|:-----------
| `appId` | `string` | The Capawesome Cloud app id of the web portal.
| `channel` | `string` | _Optional_: The channel to sync, for example `production`. Without it the server picks the app's default channel.
| `publicKey` | `string` | _Optional_: The public key as a single-line PEM. With it, bundles without a valid signature are rejected. See [code signing](https://capawesome.io/docs/cloud/live-updates/code-signing/).
| `serverDomain` | `string` | _Optional_: The API domain of the Capawesome Cloud server without scheme or path.
| `httpTimeout` | `number` | _Optional_: The request timeout in milliseconds.

### getLastSync()
### syncLiveUpdates()

```ts
const lastSync = IonicPortalManager.getLastSync('e2abc12')
await IonicPortalManager.syncLiveUpdates('webPortal')
```
Downloads the latest bundle of the portal from Capawesome Cloud. The promise rejects when the portal has no live update configuration, the device is offline, or the download or signature check fails.

| Parameter | Type | Description
|:----------|:-----|:-----------
| `appId` | `string` | Portal app id to check last sync.
| `portalId` | `string` | The portal id configured with [configureLiveUpdates()](#configureliveupdates).

## Using Capacitor Plugins with Ionic Portals

Expand All @@ -304,13 +335,16 @@ Refer to [this blog post](https://blog.nativescript.org/ionic-portals-with-capac
## Notes

> For iOS:
> You may need to add `IPHONEOS_DEPLOYMENT_TARGET = 12.0` to your `App_Resources/iOS/build.xcconfig` file.
> If your project contains `App_Resources/iOS/Podfile`, you may need to remove any post install handling which removes deployment targets, for example:
> The plugin sets the deployment target to iOS 15.0. If your project contains `App_Resources/iOS/Podfile`, remove any post install handling which removes deployment targets, for example:
> Remove anything like this: `config.build_settings.delete 'IPHONEOS_DEPLOYMENT_TARGET'`

> For Android:
> Ionic Portals 0.14 depends on Capacitor 8, which is compiled for Java 21. Build your app with JDK 21.

## Additional Resources
- You can learn more about Ionic Portals at [Opening Doors with Portals](https://www.youtube.com/watch?v=lGeeUjIMjTQ&t=609s)
- Find the video's repo [here](https://github.com/NathanWalker/ioniconf2022)
- [Live Updates for Ionic Portals apps](https://capawesome.io/docs/cloud/live-updates/ionic-portals/) in the Capawesome Cloud docs

## License

Expand Down
18 changes: 9 additions & 9 deletions packages/ionic-portals/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import { View } from '@nativescript/core';

export interface IonicPortalLiveUpdateConfig {
appId: string;
channel: string;
syncOnAdd?: boolean;
channel?: string;
publicKey?: string;
serverDomain?: string;
httpTimeout?: number;
}

export function definedValues(config: IonicPortalLiveUpdateConfig): { [key: string]: string | number } {
return Object.fromEntries(Object.entries(config).filter(([, value]) => value !== undefined));
}

export class IonicPortalManagerCommon {
static AndroidPlugins: Array<string>;
static InitialContexts: { [key: string]: any };
static LiveUpdateConfigs: { [key: string]: IonicPortalLiveUpdateConfig };
static Listeners: { [key: string]: Array<{ callback: (data?: any) => void; subscriptionId: number }> };
static setAndroidPlugins(plugins: Array<string>) {
if (!IonicPortalManagerCommon.AndroidPlugins) {
Expand All @@ -20,12 +26,6 @@ export class IonicPortalManagerCommon {
}
}
}
static configureLiveUpdates(portalId: string, config: IonicPortalLiveUpdateConfig) {
if (!IonicPortalManagerCommon.LiveUpdateConfigs) {
IonicPortalManagerCommon.LiveUpdateConfigs = {};
}
IonicPortalManagerCommon.LiveUpdateConfigs[portalId] = config;
}
static setInitialContext(id: string, initialContext: any) {
if (!IonicPortalManagerCommon.InitialContexts) {
IonicPortalManagerCommon.InitialContexts = {};
Expand Down
Loading