Plugins
Ionic Portals
@nativescript/ionic-portals β
Contents β
- @nativescript/ionic-portals
Intro β
A plugin that allows you to use Ionic Portals in NativeScript.
Ionic Portals are supercharged native WebView components for iOS and Android that let you add web-based experiences to native mobile apps.
Ionic Portal View
Installation β
To install the plugin, run the following command from the root of your project:
npm install @nativescript/ionic-portals
Enable Ionic portals in your app β
Below are the steps to enable Ionic Portal in your app.
1. Get a Portal API Key β
2. Register portals β
Register your Ionic Portals, by calling the [IonicPortalManager] class's register() method with the Portal API key.
import { Application } from'@nativescript/core'
import { IonicPortalManager } from'@nativescript/ionic-portals'
Application.on(Application.launchEvent, () => {
// Register IonicPortals
IonicPortalManager.register('<portal-api-key>')
})
// boot app here, for example:
Application.run({ moduleName: 'app-root' })
Create as many Portals as you need to use in your app.
The app will look for folders within its resources where the folder name is equal to the portal id
you used to define each portal.
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
3. Add portals view to markup β
Core β
- Register the plugin namespace with Page's
xmlns
attribute providing your prefix(ionic
, for example).
<Pagexmlns:ionic="@nativescript/ionic-portals">
- Access the
IonicPortal
view through the prefix and add it to markup.
<ionic:IonicPortalid="webPortal">
</ionic:IonicPortal>
Full code
<Pagexmlns="http://schemas.nativescript.org/tns.xsd"
xmlns:ionic="@nativescript/ionic-portals">
<StackLayoutclass="p-20">
<ionic:IonicPortalid="webPortal">
</ionic:IonicPortal>
</StackLayout>
</Page>
Angular β
- To add the
IonicPortal
view to the markup of any component, register it by adding the following code to themain.ts
file:
import { registerElement } from'@nativescript/angular'
import { IonicPortal } from'@nativescript/ionic-portals'
registerElement('IonicPortal', () => IonicPortal)
- Add
IonicPortal
to markup.
<IonicPortalid="webPortal"></IonicPortal>;
Vue β
- To add the
IonicPortal
view in the markup of any component, register it by adding the following code to theapp.ts
file:
import { IonicPortal } from'@nativescript/ionic-portals'
registerElement('IonicPortal', () => IonicPortal)
- Add
IonicPortal
to markup.
<GridLayoutheight="300"class="mt-3 p-3">
<IonicPortalid="webPortal"/>
</GridLayout>
Svelte β
- To use the
IonicPortal
view in the markup of any component, register it by adding the following code to theapp.ts
file:
import { IonicPortal } from'@nativescript/ionic-portals'
import { registerNativeViewElement } from'svelte-native/dom'
registerNativeViewElement('ionicPortal', () => IonicPortal)
- Add
IonicPortal
to markup.
<gridLayoutheight="300"class="mt-3 p-3">
<ionicPortalid="webPortal"/>
</gridLayout>
Send events from NativeScript to any web portal β
To send events from NativeScript to any web portal, use the publishTopic() method:
IonicPortalManager.publishTopic('hello', { name: 'data from NativeScript' })
Subscribe to events sent from web portals β
To subscribe to events sent from any web portal, call the subscribeToTopic method with the event name as the first argument and the event handler as the second argument.
constsubscriptionId= IonicPortalManager.subscribeToTopic(
'useful-web-event',
(result) => {
console.log('received web portal useful-web-event with data:', result.data)
},
)
Unsubscribe from events sent from web portals β
To unsubscribe from events sent from any web portal, call the unsubscribeFromTopic() method with the event name as the first argument and the subscription id as the second argument.
IonicPortalManager.unsubscribeFromTopic('useful-web-event', subscriptionId)
IonicPortalManager API β
Allows you to interact with and configure portals via the following APIs.
register() β
IonicPortalManager.register(apiKey)
Registers portals. Call it in the main.ts
file, before the app boots, in the handler of the Application.launchEvent
event.
Parameter | Type | Description |
---|---|---|
apiKey | string | Your portal API key |
setInitialContext() β
IonicPortalManager.setInitialContext(id, initialContext)
Used to set the initial context of any portal id before the portal is shown.
Parameter | Type | Description |
---|---|---|
id | string | The portal id. |
initialContext | string | Data provided as the initial context to the portal. |
setAndroidPlugins() β
IonicPortalManager.setAndroidPlugins(plugins)
Defines the usage of non-official Capacitor Plugins via Android package names
Parameter | Type | Description |
---|---|---|
plugins | Array<string> | A list of non-official Capacitor package names. |
publishTopic() β
IonicPortalManager.publishTopic(topic, data)
Sends a message to any web portal by publishing a topic (aka. event)
Parameter | Type | Description |
---|---|---|
topic | string | The name of the topic/event |
data | any | Optional: The payload to send with the topic. |
subscribeToTopic() β
subscriptionId: number = IonicPortalManager.subscribeToTopic(topic, (data?:any) =>void))
Listens to any message sent from any web portal by subscribing to the specified topic. It returns a subscription id used to later unsubscribe from the topic
.
Parameter | Type | Description |
---|---|---|
topic | string | The name of the topic/event to subscribe to. |
callback | function | The function invoked every time a message is sent via the topic with an optional data parameter. |
unsubscribeFromTopic() β
IonicPortalManager.unsubscribeFromTopic(topic, subscriptionId)
Parameter | Type | Description |
---|---|---|
topic | string | The name of the topic/event to unsubscribe from. |
subscriptionId | number | The subscription id returned by subscribeToTopic(). |
configureLiveUpdates() β
Note: configure before displaying the portal.
IonicPortalManager.configureLiveUpdates('webPortal', {
appId: 'e2abc12',
channel: 'production',
syncOnAdd: true,
})
Parameter | Type | Description |
---|---|---|
portalId | string | The portal id. |
config | IonicPortalLiveUpdateConfig | Live update configuration. |
syncNow() β
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. |
getLastSync() β
constlastSync= IonicPortalManager.getLastSync('e2abc12')
Parameter | Type | Description |
---|---|---|
appId | string | Portal app id to check last sync. |
Using Capacitor Plugins with Ionic Portals β
Refer to this blog post.
Notes β
For iOS: You may need to add
IPHONEOS_DEPLOYMENT_TARGET = 12.0
to yourApp_Resources/iOS/build.xcconfig
file. If your project containsApp_Resources/iOS/Podfile
, you may need to remove any post install handling which removes deployment targets, for example: Remove anything like this:config.build_settings.delete 'IPHONEOS_DEPLOYMENT_TARGET'
Additional Resources β
- You can learn more about Ionic Portals at Opening Doors with Portals
- Find the video's repo here
License β
Apache License Version 2.0
- Previous
- Image Picker
- Next
- iOS Security
- Contents
- Intro
- Installation
- Enable Ionic portals in your app
- IonicPortalManager API
- Using Capacitor Plugins with Ionic Portals
- Notes
- Additional Resources
- License
Contributors
Last updated: