@@ -20,7 +20,9 @@ npm install @nativescript/ionic-portals
2020
2121## Usage
2222
23- 1 . Register and create portals on app boot - [ Get a Portal API Key here] ( https://ionic.io/docs/portals/getting-started/guide ) :
23+ ### 1. Register portals on app boot
24+
25+ [ Get a Portal API Key here] ( https://ionic.io/docs/portals/getting-started/guide ) :
2426
2527``` ts
2628import { Application } from ' @nativescript/core'
@@ -29,21 +31,24 @@ import { IonicPortalManager } from '@nativescript/ionic-portals'
2931Application .on (Application .launchEvent , () => {
3032 // Register IonicPortals
3133 IonicPortalManager .register (' <portal-api-key>' )
32-
33- // Create as many Portals as you need to use in your app
34- // By default, the app will look for folders equal to the portal id you use here
35- // For iOS: App_Resources/iOS/webPortal
36- // For Android: App_Resources/Android/src/main/asssets/webPortal
37- IonicPortalManager .create (' webPortal' )
3834})
3935
4036// boot app here, for example:
4137Application .run ({ moduleName: ' app-root' })
4238```
4339
44- 2 . Use in your views
40+ Create as many Portals as you need to use in your app.
41+
42+ The app will look for folders within it's resources where the folder name is equal to the portal ` id ` you use to define each portal.
43+
44+ Given the following examples, ensure your web portal is built into the following folders:
45+
46+ - For iOS: ` App_Resources/iOS/webPortal `
47+ - For Android: ` App_Resources/Android/src/main/asssets/webPortal `
4548
46- ### Vanilla/Plain/Core
49+ ### 2. Use in your views
50+
51+ #### Vanilla/Plain/Core
4752
4853``` xml
4954<Page xmlns =" http://schemas.nativescript.org/tns.xsd"
@@ -55,7 +60,7 @@ Application.run({ moduleName: 'app-root' })
5560</Page >
5661```
5762
58- ### Angular
63+ #### Angular
5964
6065``` ts
6166import { registerElement } from ' @nativescript/angular'
@@ -66,17 +71,74 @@ registerElement('IonicPortal', () => IonicPortal)
6671;< IonicPortal id = " webPortal" > </IonicPortal >
6772```
6873
69- ### API
74+ ## Communication
75+
76+ - Send events from NativeScript to any web portal:
77+
78+ ```
79+ IonicPortalManager.publishTopic('hello', { name: 'data from NativeScript' });
80+ ```
81+
82+ - Subscribe to events sent from any web portal:
83+
84+ ```
85+ const subscriptionId = IonicPortalManager.subscribeToTopic('useful-web-event', result => {
86+ console.log('received web portal useful-web-event with data:', result.data);
87+ });
88+ ```
89+
90+ - Unsubscribe from events sent from any web portal:
7091
71- - ` IonicPortalManager.register(apiKey: string) ` : Register Portals when your app boots
92+ ```
93+ IonicPortalManager.unsubscribeFromTopic('useful-web-event', subscriptionId);
94+ ```
95+
96+ ## API
7297
73- - https://ionic.io/docs/ portals/getting-started/guide#configure
98+ Interact and configure portals via ` IonicPortalManager ` which provides the following APIs:
7499
75- - ` IonicPortalManager.create(portalId: string, startDir?: string) ` : Create a Portal
76- - ` portalId ` : The portal id to register
77- - ` startDir ` : Set the web applications directory. By default it will look for a folder named the same as the portalId as the location of the web assets. Use this optional 2nd arg if you would like the folder name to be different that the portalId.
100+ ``` ts
101+ class IonicPortalManager {
102+ /**
103+ * Register Portals when your app boots
104+ * https://ionic.io/docs/portals/getting-started/guide#configure
105+ * @param apiKey your portal api key
106+ */
107+ static register(apiKey : string ): void
108+ /**
109+ * Used to set the initial context of any portal id before the portal is shown
110+ * @param id portal id
111+ * @param initialContext data provided as the initial context to the portal
112+ */
113+ static setInitialContext(id : string , initialContext : any ): void
114+ /**
115+ * Define usage of non-official Capacitor Plugins via Android package names
116+ * @param plugins list of non-official Capacitor package names
117+ */
118+ static setAndroidPlugins(plugins : Array <string >): void
119+ /**
120+ * Send a message to any web portal via publishing a topic (aka. event)
121+ * @param topic name of topic/event
122+ * @param data payload to send
123+ */
124+ static publishTopic(topic : string , data ? : any ): void
125+ /**
126+ * Listen to any message sent from any web portal via subscribing to the topic (aka. event)
127+ * @param topic name of topic/event
128+ * @param callback method which is invoked everytime a message is sent via the topic
129+ * @returns subscription id used to unsubscribe later
130+ */
131+ static subscribeToTopic(topic : string , callback : (data ? : any ) => void ): number
132+ /**
133+ * Unsubscribe from any topic (aka. event)
134+ * @param topic name of topic/event
135+ * @param subscriptionId subscription id
136+ */
137+ static unsubscribeFromTopic(topic : string , subscriptionId : number ): void
138+ }
139+ ```
78140
79- ### Use Capacitor Plugins
141+ ## Use Capacitor Plugins
80142
81143Refer [ to this blog post] ( https://blog.nativescript.org/ionic-portals-with-capacitor-plugins ) .
82144
0 commit comments