Skip to main content

Using Portals in iOS

Once you obtain an API key and install Ionic Portals, you can start creating Portals for your application.

Registering with your Portals Key

Before using Ionic Portals, you must register with your API key. A typical place to do so is in the AppDelegate application(_:didFinishLaunchingWithOptions) method. There, you can use the PortalsRegistrationManager to register:

AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
PortalsRegistrationManager.shared.register(key: "MY_API_KEY")
return true
}

If you're integrating Ionic Portals in a pure SwiftUI application, you can register your API key in your Apps initializer:

PortalsApp.swift
import SwiftUI
import IonicPortals

@main
struct PortalsApp: App {
init() {
PortalsRegistrationManager.shared.register(key: "MY_API_KEY")
}

var body: some Scene {
WindowGroup {
ContentView()
}
}
}
caution

Avoid committing your Portals key to source code repositories where it may be publicly visible! On iOS, you can use an .xcconfig file to keep it out of a public repository.

Creating a Portal

Create a Portal via it's initializer:

let portal = Portal(name: "webapp")

Portal also conforms to ExpressibleByStringLiteral:

let portal: Portal = "webapp"

By default, a Portal will use the name property as the directory to load web content from (relative to the root of Bundle.main). You can specify another location if needed:

let portal = Portal(name: "webapp", startDir: "portals/webapp")

Using PortalView and PortalUIView

After you initialize a Portal, you create a PortalView (for SwiftUI) or PortalUIView (for UIKit) by passing in the Portal.

Portal Initializer
class ViewController: UIViewController {
override func loadView() {
let portal = Portal(name: "webapp")
self.view = PortalUIView(portal: portal)
}
}
ExpressibleByStringLiteral
class ViewController: UIViewController {
override func loadView() {
self.view = PortalUIView(portal: "webapp")
}
}

Adding Web Code

Now that your Portal is successfully created and added to the view, you need to add the web assets to your application. In iOS, the web folder needs to be copied and added to the XCode project. After the folder is added, you can update its contents with fresh builds of the web application. For more information on how to set up your web bundle, see our how-to guide on how to pull in a web bundle.