# Installing MPM SDK

If you add IMQA MPM to an iOS project, you can check the performance issues of the application, which occur in the app developed with Objective-C or Swift, in various viewpoints.&#x20;

The latest update guide will be updated at <https://github.com/onycom-imqa/IMQA-MPM-Release>.

{% hint style="warning" %}
\[Supported OS] iOS 9.0 and later / latest version of Xcode
{% endhint %}

{% hint style="warning" %}
Please install Crash SDK after creating an iOS project at the IMQA project page and receiving the project key.
{% endhint %}

## 1. Installing the SDK

You can install the IMQA MPM SDK using one of the following two methods. You can select your preferred installation method according to your environment.

### XCFrameworks

1\. Download the latest SDK file using the link below: \
&#x20; \[<https://github.com/onycom-imqa/IMQA-MPM-Release/releases/download/v3.18.0/IMQAMPMAgent.zip>]

2\. Extract the downloaded file and drag and move the IMQAMpmAgent.framework file to Targets > \[Project Name] > General > Frameworks, Libraries, and Embedded Content.

3\. Change the Embed Option of IMQAMpmAgent to Embed & Sign.

4\. Now, SDK installation is complete.

### CocoaPods method

1\. Enter the following statement in Podfile:

```
use_frameworks!
pod 'IMQAMPMAgent', '3.24.1'
```

2\. Run “pod install” at the project directory in the terminal.

3\. Now, the SDK installation is complete.

## 2. Setting the project

### etting an Objective-C project

Change “Always Embed Swift Standard Libraries” to YES in Targets > \[Project Name] > Build Settings > Build Options.

## 3. Codes for initializing the Objective-C environment

### Setting the AppDelegate.m header

Add the IMQAMpmAgent header to AppDelegate.m.

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

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

{% endcode %}

### Setting the AppDelegate.m execution code

Initialize IMQAMpmAgent in application: didFinishLaunchingWithOptions: at AppDelegate.m. You can receive PROJECT\_KEY after creating a project on the IMQA website.

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

```swift
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    NSString* PROJECT_KEY = @"<PROJECT_KEY>"; 
    IMQAConfig* mpmConfig = [[IMQAConfig alloc] init:PROJECT_KEY]; 
    [[IMQAMpm sharedInstance] runWithConfig:mpmConfig];
    
    return YES;
}
```

{% endcode %}

## 4. Codes for initializing the Swift environment

### Setting the AppDelegate.swift header

Add the IMQAMpmAgent header to AppDelegate.swift.

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

```swift
import IMQAMPMAgent
```

{% endcode %}

### Setting the AppDelegate.swift execution code

Initialize IMQAMpmAgent in application: didFinishLaunchingWithOptions: at AppDelegate.swift. You can receive PROJECT\_KEY after creating a project on the IMQA website.

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

```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
        
    let PROJECT_KEY = ""
    let mpmConfig = IMQAConfig(PROJECT_KEY) 
    IMQAMpm.sharedInstance.run(config: mpmConfig)
        
    return true
}
```

{% endcode %}

## 5. Detailed setting

The iOS MPM framework supports various settings.

### Setting the collection interval

You can set the interval for the IMQA MPM framework to save and collect data in seconds.&#x20;

* **Objective-C**

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

```objectivec
IMQAConfig* mpmConfig = [[IMQAConfig alloc] init:@"PROJECT_KEY"];
mpmConfig.dump_interval = 10; // Interval of getting the performance data dump (sec)
mpmConfig.save_interval = 60; // Interval of sending performance data to the collection server (sec)
```

{% endcode %}

* **Swift**

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

```swift
let mpmConfig = IMQAConfig("<PROJECT_KEY>")
mpmConfig.dump_interval = 10 // Interval of getting the performance data dump (sec)
mpmConfig.save_interval = 60 // Interval of sending performance data to the collection server (sec)
```

{% endcode %}

### Setting the collection server

{% hint style="warning" %}
Clients who have selected an installation-type must perform this setting.
{% endhint %}

The collection server can be set separately, which sends data to be collected by the IMQA MPM framework. Basically, no separate settings are required for clients using SaaS.

* **Objective-C**

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

```objectivec
NSString* imqaUrl = @"https://custom-url"; 
IMQAConfig* mpmConfig = [[IMQAConfig alloc] init:PROJECT_KEY]; 
mpmConfig.api_url = imqaUrl;
```

{% endcode %}

* **Swift**

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

