# MPM SDK設定

## 1. 詳細設定

iOS MPMフレームワークには、様々な設定を提供しています。

### 収集周期設定

IMQA MPMフレームワークがデータを保存する周期と収集する周期を秒単位で設定することができます。

* **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 %}

### 収集サーバー設定

{% hint style="warning" %}
インストール型で実行された顧客会社では、必ずこの設定をする必要がございます。
{% endhint %}

IMQA MPMフレームワークが収集するデータを送る収集サーバーを別途に設定することができます。基本的に、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 %}

### ネットワーク収集設定&#x20;

IMQA MPMフレームワークでは、ネットワーク収集に対する収集の有無を設定することができます。また、特定のURLに対する収集を無視できる機能を提供しているため、セキュリティが要求されるURLを無視することができます。

* **Objective-C設定方法**

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

```objectivec
IMQAConfig* mpmConfig = [[IMQAConfig alloc] init:@"PROJECT_KEY"];
mpmConfig.useNetworkCollect = YES; // YES: Collect, NO: Do not collect
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 %}

### 収集データ確認&#x20;

IMQA MPMフレームワークで収集しているデータを確認したい場合は、オプションを通してデータをConsoleで確認することができます。

* **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 %}

## 2. WKWebView設定

IMQA MPMフレームワークは、WKWebViewの性能情報も収集しています。UIWebViewは、Apple社のポリシーによって対応しておりませんのでご注意ください。

### Web Server (HTML)設定&#x20;

WKWebViewデータを収集したい場合は、ウェブページにIMQA WebView JavaScriptライブラリを挿入してください。ファイルをダウンロードして、ウェブページから直接パスを追加して使用することもできます。

{% hint style="info" %}
MPM / WPM統合を使用する場合のエージェントのインストール方法は、「[IMQA WPM / WCRashインストールガイド > WebAgentのインストール > WPM / MPM 統合使用 (WebAgent+WebviewAgentインストール)](https://docs.imqa.io/imqa-guide/jpn/web-wpm-wcrash/webagentnoinsutru#2-wpm-mpm-webagentwebviewagentinsutru)」を参照してください。
{% 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>
```

### iOSプロジェクト設定

性能収集が必要なWebViewに以下のとおり設定してください。また、WebViewで発生したエラー情報を収集することもできます。 Webview Crashを収集するには、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 %}

## 3. ネットワーク収集(NSURLSessionまたはURLSessionを使用する場合)

NSURLSessionまたはURLSessionでネットワークを通信する場合、一部の状況でネットワーク収集のための設定が必要です。

* **Objective-C設定方法**

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

## 4. dSYM設定

iOSのSymbolication（シンボル化）を適用したアプリの場合は、dSYM設定でクラス名と関数名を確認できます。 「IMQA MPMユーザーガイド> 2.4。 プロジェクト管理> dSYM設定」を参照してください。
