Display web pages hosted on your ServiceNow instance
Summarize
Summary of Display Web Pages Hosted on Your ServiceNow Instance
This guide provides instructions on how to display web pages hosted within your ServiceNow instance using the Mobile SDK and NowWebService API. It outlines the authentication and session management processes necessary to facilitate dynamic interactions on these web pages.
Show less
Key Features
- Authentication and Session Management: The Mobile SDK manages authentication, creating HTTP requests with bearer headers containing OAuth access tokens for web page requests. After the initial request, session validation relies on cookies.
- Session Expiry Handling: If a user is inactive beyond the timeout period, they are redirected to the login page, and a new session is initiated using a refreshed access token.
- NowWebService Initialization: Customers must create an instance of NowWebService by calling the
makeWebService()method, providing the ServiceNow instance URL and an initialized NowSDK. - Pre-loading Pages: Users can enhance load times by pre-loading cacheable resources using the
preloadWebCache()function. - Creating Web View Controllers: To display web content, the
makeWebViewController()method is used to create a NowWebViewController instance, allowing navigation to ServiceNow-hosted pages. - Theming the User Interface: The web UI can be customized by passing a theme object to the view controller creation method, allowing adjustments to colors and styles according to user preferences.
Key Outcomes
By utilizing the Mobile SDK and NowWebService API, ServiceNow customers can effectively display and interact with their hosted web pages in mobile applications. This capability enhances user experience through dynamic content access, efficient session management, and customizable UI elements, ultimately leading to improved engagement and productivity within the ServiceNow environment.
You can display web pages hosted within your ServiceNow instance in your native iOS application.
The Mobile SDK provides seamless handling of the required authentication and session management, while the NowWebService API provides the functionality required to handle pages residing on your ServiceNow instance. These web pages have all the dynamic interactions that are possible on the ServiceNow platform.
When the webview is requested to load a web page hosted on a ServiceNow instance, it creates an HTTP request with the appropriate bearer header containing the OAuth access token provided by the NowSDK. When the instance receives this request, it starts a new user session and returns cookies related to that session. As the user interacts with the web pages in the webview, the instance uses the cookies to validate that the session is still valid. At this point, the bearer header is no longer relevant, only the cookies are used for session validation. The web session expires if the user stops interacting with the website for longer than the timeout period. After the session expires, if the user tries to interact with the web page, they are automatically redirected to the login page. The NowWebViewController detects the redirection and attempts to start a new web session by requesting a refreshed access token from the NowSDK and using the token to create a HTTP request (with bearer header) for the last known loaded page.
To use these features, you must first create an instance of the
NowWebService. To do this you must call the
makeWebService() method and provide the URL to your ServiceNow instance and an already initialized NowSDK.
In addition, you must have imported NowWeb within your application. You
should hold a reference to the NowWebService for as long as you are using
the NowWeb service.
The following shows an example of how to create a NowWebService object using async/await:
do {
let service = try await NowWeb.makeWebService(instanceUrl: instanceUrl)
self.webService = service
} catch {
debugPrint("Web Service creation failed with error: \(error.localizedDescription)")
}The following shows an example of how to create a NowWebService object using a completion handler:
import Combine
import NowWeb
import UIKit
...
public func makeWebService(instanceUrl: URL,
completion: @escaping ((Result<NowWeb.NowWebService, NowSDK.NowServiceError>) -> Void))
Pre-load pages to improve NowWeb load time
You can pre-load pages with cacheable resources in the background to improve initial NowWeb load times. The following example shows how to use the webService.preloadWebCache() function to pre-load specified pages.
private func preloadURLs(urls: [URL]) {
guard let webService = webService else {
debugPrint("Web service not initialized")
return
}
do {
try webService.preloadWebCache(urls: urls) {
debugPrint("URLs did complete preloading")
}
} catch {
debugPrint(error.localizedDescription)
}
}
Create views to display web content
Once you have instantiated an instance of NowWebService, you can use it to create views to display web pages hosted on your ServiceNow instance.
The following example shows how to create a NowWebViewController view:
let result = webService.makeWebViewController(for: url,
delegate: self, theme: MyWebTheme(nowUITheme: MyUITheme()))
switch result {
case .success(let viewController):
return viewController
case .failure(let error):
debugPrint("Web view creation failed with error: \(error.localizedDescription)")
}In this example, url is the URL of the initial page to load within the view. This URL must point to a page that is hosted on the specified ServiceNow instance. Relative URLs also work and are relative to the ServiceNow instance targeted by the NowWebService.
Once created, you can push the NowWebViewController onto an existing navigation stack. The initial page is not loaded until you call the loadPage() method.