```swift
let imqaUrl = "https://custom-url" 
let mpmConfig = IMQAConfig(PROJECT_KEY) 
mpmConfig.api_url = imqaUrl
```

{% endcode %}

### Setting network collection&#x20;

The IMQA MPM framework can set whether or not to collect network information. The framework also provides a function that can ignore the collection of a specific URL, which enables you to ignore the URL that requires security.

* **Objective-C**

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

```objectivec
IMQAConfig* mpmConfig = [[IMQAConfig alloc] init:@"PROJECT_KEY"];
mpmConfig.useNetworkCollect = YES; // YES: Collect, NO: Do not collec
mpmConfig.ignoreUrl = @[@"https://url1.com", @"http://url.com"]; // An array of specific URLs to ignore
```

{% endcode %}

* **Swift**

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

```swift
let mpmConfig = IMQAConfig("<PROJECT_KEY>")
mpmConfig.useNetworkCollect = true // true: Collect, false: Do not collect
mpmConfig.ignoreUrl = ["https://url1.com", "http://url.com"] // An array of specific URLs to ignore
```

{% endcode %}

### Checking collected data&#x20;

If you want to check the data collected by the IMQA MPM framework, you can check the data on the console using its own options.

* **Objective-C**

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

```objectivec
IMQAConfig* mpmConfig = [[IMQAConfig alloc] init:@"PROJECT_KEY"];
mpmConfig.downloadDumpData = YES;
```

{% endcode %}

* **Swift**

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

```swift
let mpmConfig = IMQAConfig("<PROJECT_KEY>")
mpmConfig.downloadDumpData = true
```

{% endcode %}

## 6. Setting WKWebView

The IMQA MPM framework also collects the performance information of WKWebView. Please be noted that UIWebView is not supported due to the Apple policy.

### Setting the web server (HTML)

The IMQA WebView JavaScript library should be inserted into a web page to collect WKWebView data. If you wish, you can also download the file and use it by adding a direct path to the web page.

{% hint style="info" %}
For information on how to install the Agent when using MPM / WPM integration, refer to ‘IMQA WPM/WCrash Installation Guide > 1.2. Please refer to 'Using WPM / MPM integration (Installing WebAgent + WebviewAgent)'.
{% endhint %}

{% code title="page.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 %}

```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>
```

### Setting iOS project&#x20;

The WebView that requires performance collection requires the following settings. Additionally, error information occurring in WebView can be collected. To collect Webview Crash, you must add ImqaBridgeCrash.

* **Objective-C**

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

```objectivec
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
   // IMQA settings section
   if([message.name isEqual: @"ImqaBridge"]) {
        [[IMQAMpm sharedInstance] saveWebviewPostMessage:self :message.body];
   }
   // Additional setup code (Webview Agent (ver. 1.1.2 later) WebView Crash collection)
   if(message.name == "ImqaBridgeCrash") {
    IMQACrash.notifyWebCrash("\(message.body)")
   }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL* url = [[NSURL alloc] initWithString:@""];
    NSURLRequest* reqeust = [[NSURLRequest alloc] initWithURL:url];
    [_webView loadRequest:request];
    [_webView.configuration.userContentController addScriptMessageHandler:self           name:@"ImqaBridge"];
}
```

{% endcode %}

* **Swift**

{% code title="ViewController.swift" %}

```swift
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
    // IMQA settings section
    if(message.name == "ImqaBridge") {
        IMQAMpm.sharedInstance.saveWebviewPostMessage(self,"\(message.body)")
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    let request = URLRequest(url: URL(string: url)!) webView.load(request)

    // IMQA settings section
    webView.configuration.userContentController.add(self, name: "ImqaBridge")
}
```

{% endcode %}

## 7. Network Collection (NSURLSession or URLSession)

When using NSURLSession or URLSession to communicate over the network, some settings are required for network collection.

* **Objective-C**&#x20;

```swift
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; // or default
configuration.protocolClasses = [[IMQAMpm sharedInstance] imqaURLSessionProtocol];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
```

* **Swift**

```objectivec
let configuration = URLSessionConfiguration.ephemeral // or default
configuration.protocolClasses = IMQAMpm.sharedInstance.imqaURLSessionProtocol()
let session = URLSession.init(configuration: configuration)
```

## 8. 2.5. dSYM Settings

For apps with iOS Symbolication, you can view class names and function names through dSYM settings. See Using MPM > Management > Project Management > dSYM Settings.

<br>

<br>
