Last active
August 29, 2015 14:22
-
-
Save ydn/67f67fe60d51396cda93 to your computer and use it in GitHub Desktop.
Flurry iOS Native Ad view composition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func adNativeDidFetchAd(nativeAd: FlurryAdNative!) { | |
NSLog("Native Ad for Space \(nativeAd.space) Received Ad with \(nativeAd.assetList.count) assets"); | |
for asset in nativeAd.assetList { | |
switch(asset.name) { | |
case "headline": | |
streamTitleLabel.text = asset.value; | |
case "summary": | |
streamDescriptionLabel.text = asset.value; | |
case "secImage": | |
if let url = NSURL(string: asset.value!!) { | |
if let imageData = NSData(contentsOfURL: url) { | |
streamImageView.image = UIImage(data: imageData); | |
} | |
} | |
case "source": | |
streamSourceLabel.text = asset.value; | |
case "secHqBrandingLogo": | |
if let url = NSURL(string: asset.value!!) { | |
if let imageData = NSData(contentsOfURL: url) { | |
streamSponsoredImageView.image = UIImage(data: imageData); | |
} | |
} | |
default: (); | |
} | |
} | |
} |
Use the native for-in syntax and switch statements to make this more concise and swifty. You can also lose some of the extra steps of creating optional NSURL
and NSData
instances.
e.g. (This was freehand, not guaranteed it compiles)
for asset in nativeAd.assetList {
switch (asset.name) {
case "headline":
streamTitleLabel.text = asset.value
case "summary":
streamDescriptionLabel.text = asset.value;
case "secImage":
if let url = NSURL(string: asset.value) {
if let imageData = NSData(contentsOfURL: url) {
streamImageView.image = UIImage(data: imageData);
}
}
case "source":
streamSourceLabel.text = asset.value;
case "secHqBrandingLogo":
if let url = NSURL(string: asset.value) {
if let imageData = NSData(contentsOfURL: url) {
streamSponsoredImageView.image = UIImage(data: imageData);
}
}
}
}
nativeAd.space
and nativeAd.assetList
will are guaranteed to be non-nil, as this method is a callback function which is called on successfully fetching an ad.
As for using the native for-in
syntax, I'll update the gist with your respective changes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's hard to tell from the existing Pod documentation if
nativeAd.space
andnativeAd.assetList
are guaranteed to be non-nil. You may want to add a guard before using them.