feature/replaceExistingPage #4
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Steps to Replace Existing View for Micro Frontend Ionic Portal
Initialize an Angular Project
To begin, we will initialize an Angular project at the root of your project directory using Angular CLI.
Note: Ensure you have Angular CLI installed with the command:
Now, create an Angular application with the command:
Install Ionic Portals
Once the project is created, proceed to install Ionic Portals in the Angular project to facilitate communication between the web app and the native application. Use the command:
Additionally, add Ionic to design your loading page:
Modify the Main View
In the main view of the web app (
app.component.html), add elements to show that user information is being processed:Add Timeout in Component
In
app.component.ts, add a timeout to delay the loading and better appreciate the web app:We have imported the
publishmethod from Ionic Portals, which will be used to communicate with the native application through PubSub communication. Here, our web app will be the publisher, notifying that the view is loaded by communicating via the channel/topicloadingwith dataend, indicating that 3 seconds have passed.Build the Application
Finally, build the application with the command:
Then, copy the generated code from the
distfolder to the main folder of your native app (in this case,HabiticaRPG), and rename the folder toLoadingScreenPortalas this name will be used to initialize our Ionic portal later.Replace Native iOS View
To start replacing a view in the native iOS application, first replace the superclass of
ViewController. For example, replace:with:
This change allows us to initialize our Ionic portal by overriding the
loadView()method:Where
LoadingScreenPortalis the name of the folder containing your web app generated withnpm run build. Thus, theinit()method is no longer necessary as the view will be generated from the web app.Create a Subscriber
As a final step, create a subscriber within the
viewDidLoadmethod:Where
dismissCancellableis of typeAnyCancellable?. This code subscribes to theloadingtopic (the channel), receives data as aStringon the main thread, and dismisses the view with the Ionic portal, continuing the user's navigation.This guide demonstrates how to use micro frontends in your native application and communicate between the web app and the native app. I hope this information was useful and enjoyable. Please share this publication, as we frequently share similar material to keep everyone updated. Thank you for your attention, and see you in the next guide.
Steps to Create a New Ionic Portal
Initialize an Angular Project
To begin, we will initialize an Angular project at the root of your project directory using Angular CLI.
Note: Ensure you have Angular CLI installed with the command:
Now, create an Angular application with the command:
Install Ionic Portals
Once the project is created, proceed to install Ionic Portals in the Angular project to facilitate communication between the web app and the native application. Use the command:
Additionally, add Ionic to design your loading page:
Modify the Main View
In the main view of the web app (
app.component.html), add elements to show the information about Open Forge:Add Event for the Button
In
app.component.ts, add an event for the button to return to the previous scene from the About view:Style the Application
In your styles file (
app.component.scss), add the following styles:Build the Application
Finally, build the application with the command:
Then, copy the generated code from the
distfolder to the main folder of your native app (in this case,HabiticaRPG), and rename the folder toHabiticaAboutOFPortalas this name will be used to initialize our Ionic portal later.Create a New Scene in iOS
The first step to create our Ionic portal from the native application side is to create a new UIView scene which will use the
AboutOFViewControllercontroller with the following code:In the code above,
HabiticaAboutOFPortalis the name of the folder containing your web app generated withnpm run build.This allows us to initialize our Ionic portal by overriding the
loadView()method:Subscribe to the Event
The code that listens for the event from our web app in the
viewDidLoadlifecycle hook is:Where
dismissCancellableis of typeAnyCancellable?. This code subscribes to thenavigatetopic (the channel), receives data as aStringon the main thread, and navigates to the next scene in our storyboard (in this case, the Loading scene).Define the Segue
Define the segue in your storyboard to connect

AboutOFViewControllerwithLoadingViewControllerand name itAboutSegue.Final Remarks
This guide demonstrates how to use micro frontends in your native application and communicate between the web app and the native app. I hope this information was useful and enjoyable. Please share this publication, as we frequently share similar material to keep everyone updated. Thank you for your attention, and see you in the next guide.