Embedding the Poll in your Mobile App
Navigation
Navigation is a core part of your app, so our SDK aims to be flexible and allows you to control the navigation.
You will want to consider the following when planning navigation:
- Opening the poll after creating it
- Navigation to the Details screen of a Poll Option
- Opening a poll from a deeplink (for voters)
- Closing/Dismissing the poll
PollViewController
The PollViewController handles all of the functionality of viewing and interacting the a poll. The view controller can be used to display the poll after creating it, or for displaying a poll when a user opens it or deep links into it.
In the example below, we are creating an instance of the view controller and presenting it.
- Swift
let pollViewController = PollViewController(pollUrl: pollUrl, flow: .createPoll, delegate: self) present(pollViewController, animated: true, completion: nil)
Delegate
The PollViewControllerDelegate
can be used to receive callbacks about what is happening in the poll.
- Swift
extension YourViewController: PollViewControllerDelegate { func didPressViewDetails(option: PollOptionResponse) { // navigate to the details screen for the option let detailViewController = DetailViewController(resourceId: option.resourceId) navigationController?.pushViewController(detailViewController, animated: true) } func didReceiveAnalyticsEvent(event: AnalyticsEvent) { // forward analytics to your system (e.g. Google Analytics) Analytics.track(event: event.name, parameters: event.parameters) } }
Putting it all together
At this point we have learned how to:
- Configure the SDK
- Create a poll by mapping our data into the poll fields
- Render a poll using the PollViewController
- Handle navigating to the Details Screen of an Option
Let's see it all together below
- Swift
// configure Polls Platform SDK let config = PollsPlatform.Config(apiKey: "YOUR API KEY", domainConfig: .subdomain(subdomain: "YOUR SUBDOMAIN"), environment: .production) PollsPlatform.setConfig(config: config) // create a poll let poll = Poll(ownerId: ownerId, settings: settings, title: title, options: options) let response = PollsPlatform.createPoll(poll: poll)! let pollViewController = PollViewController(pollUrl: response.url, flow: .createPoll, delegate: self) present(pollViewController, animated: true, completion: nil) // deep link into a poll func handleDeepLink(url: URL) { if (url.path.starts(with: "/poll/")) { // this is a poll url let pollViewController = PollViewController(pollUrl: response.url, // note we use .openPoll here flow: .openPoll, delegate: self) present(pollViewController, animated: true, completion: nil) } }