Mopinion Mobile web SDK iOS
The Mopinion Mobile SDK can be used to collect feedback from iOS apps based on events. To use Mopinion mobile web feedback forms in your app you can include the SDK as a Framework in your Xcode project.
Other Mopinion SDK's are also available:
Contents
- Release notes
- Installation
- Implement the SDK
- Submitting extra data
- Evaluate if a form will open
- Using callback mode
- Edit triggers
Release notes for version 0.5.2
Changes in 0.5.2
- Rebuilt with Xcode 14.1, tested on iOS 16.
- Dropped 32-bit support.
- CocoaPods minimum iOS version raised to 11.
- Removed an unused setting.
- Brought forward the insertion of extra/meta data in the webform.
- Downloaded deployments are cached and only reloaded after least 30 minutes.
Remarks
- This readme is also included in github release 0.5.2-swiftpm, which is repackaged for Swift Package Manager. That release is not designed for cocoapods.
- For cocoapods, only use the plain 0.5.2 release.
Install
The Mopinion Mobile SDK Framework can be installed via either the Swift Package Manager or the popular dependency manager Cocoapods.
Install via Swift Package Manager in Xcode 14
-
If you no longer want to use CocoaPods for your project, then in terminal, at the root folder of your project execute:
pod deintegrate
-
Open your project's
<your-project-name>.xcodeproj
file in Xcode. -
In Xcode 14, from the menu, select
File -> Add Packages…
.
The Swift Package Collections panel appears. -
In the search field of the panel, enter
https://github.com/mopinion/mopinion-sdk-ios-web
and press enter. -
From the drop-down button
Dependency Rule
, chooseExact Version
and in the version field enter0.5.2-swiftpm
. -
Click the button
Add Package
. A package product selection panel appears. -
Choose
MopinionSDK
and click the buttonAdd Package
.
Install CocoaPods native on ARM based Macs
From macOS Monterey 12.1 installation of cocoapods 1.11.2 works out of the box:
sudo gem install cocoapods
Install the SDK with CocoaPods
For Xcode 14, make a Podfile
in root of your project:
platform :ios, '11.0'
use_frameworks!
target '<YOUR TARGET>' do
pod 'MopinionSDKWeb', '>= 0.5.2'
end
Install the needed pods:
$ pod install
After this you should use the newly made <your-project-name>.xcworkspace
file to open in Xcode.
Implement the SDK
In your app code, for instance the AppDelegate.swift
file, put:
import MopinionSDK
...
// debug mode
MopinionSDK.load(<MOPINION DEPLOYMENT KEY>, true)
// live
MopinionSDK.load(<MOPINION DEPLOYMENT KEY>)
The <MOPINION DEPLOYMENT KEY>
should be replaced with your specific deployment key. Copy this key using a web browser from your Mopinion account, in side menu Data collection
, section Deployments
, via the button with symbol <>
.
In a UIViewController, for example ViewController.swift
, put:
import MopinionSDK
...
MopinionSDK.event(self, "_button")
where "_button"
is the default passive form event.
You can also make custom events and use them in the Mopinion deployment interface.
In the Mopinion system you can enable or disable the feedback form when a user of your app executes the event.
The event could be a touch of a button, at the end of a transaction, proactive, etc.
extra data
From version 0.3.1
it's also possible to send extra data from the app to your form.
This can be done by adding a key and a value to the data()
method.
The data should be added before the event()
method is called if you want to include the data in the form that comes up for that event.
MopinionSDK.data(_ key: String, _ value: String)
Example:
import MopinionSDK
...
MopinionSDK.load("abcd1234")
...
MopinionSDK.data("first name", "Steve")
MopinionSDK.data("last name", "Jobs")
...
MopinionSDK.event(self, "_button")
Note: In the set of meta data, the keys are unique. If you re-use a key, the previous value for that key will be overwritten.
clear extra data
From version 0.3.4
it's possible to remove all or a single key-value pair from the extra data previously supplied with the data(key,value)
method.
To remove a single key-value pair use this method:
MopinionSDK.removeData(forKey: String)
Example:
MopinionSDK.removeData(forKey: "first name")
To remove all supplied extra data use this method without arguments:
MopinionSDK.removeData()
Example:
MopinionSDK.removeData()
Evaluate if a form will open
The event() method of the SDK autonomously checks deployment conditions and opens a form, or not.
From SDK version 0.4.6
you can use the evaluate() and related methods to give your app more control on opening a form for proactive events or take actions when no form would have opened.
It can also be used on passive events, but such forms will always be allowed to open.
Procedure overview
- Call the
evaluate()
method and pass it the delegate object that implements theMopinionOnEvaluateDelegate
protocol. - In your delegate's callback method
mopinionOnEvaluateHandler()
, check the response parameters and retrieve theformKey
if there is any. - Optionally, pass the
formKey
to the methodopenFormAlways()
to open your form directly, ignoring any conditions in the deployment.
evaluate() method
Evaluates whether or not a form would have opened for the specified event. If without errors, the delegate object will receive the mopinionOnEvaluateHandler()
call with the response.
public func evaluate( _ event: String, onEvaluateDelegate: MopinionOnEvaluateDelegate )
Parameters:
event
: The name of the event as definied in the deployment. For instance "_button".onEvaluateDelegate
: The object implementing theMopinionOnEvaluateDelegate
protocol to handle themopinionOnEvaluateHandler()
callback method.
mopinionOnEvaluateHandler() method
Method where the app receives the response of the evaluate call. Defined by the MopinionOnEvaluateDelegate
protocol. Note that in case of any system errors this may not be called at all.
func mopinionOnEvaluateHandler(hasResult: Bool, event: String, formKey: String?, response: [String : Any]?)
Parameters:
hasResult
: if true then the form identified by the formKey would have opened. If false then the form would not have opened and the formKey might be null in case no forms were found associated with the event.event
: the original event name that was passed to the evaluate call to check in the deployment.formKey
: identifying key of the first feedback form found associated with the event. Only one formKey will be selected even if multiple forms matched the event name in the deployment.response
: optional dictionary object for extra response details on success/failure and forms. Reserved for future extensions.
openFormAlways() method
Opens the form specified by the formkey, regardless of any proactive conditions set in the deployment.
public func openFormAlways(_ parentView: UIViewController,_ formKey: String)
Parameters:
parentView
: Your UIViewController object that can act as a parent view controller for the SDK.formKey
: key of a feedback form as provided by the mopinionOnEvaluateHandler() call.
Example of using evaluate()
This snippet of pseudo code highlights the key points on how the aforementioned procedure fits together to implement the MopinionOnEvaluateDelegate
protocol.
...
import MopinionSDK
...
// assuming that in your AppDelegate, you already did MopinionSDK.load(<MOPINION DEPLOYMENT KEY>)
...
class ViewController: UIViewController, MopinionOnEvaluateDelegate {
...
func doSomething() {
// check if a form would open
MopinionSDK.evaluate("_myproactiveevent", onEvaluateDelegate: self)
// the actual result will be in the mopinionOnEvaluateHandler call
}
...
// callback handler for protocol MopinionOnEvaluateDelegate
func mopinionOnEvaluateHandler(hasResult: Bool, event: String, formKey: String?, response: [String : Any]?) {
if(hasResult) {
// at least one form was found and all optional parameters are non-null
// because conditions can change every time, use the form key to open it directly
MopinionSDK.openFormAlways(self, formKey!)
}else{
if let _ = formKey {
// Found form wouldn't open for event
// we'll open it anyway using the formKey
MopinionSDK.openFormAlways(self, formKey!)
}else{
// no form found for event
...
}
}
}
...
Using callback mode
By default the SDK manages the feedback form autonomously without further involving your app.
SDK version 0.5.0
introduces callbacks to inform your code of certain actions (MopinionCallbackEvent).
Provide a callback handler to receive a response, containing either data or possible error information.
Procedure overview
- Call the
event()
method and pass it a callback method that implements theMopinionCallbackEventDelegate.onMopinionEvent
protocol. - In your callback method
onMopinionEvent()
, check the kind ofmopinionEvent
and optionally calldidSucceed()
orhasErrors()
on theresponse
to check for errors. - Optionally, call
hasData()
on theresponse
object to check if there is data. - Depending on the kind of
mopinionEvent
, check for the presence of data specified by aResponseDataKey
using the callhasData(ResponseDataKey)
on theresponse
. - To get the data, call
getString(ResponseDataKey)
respectivelygetJSONObject(ResponseDataKey)
on theresponse
, depending on the type of data to retrieve.
You can also provide an optional error-callback handler to event()
to seperately receive responses with error information. In that case the primary handler only receives responses without errors.
event()
method
Callback variants of the Triggers an event you defined in your deployment to open a form and receive MopinionCallbackEvent callbacks. If you don't specify a failHandler, the callback handler will also receive error responses.
func event(parentView: event: onCallbackEvent: onCallbackEventError:)
func event(parentView: event: onCallbackEventDelegate:)
func event(parentView: event: onCallbackEventDelegate: onCallbackEventErrorDelegate:)
Parameters:
parentView
: The UIViewController that serves as parent view of the app.event
: The name of the event as defined in the deployment for the form. For instance "_button".onCallbackEvent
: a closure implementing theonMopinionEvent()
callback.onCallbackEventError
: a closure implementing theonMopinionEventError()
callback for MopinionCallbackEvents that resulted in errors.onCallbackEventDelegate
: The object implementing theMopinionCallbackEventDelegate
protocol to handle theonMopinionEvent()
callback.onCallbackEventErrorDelegate
: The object implementing theMopinionCallbackEventErrorDelegate
protocol to handle theonMopinionEventError()
callback for MopinionCallbackEvents that resulted in errors.
onMopinionEvent()
and onMopinionEventError()
Callback methods These methods you implement in your code to receive MopinionCallbackEvents. They have the same parameters to pass you a response with optional additional information.
What information is provided depends on the type of MopinionCallbackEvent
and its origin.
func onMopinionEvent(mopinionEvent: MopinionCallbackEvent, response: MopinionResponse)
func onMopinionEventError(mopinionEvent: MopinionCallbackEvent, response: MopinionResponse)
Parameters:
-
mopinionEvent
: The kind of response event that you receive from the SDK. Currently one of the following:FORM_OPEN
: when the form is shownFORM_SENT
: when the user has submitted the formFORM_CLOSED
: when the form has closed
-
response
: The MopinionResponse object containing additional information on the MopinionEvent. The response is nevernil
, but use itshasData()
methods to check if it contains any additional data, orhasErrors()
for errors.
MopinionResponse object
The data collection present in this object depends on the kind of MopinionCallbackEvent and its origin. The data is a key-value collection. Both data and errors can be missing. The response object contains methods to inspect and retrieve them.
response.get()
and response.hasData()
Getting data with Check with hasData(key)
first, as the get<>(key)
methods can return null
. Pass a standard ResponseDataKey
to these methods for the data you're interested in.
ResponseDataKey | Method to read it | Description |
---|---|---|
DATA_JSONOBJECT | .getJSONObject() | dictionary of the 'raw' JSONObject with all available data |
FORM_KEY | .getString() | the internal unique identifier for the form |
FORM_NAME | .getString() | the name of the form. Distinct from the title of the form. |
response
MopinionCallbackEvents and provided data in This is the data that can be present for a certain MopinionCallbackEvent:
MopinionCallbackEvent | ResponseDataKeys | Remarks |
---|---|---|
FORM_OPEN | DATA_JSONOBJECT | |
FORM_KEY | ||
FORM_NAME | ||
FORM_SENT | DATA_JSONOBJECT | |
FORM_KEY | ||
FORM_NAME | ||
FORM_CLOSED | DATA_JSONOBJECT | Currently only automatically closed forms provide data |
FORM_KEY | only when autoclosed | |
FORM_NAME | only when autoclosed |
The order in which MopinionCallbackEvents occur is:
1. FORM_OPEN
2. FORM_SENT (only if the user submits a form)
3. FORM_CLOSED
response
errors
Reading Call response.hasErrors()
, followed by response.getError()
to get the error object.
The getError()
method might return nil
.
Callback handler example to run code after send
Pseudo code to show the usage of the event()
callback with closures and some involved objects to implement running code after send.
You must wait for the form to be closed after send before running any code affecting your own UI.
...
import MopinionSDK
...
// assuming that in your AppDelegate, you already did MopinionSDK.load(<MOPINION DEPLOYMENT KEY>)
...
class YourViewController: UIViewController, MopinionOnEvaluateDelegate {
...
var wasFormSent: Bool = false // track state outside closure
...
func demonstrateMopinionCallback() {
self.wasFormSent = false
// open the form associated with the event "_myfeedbackbutton" from the deployment and receive callbacks in the closures
MopinionSDK.event(self, "_myfeedbackbutton", onCallbackEvent: { (mopinionEvent, response) -> (Void) in
print("callback in success closure")
if(mopinionEvent == .FORM_SENT) {
let formKey = response.getString(.FORM_KEY)!
print("The form with formKey=\(formKey) has been sent, but is still displayed")
self.wasFormSent = true
} else if(mopinionEvent == .FORM_CLOSED) {
if(self.wasFormSent) {
let formKey = response.getString(.FORM_KEY) ?? ""
print("The form \(formKey) has beent sent and closed, now you can run some code.")
}
}
}, onCallbackEventError: { (mopinionEvent, response) -> (Void) in
let myError = response.getError();
print("there was an error during callback: \(String(describing: myError))")
} )
}
...
}
...
Edit triggers
In the Mopinion deployment editor you can define event names and triggers that will work with the SDK event names that you used in your app. Login to your Mopinion account and go to Data collection, Deployments to use this functionality.
The custom defined events can be used in combination with rules/conditions:
- trigger:
passive
orproactive
. A passive form always shows when the event is triggered. A proactive form only shows once, you can set the refresh duration after which the form should show again. - submit: allow opening a proactive form until it has been submitted at least once. This affects the trigger rule, to allow opening a form more than once. Support for this appeared in SDK version 0.4.3.
- percentage (proactive trigger): % of users that should see the form
- date: only show the form at at, after or before a specific date or date range
- time: only show the form at at, after or before a specific time or time range
- target: only show the form for a specific OS (iOS or Android) and optional list of versions.