# Delegate Protocols

The DT Exchange SDK delegate protocols include the following methods:

* [Global Ad](#global-a-d)
* [Unit](#unit)
* [Video](#video-content)
* [HTML/MRAID](#html-mraid)

## Global Ad

To receive global SDK callbacks, implement the `IAGlobalAdDelegate` methods.

### Assigning the Delegate Implementor

Use the following API to assign the delegate implementor:

{% code title="Objective-C" %}

```objective-c
IASDKCore.sharedInstance.globalAdDelegate = ;
```

{% endcode %}

**Example**

{% code title="Objective-C" %}

```objective-c
IASDKCore.sharedInstance.globalAdDelegate = self;
```

{% endcode %}

For more information see, [Impression Level Data](https://docs.digitalturbine.com/dt-exchange/publishers/sdk-configuration/integrating-the-ios-sdk/impression-level-data).

## Unit

{% hint style="warning" %}
`IAParentViewControllerForUnitController:unitController` is a required delegate method. When an ad transitions to full-screen, the IASDK will need to get a "parent" view controller, and this method will be invoked. If it is not implemented, the app will crash with an exception, intentionally, indicating the missing required method implementation.

This delegate method is required to get a client-side view controller to present modal views, such as an in-app browser, StoreKit controller, MRAID expand, etc. In this method, the client side returns a desired view controller. In most cases, it will be the same view controller that presents an ad.
{% endhint %}

### Parent View Controller

`IAParentViewControllerForUnitController:` Parent view controller evaluation from a publisher to render ads in full-screen or modal mode.

{% code title="Objective-C" %}

```objective-c
- (UIViewController* _Nonnull)IAParentViewControllerForUnitController:
    (IAUnitController* _Nullable)unitController {
  return self;  // in case 'self' is a UIViewController subclass;
}
```

{% endcode %}

{% hint style="info" %}
All other delegate methods of this protocol (interface) are optional.
{% endhint %}

### User Click

`IAAdDidReceiveClick:`  will be invoked when the user clicks an ad.

{% code title="Objective-C" %}

```objective-c
- (void)IAAdDidReceiveClick:(IAUnitController * _Nullable)unitController {
    NSLog(@"ad did click");
}
```

{% endcode %}

### Ad Impression

`IAAdWillLogImpression:`  will be invoked when the ad is rendered and is visible.

{% code title="Objective-C" %}

```objective-c
- (void)IAAdWillLogImpression:(IAUnitController * _Nullable)unitController {
    NSLog(@"ad impression");
}
```

{% endcode %}

### Future Fullscreen Transition Notification

`IAUnitControllerWillPresentFullscreen:` indicates that the transition from full-screen mode will be performed.

{% code title="Objective-C" %}

```objective-c
- (void)IAUnitControllerWillPresentFullscreen:(IAUnitController * _Nullable)unitController {
    NSLog(@"ad will present fullscreen");
}
```

{% endcode %}

#### Notification of Performed Fullscreen Transition

`IAUnitControllerDidPresentFullscreen:` indicates that the transition to full screen already performed.

{% code title="Objective-C" %}

```objective-c
- (void)IAUnitControllerDidPresentFullscreen:(IAUnitController * _Nullable)unitController {
    NSLog(@"ad did present fullscreen");
}
```

{% endcode %}

### Future Transition from Fullscreen Notification

`IAUnitControllerWillDismissFullscreen:` indicates that the transition from full-screen mode will happen.

{% code title="Objective-C" %}

```objective-c
(void)IAUnitControllerWillDismissFullscreen:(IAUnitController * _Nullable)unitController {
    NSLog(@"ad will dismiss fullscreen");
}
```

{% endcode %}

#### Notification of Performed Transition from Fullscreen Mode

`IAUnitControllerDidDismissFullscreen:` indicates that the transition from full-screen mode already performed.

{% code title="Objective-C" %}

```objective-c
(void)IAUnitControllerDidDismissFullscreen:(IAUnitController * _Nullable)unitController {
    NSLog(@"ad did dismiss fullscreen");
}
```

{% endcode %}

### Opening an External App Notification

`IAUnitControllerWillOpenExternalApp:` is a notification that your app will move to the background, and an external app, such as Safari, the App Store, or a Universal link-supported app, will open.

{% code title="Objective-C" %}

```objective-c
(void)IAUnitControllerWillOpenExternalApp:(IAUnitController * _Nullable)unitController {
    NSLog(@"ad will open external app");
}
```

{% endcode %}

## Video Content

{% hint style="info" %}
All video delegate methods are optional.
{% endhint %}

### Video Completion Event

`IAVideoCompleted:` is invoked on video completion.

{% code title="Objective-C" %}

```objective-c
- (void)IAVideoCompleted:(IAVideoContentController * _Nullable)contentController {
    NSLog(@"video completed");
}
```

{% endcode %}

### Interruption Event

`videoInterruptedWithError:` invoked if a video started to play, as the ad was already received, but the buffer became empty for some reason and didn't refill.

{% code title="Objective-C" %}

```objective-c
- (void)IAVideoContentController:(IAVideoContentController * _Nullable)contentController
                  videoInterruptedWithError:(NSError * _Nonnull)error {
    NSLog(@"video interrupted");
}
```

{% endcode %}

### Video Duration Received

`videoDurationUpdated:` updated the video's duration.

{% code title="Objective-C" %}

```objective-c
(void)IAVideoContentController:(IAVideoContentController * _Nullable)contentController
                videoDurationUpdated:(NSTimeInterval)videoDuration {
    NSLog(@"video duration updated");
}
```

{% endcode %}

### Progress Tracking

`videoProgressUpdatedWithCurrentTime:totalTime:` is invoked each time the video has played for a certain number of milliseconds.

{% code title="Objective-C" %}

```objective-c
(void)IAVideoContentController:(IAVideoContentController * _Nullable)contentController
     videoProgressUpdatedWithCurrentTime:(NSTimeInterval)currentTime
                              totalTime:(NSTimeInterval)totalTime {
    NSLog(@"video progress updated");
}
```

{% endcode %}

## HTML/MRAID

MRAID delegate methods allow you to manage interactive HTML ad behavior, including resize, expand, and collapse events.

{% hint style="info" %}
All HTML / MRAID delegate methods are optional.
{% endhint %}

### MRAID Resize Event (Future)

`MRAIDAdWillResizeToFrame:` will be invoked on the MRAID Resize command, before the ad transformation.

{% code title="Objective-C" %}

```objective-c
- (void)IAMRAIDContentController:(IAMRAIDContentController * _Nullable)contentController 
                  MRAIDAdWillResizeToFrame:(CGRect)frame {
    NSLog(@"MRAIDAdWillResizeToFrame");
    self.isMRAIDResize = YES; // a way to distinct in 'collapse', whether was resize or expand;
    
    if (!self.adView.translatesAutoresizingMaskIntoConstraints) { 
        // a way to distinct the work with constraints;
        // ok, we are working with constraints, if so - it is publisher responsibility to treat MRAID:RESIZE:
        //
        // 1. remove existing constraints from adView (in this method);
        // 2. set new constraints, satisfying the received frame ('MRAIDAdDidResizeToFrame' method);
        //
        // note: that if you are not working with constraints, the adView will treat all the needed sizes, 
        // but you will need to setup other UI in current view controller, according to adView's new frame;
        // note: MRAID:EXPAND is MODAL, so no need to implement the same work;
        [self.adView removeFromSuperview]; // remove self.view <--> adView constraints;
    }
    
    // the rest of work is implemented inside the 'MRAIDAdDidResizeToFrame:' method;
}</-->
```

{% endcode %}

### MRAID Resize Event (Happened)

`MRAIDAdDidResizeToFrame:` will be invoked on MRAID Resize completion.

{% code title="Objective-C" %}

```objective-c
- (void)IAMRAIDContentController:(IAMRAIDContentController * _Nullable)contentController 
                 MRAIDAdDidResizeToFrame:(CGRect)frame {
    NSLog(@"MRAIDAdDidResizeToFrame");
    
    // means we are working with constraints;
    if (!self.adView.translatesAutoresizingMaskIntoConstraints) {
        [self.viewUnitController showAdInParentView:self.view]; 
        // add once again to view, because was removed previously in order to remove constraints;
        
        // if so, it is on publisher responsibility to set up a new consrtraints:
        [self.view addConstraint:
         [NSLayoutConstraint constraintWithItem:self.adView
                                      attribute:NSLayoutAttributeLeading
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:self.view
                                      attribute:NSLayoutAttributeLeading
                                     multiplier:1
                                       constant:frame.origin.y]];
        
        [self.view addConstraint:
         [NSLayoutConstraint constraintWithItem:self.adView
                                      attribute:NSLayoutAttributeWidth
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:nil
                                      attribute:NSLayoutAttributeWidth
                                     multiplier:1
                                       constant:frame.size.width]];
        
        [self.view addConstraint:
         [NSLayoutConstraint constraintWithItem:self.adView
                                      attribute:NSLayoutAttributeHeight
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:nil
                                      attribute:NSLayoutAttributeHeight
                                     multiplier:1
                                       constant:frame.size.height]];
    } else {
        // working with 'frame',
        // set up your UI according to adView's new frame;
    }
}
```

{% endcode %}

### MRAID Expand Event (Future)

`MRAIDAdWillExpandToFrame:` will be invoked on the MRAID Expand command, before the ad transformation.

{% code title="Objective-C" %}

```objective-c
- (void)IAMRAIDContentController:(IAMRAIDContentController * _Nullable)contentController 
                 MRAIDAdWillExpandToFrame:(CGRect)frame {
    NSLog(@"MRAIDAdWillExpandToFrame");
    
    self.isMRAIDResize = NO; 
// boolean flag to distinct whether 'collapse' event is after 'resize' or 'expand'
}
```

{% endcode %}

### MRAID Expand Event (Happened)

`MRAIDAdDidExpandToFrame:`  will be invoked on the MRAID Expand completion event.

{% code title="Objective-C" %}

```objective-c
- (void)IAMRAIDContentController:(IAMRAIDContentController * _Nullable)contentController 
                MRAIDAdDidExpandToFrame:(CGRect)frame {
    NSLog(@"MRAIDAdDidExpandToFrame");
}
```

{% endcode %}

### MRAID Collapse Event (Future)

`IAMRAIDContentControllerMRAIDAdWillCollapse:` will be invoked on an MRAID Collapse command, before the ad transformation.

{% code title="Objective-C" %}

```objective-c
NSLog(@"IAMRAIDContentControllerMRAIDAdWillCollapse");

// if we are working with constraints AND there was resize before (not expand):
if (!self.adView.translatesAutoresizingMaskIntoConstraints && self.isMRAIDResize) {
    // the same as in 'MRAIDAdWillResizeToFrame';
    [self.adView removeFromSuperview];
}
```

{% endcode %}

### MRAID Collapse Event (Happened)

`IAMRAIDContentControllerMRAIDAdDidCollapse:`  will be invoked upon MRAID Collapse completion.

{% code title="Objective-C" %}

```objective-c
- (void)IAMRAIDContentControllerMRAIDAdDidCollapse:(IAMRAIDContentController * _Nullable)contentController {
    NSLog(@"IAMRAIDContentControllerMRAIDAdDidCollapse");
    
    // if we are working with constraints AND there was resize before (not expand):
    if (!self.adView.translatesAutoresizingMaskIntoConstraints && self.isMRAIDResize) {
        // restore everything as it was before resize (in case it is resize):
        [self.viewUnitController showAdInParentView:self.view];
        
        // adding centerX constraint
        [self.view addConstraint:
         [NSLayoutConstraint constraintWithItem:self.adView
                                      attribute:NSLayoutAttributeCenterX
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:self.view
                                      attribute:NSLayoutAttributeCenterX
                                     multiplier:1
                                       constant:0]];
        
        // adding top constraint
        [self.view addConstraint:
         [NSLayoutConstraint constraintWithItem:self.adView
                                      attribute:NSLayoutAttributeTop
                                      relatedBy:NSLayoutRelationEqual
                                         toItem:self.view
                                      attribute:NSLayoutAttributeTop
                                     multiplier:1
                                       constant:0]];
    }
}
```

{% endcode %}
