Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Native Ad to Objective C #5

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added iOS/.DS_Store
Binary file not shown.
24 changes: 13 additions & 11 deletions iOS/Objective C/IronSourceDemoApp/LevelPlayNativeAdView.xib
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="NativeAdView"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" id="itB-f1-pgg" customClass="NativeAdView">
<rect key="frame" x="0.0" y="0.0" width="300" height="250"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES" flexibleMaxY="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" ambiguous="YES" insetsLayoutMarginsFromSafeArea="NO" axis="vertical" spacing="4" translatesAutoresizingMaskIntoConstraints="NO" id="7Jf-gS-kFJ">
<rect key="frame" x="8" y="8" width="284" height="248"/>
<rect key="frame" x="8" y="-6" width="284" height="248"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" alignment="top" spacing="5" translatesAutoresizingMaskIntoConstraints="NO" id="FzH-MW-uJc" userLabel="Top Stack View">
<rect key="frame" x="0.0" y="0.0" width="284" height="40"/>
Expand All @@ -35,20 +35,20 @@
<rect key="frame" x="0.0" y="0.0" width="239" height="17"/>
<accessibility key="accessibilityConfiguration" identifier="AdTitle"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="14"/>
<nil key="textColor"/>
<color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="2" baselineAdjustment="alignBaselines" minimumFontSize="12" adjustsLetterSpacingToFitWidth="YES" translatesAutoresizingMaskIntoConstraints="NO" id="lPh-Kg-YxX">
<rect key="frame" x="0.0" y="17" width="239" height="17"/>
<accessibility key="accessibilityConfiguration" identifier="AdAdvertiser"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="14"/>
<nil key="textColor"/>
<color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</stackView>
</subviews>
<color key="backgroundColor" systemColor="linkColor"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</stackView>
<stackView opaque="NO" contentMode="scaleToFill" ambiguous="YES" axis="vertical" spacing="5" translatesAutoresizingMaskIntoConstraints="NO" id="RmB-ZX-3EG" userLabel="Bottom Stack View">
<rect key="frame" x="0.0" y="44" width="284" height="204"/>
Expand All @@ -57,7 +57,7 @@
<rect key="frame" x="0.0" y="0.0" width="284" height="14.333333333333334"/>
<accessibility key="accessibilityConfiguration" identifier="adBody"/>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<nil key="textColor"/>
<color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<view contentMode="scaleToFill" ambiguous="YES" translatesAutoresizingMaskIntoConstraints="NO" id="nKi-rK-Qb6" userLabel="Media View" customClass="LevelPlayMediaView">
LugaWUnity marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -85,6 +85,7 @@
<button opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" contentHorizontalAlignment="right" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="dGK-DS-IwI">
<rect key="frame" x="0.0" y="0.0" width="284" height="28.333333333333332"/>
<accessibility key="accessibilityConfiguration" identifier="adCallToAction"/>
<color key="tintColor" systemColor="systemBlueColor"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="plain" title="Button">
<fontDescription key="titleFontDescription" type="system" pointSize="12"/>
Expand All @@ -100,30 +101,31 @@
</subviews>
</stackView>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="7Jf-gS-kFJ" firstAttribute="leading" secondItem="itB-f1-pgg" secondAttribute="leading" constant="8" id="6rw-wg-tPc"/>
<constraint firstItem="7Jf-gS-kFJ" firstAttribute="bottom" secondItem="itB-f1-pgg" secondAttribute="bottom" constant="-8" id="iLQ-zX-2BG"/>
<constraint firstItem="7Jf-gS-kFJ" firstAttribute="trailing" secondItem="itB-f1-pgg" secondAttribute="trailing" constant="-8" id="mOG-ly-1rQ"/>
<constraint firstItem="nKi-rK-Qb6" firstAttribute="centerX" secondItem="itB-f1-pgg" secondAttribute="centerX" id="wsE-rI-Ide"/>
</constraints>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<connections>
<outlet property="adAdvertiserView" destination="lPh-Kg-YxX" id="nmV-M2-SRt"/>
<outlet property="adAppIcon" destination="AQf-Az-3C5" id="uA3-Zh-ytL"/>
<outlet property="adBodyView" destination="PeD-4v-BX7" id="ddY-QB-evY"/>
<outlet property="adCallToActionView" destination="dGK-DS-IwI" id="BXJ-NZ-5qb"/>
<outlet property="adMediaView" destination="nKi-rK-Qb6" id="77A-fa-gbs"/>
<outlet property="adMediaView" destination="nKi-rK-Qb6" id="jnb-FF-Aoi"/>
<outlet property="adTitleView" destination="afS-YP-sLG" id="dRf-vk-t6d"/>
</connections>
<point key="canvasLocation" x="-681" y="107"/>
</view>
</objects>
<resources>
<systemColor name="linkColor">
<color red="0.0" green="0.47843137254901963" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
<systemColor name="systemBlueColor">
<color red="0.0" green="0.47843137254901963" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
</resources>
</document>
2 changes: 1 addition & 1 deletion iOS/Objective C/IronSourceDemoApp/NativeAdView.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@property (nonatomic, weak) id<NativeAdViewDelegate> delegate;

@property (nonatomic, strong) LevelPlayNativeAd *nativeAd;
@property (nonatomic, strong) LevelPlayNativeAd *levelPlayNativeAd;
- (void)loadNativeAdLayout: (LevelPlayNativeAd *)nativeAd;

