# iOS

{% hint style="warning" %}
이 문서는 모피어스 환경에서의 IMQA SDK를 적용하는 방법을 제공합니다.&#x20;

[IMQA MPM, Crash 설치 가이드](https://docs.imqa.io/imqa-guide/installation/ios)를 통해 IMQA agent 코드 적용 후, 이 문서의 내용을 추가적으로 적용해주세요.
{% endhint %}

## 1. Podfile 추가

프로젝트 Podfile 에 다음과 같이 작성합니다.&#x20;

```
pod 'IMQACrashAgent', '~> 3.24.1' 
pod 'IMQAMPMAgent', '~> 3.24.1'
```

## 2. PPHybridViewController 수정

viewWillAppear 코드와 userContentController 함수 부분을 수정합니다.

{% code title="viewWillAppear" %}

```objectivec
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"PPHybridViewController viewWillAppear");
    [_poperaWebview.configuration.userContentController addScriptMessageHandler:self name:@"ImqaBridge"];
}
```

{% endcode %}

{% code title="userContentController" %}

```objectivec
- (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
    if([message body]) {
        if([message.name isEqual: @"ImqaBridge"]) {
           [[IMQAMpm sharedInstance] saveWebviewPostMessage:self :message.body];
         }
        NSLog(@"[message body] = %@", [message body]);
        NSDictionary *dictJson;
        if([[message body] isKindOfClass:[NSDictionary class]]) {
            dictJson = [message body];
            
        } else {
            NSData *jsonData = [[message body] dataUsingEncoding:NSUTF8StringEncoding];
            //colors is a NSArray property used as dataSource of TableView
            dictJson = [NSJSONSerialization JSONObjectWithData:jsonData
                                                       options:NSJSONReadingMutableContainers
                                                       error:nil];
        }
    }
    else{
        NSLog(@"잘못된 javascript 요청입니다.");
    }
}
```

{% endcode %}

## \* Webview Agent 등록

모피어스는 서버에서 모바일 페이지를 가져오는 방식이 아닌 모바일 내부로 static 파일을 내려놓는 방식입니다. 제공해 드리는 모피어스용 webview agent를 모든 페이지에 head의 최상단에 위치하도록 합니다. 모피어스 구조상 cdn 을 사용하지 마시고 직접 리소스를 삽입하시기 바랍니다.
