Using Portals in iOS
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:
- Swift
- Objective-C
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
PortalsRegistrationManager.shared.register(key: "YOUR_PORTALS_KEY")
return true
}
If you're integrating Ionic Portals in a pure SwiftUI application, you can register your API key in your App
s initializer:
import SwiftUI
import IonicPortals
@main
struct PortalsApp: App {
init() {
PortalsRegistrationManager.shared.register(key: "YOUR_PORTALS_KEY")
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[IONPortalsRegistrationManager shared] registerWithKey:@"YOUR_PORTALS_KEY"];
return YES;
}
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
- Swift
- Objective-C
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")
Create an IONPortal
via it's initializer:
IONPortal *portal = [[IONPortal alloc] initWithName:@"webapp", startDir:nil, initialContext:nil];
By default, IONPortal
will use the name
provided in the initializer if startDir
is nil
.
Using PortalView and PortalUIView
After you initialize a Portal
, you create a PortalView (for SwiftUI) or PortalUIView (for UIKit) by passing in the Portal
.
- UIKit (Swift)
- UIKit (Objective-C)
- SwiftUI
class ViewController: UIViewController {
override func loadView() {
let portal = Portal(name: "webapp")
self.view = PortalUIView(portal: portal)
}
}
class ViewController: UIViewController {
override func loadView() {
self.view = PortalUIView(portal: "webapp")
}
}
@implementation ViewController
- (void)loadView {
IONPortal *portal = [[IONPortal alloc] initWithName:@"webapp" startDir:nil initialContext:nil];
self.view = [[IONPortalUIView alloc] initWithPortal:portal];
}
@end
struct ContentView: View {
var body: some View {
VStack {
// Using ExpressibleByStringLiteral conformance
PortalView(portal: "webapp")
PortalView(portal: .init(name: "webapp", startDir: "portals/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.