# iOS

{% hint style="warning" %}
This document provides instructions on how to apply IMQA SDK in the Cordova environment.
{% endhint %}

## 1. Installing SDK

1\. Open the \<PROJECT-NAME>.xcworkspace file.

2\. Create a folder called frameworks under the ios folder.

3\. Install the desired version of the framework from the link below and move the framework files to the frameworks folder in Xcode, checking Copy Items If Needed.

* <https://github.com/onycom-imqa/IMQA-MPM-Release/releases>
* <https://github.com/onycom-imqa/IMQA-Crash-Release/releases>&#x20;

4\. In the project settings, set all frameworks to Embed & Sign in Frameworks, Libraries, and Embedded Content.

## 2. Setting SDK

1\.  Import two below from the AppDelegate.h file.

{% code title="AppDelegate.m" %}

```
#import <IMQAMPMAgent/IMQAMPMAgent.h>
#import <IMQACrashAgent/IMQACrash.h>
```

{% endcode %}

2\.  In the AppDelegate.m file, add the code below to the top of - (BOOL)application:(UIApplication\*)application didFinishLaunchingWithOptions:(NSDictionary\*)launchOptions.

{% code title="AppDelegate.m" %}

```
NSString* PROJECT_KEY = @"<PROJECT_KEY>";

IMQACrashConfiguration* crashConfig = [[IMQACrashConfiguration alloc] initWithApiKey:PROJECT_KEY];
[IMQACrash startWithConfiguration:crashConfig];

IMQAConfig* mpmConfig = [[IMQAConfig alloc] init:PROJECT_KEY];
[[IMQAMpm sharedInstance] runWithConfig:mpmConfig];
```

{% endcode %}

3\. If you see the log like below when running it after buildup, the installation is completed successfully.

{% code title="AppDelegate.m" %}

```
[IMQACrash] [INFO] Sent session AC868F82-97E7-4DAA-A75B-E6E5CEB64F46

[IMQA] (1/4) Start tracking viewcontroller
[IMQA] (2/4) Start tracking network
[IMQA] (3/4) Start tracking resource
[IMQA] (4/4) Start tracking backtrace
[IMQA] (4/4) Complete setting IMQA MPM
[IMQA] IMQA MPM Version : <VERSION>
```

{% endcode %}

## 3. Cordova Setup

1\. Install WebView Agent at the top of your HTML. (Requires a build after installation)

{% code title="index.html" %}

```html
<!-- IMQA Webview Agent(ver. 1.1.3 earlier) -->
<script type="text/javascript" src="https://imqawebviewagent.blob.core.windows.net/agent/webview-agent-1.1.2.js"
crossorigin></script>

// If you want to apply it as a min file:
<script type="text/javascript" src="https://imqawebviewagent.blob.core.windows.net/agent/webview-agent-1.1.2.min.js"
crossorigin></script>
```

{% endcode %}

{% code title="index.html" %}

```html
<!-- IMQA Webview Agent (ver. 1.1.3 later) -->
<script type="text/javascript" src="https://cdn.imqa.io/agent/webview-agent-1.1.3.js"
crossorigin></script>

<script>
    ((w, c, _wv, _w, _wk, _mh, _b) => {        
       w[_wv](w); // Webview Agent Execution Function
    })(window, 'imqaClientConfig', 'IMQAWebviewMain', 'IMQAWebMain', 'webkit', 'messageHandlers', 'ImqaBridge')
</script>
```

{% endcode %}

2\. In Xcode, locate the CordovaLib/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m file.

3\. Import MPM SDK at the top in CDVWebViewEngine.m.

{% code title="CDVWebViewEngine.m" %}

```
#import <IMQAMPMAgent/IMQAMPMAgent.h>
```

{% endcode %}

4\. In - (void)pluginInitialize, find a part that modifies WKUserContentController and modify it.

{% code title="CDVWebViewEngine.m" %}

```
// existing code
WKUserContentController* userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:weakScriptMessageHandler name:CDV_BRIDGE_NAME];
// additional code
[userContentController addScriptMessageHandler:self name:@"ImqaBridge"];
```

{% endcode %}

5\. Find and modify - (void)userContentController:(WKUserContentController\*)userContentController didReceiveScriptMessage:(WKScriptMessage\*)message.

<mark style="color:red;">**\*\*\*Must be added to the very top of that function.\*\*\***</mark>

{% code title="CDVWebViewEngine.m" %}

```
if([message.name isEqual: @"ImqaBridge"]) {
 [[IMQAMpm sharedInstance] saveWebviewPostMessage:(CDVViewController*)self.viewController :message.body];
}
```

{% endcode %}

\ <br>