@property (nonatomic, strong) UILabel *adBadge;
Expand Down
90 changes: 22 additions & 68 deletions iOS/Objective C/IronSourceDemoApp/NativeAdView.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,12 @@

@implementation NativeAdView

@dynamic adAppIcon;
@dynamic adTitleView;
@dynamic adAdvertiserView;
@dynamic adBodyView;
@dynamic adMediaView;
@dynamic adCallToActionView;

- (instancetype)init {
UINib *nib = [UINib nibWithNibName:@"LevelPlayNativeAdView" bundle:[NSBundle mainBundle]];
NSArray *nibContents = [nib instantiateWithOwner:nil options:nil];
NativeAdView *nativeAdView = (NativeAdView *)[nibContents firstObject];

if (!nativeAdView) {
nativeAdView = [[NativeAdView alloc] init];
}

self = nativeAdView;
[self setNativeAdViewConstraints];
[self setupTopView];
self.translatesAutoresizingMaskIntoConstraints = NO;

Expand Down Expand Up @@ -65,10 +53,14 @@ - (void)setupTopView {
_topView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:_topView];

// Set up constraints
[self setTopViewConstraints];
}

- (void)setTopViewConstraints {
// Get the current top view
UIView *firstSubview = self.subviews[0];

// Set up constraints
[NSLayoutConstraint activateConstraints:@[
maozel marked this conversation as resolved.
Show resolved Hide resolved
[_topView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:8],
[_topView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-8],
Expand Down Expand Up @@ -96,69 +88,31 @@ - (void)setClearBackgroundRecursively:(UIView *)parentView {
}
}

// Predefine the colors and constraints of the native ad's properties.
// Replace with your desired colors and look.
- (void)setNativeAdViewConstraints {
[self setClearBackgroundRecursively:self];
self.translatesAutoresizingMaskIntoConstraints = NO;

self.adTitleView.textColor = UIColor.blackColor;
self.adAdvertiserView.textColor = UIColor.blackColor;
self.adBodyView.textColor = UIColor.blackColor;
[self.adCallToActionView setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

UIView *mediaView = self.adMediaView;
if (mediaView) {
for (UIView *subview in mediaView.subviews) {
[subview.centerXAnchor constraintEqualToAnchor:self.centerXAnchor].active = YES;
}
}
}

// This was triggered after a successful load of a native ad.
- (void)loadNativeAdLayout:(LevelPlayNativeAd *)nativeAd {
_nativeAd = nativeAd;
_levelPlayNativeAd = nativeAd;

if (_nativeAd.icon.image) {
self.adAppIcon.image = _nativeAd.icon.image;
if (_levelPlayNativeAd.icon.image) {
self.adAppIcon.image = _levelPlayNativeAd.icon.image;
} else {
[self.adAppIcon removeFromSuperview];
}
self.adTitleView.text = _nativeAd.title;
self.adAdvertiserView.text = _nativeAd.advertiser;
self.adBodyView.text = _nativeAd.body;
[self.adCallToActionView setTitle:_nativeAd.callToAction forState:UIControlStateNormal];
self.adCallToActionView.userInteractionEnabled = NO;

// The below part is copied from ISNativeAdView's (void)setNativeAd method.
// For some unknow reason these parts were not triggered.
// adMediaView and networkNativeAdView was affected the most.
// Should have the below single line instead:
// [self setNativeAd:nativeAd];

ISAdapterNativeAdViewBinder *binder = _nativeAd.nativeAdViewBinder;
if (binder) {
[binder setIconView:self.adAppIcon];
[binder setTitleView:self.adTitleView];
[binder setAdvertiserView:self.adAdvertiserView];
[binder setBodyView:self.adBodyView];
[binder setMediaView:self.adMediaView];
[binder setCallToActionView:self.adCallToActionView];

// This last part of the binding is adding the container to the NetworkNativeAdView
[binder setNativeAdView:self];

// Then we add the NetworkNativeAdView as a child to this NativeAdLayout
binder.networkNativeAdView.translatesAutoresizingMaskIntoConstraints = false;
[self addSubview:binder.networkNativeAdView];
[NSLayoutConstraint activateConstraints:@[
[binder.networkNativeAdView.topAnchor constraintEqualToAnchor:self.topAnchor],
[binder.networkNativeAdView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
[binder.networkNativeAdView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
[binder.networkNativeAdView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor]
]];
if (_levelPlayNativeAd.title) {
self.adTitleView.text = _levelPlayNativeAd.title;
}
if (_levelPlayNativeAd.advertiser) {
LugaWUnity marked this conversation as resolved.
Show resolved Hide resolved
self.adAdvertiserView.text = _levelPlayNativeAd.advertiser;
}
if (_levelPlayNativeAd.body) {
LugaWUnity marked this conversation as resolved.
Show resolved Hide resolved
self.adBodyView.text = _levelPlayNativeAd.body;
}
if (_levelPlayNativeAd.callToAction) {
[self.adCallToActionView setTitle:_levelPlayNativeAd.callToAction forState:UIControlStateNormal];
self.adCallToActionView.userInteractionEnabled = NO;
}

[self setNativeAd:_levelPlayNativeAd];

// Bring the adBadge and the delete button to the front of the layer
[self bringSubviewToFront:_topView];
}
Expand Down