Implementing the MRAID Content Delegate Methods
Note
All HTML / MRAID delegate methods are optional.
MRAID Resize Event (Future)
MRAIDAdWillResizeToFrame:
This will be in invoked on MRAID Resize command, before the ad transformation.
- (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;
}
MRAID Resize Event (Happened)
MRAIDAdDidResizeToFrame:
This will be in invoked on MRAID Resize completion.
- (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.x]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:self.adView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
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;
}
}
MRAID Expand Event (Future)
MRAIDAdWillExpandToFrame:
This will be in invoked on MRAID Expand command, before the ad transformation.
- (void)IAMRAIDContentController:(IAMRAIDContentController * _Nullable)contentController MRAIDAdWillExpandToFrame:(CGRect)frame {
NSLog(@"MRAIDAdWillExpandToFrame");
self.isMRAIDResize = NO; // add this boolean flag to properties, in order to distinct whether 'collapse' event (when it will be invoked) is after 'resize' or after 'expand';
}
MRAID Expand Event (Happened)
MRAIDAdDidExpandToFrame:
This will be in invoked on MRAID Expand completion event.
- (void)IAMRAIDContentController:(IAMRAIDContentController * _Nullable)contentController MRAIDAdDidExpandToFrame:(CGRect)frame {
NSLog(@"MRAIDAdDidExpandToFrame");
}
MRAID Collapse Event (Future)
IAMRAIDContentControllerMRAIDAdWillCollapse:
This will be in invoked on MRAID Collapse command, before the ad transformation.
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];
}
MRAID Collapse Event (Happened)
IAMRAIDContentControllerMRAIDAdDidCollapse:
This will be in invoked on MRAID Collapse completion.
- (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]];
}
}