Skip to content

Instantly share code, notes, and snippets.

@ydn
Last active August 29, 2015 14:22
Show Gist options
  • Save ydn/67f67fe60d51396cda93 to your computer and use it in GitHub Desktop.
Save ydn/67f67fe60d51396cda93 to your computer and use it in GitHub Desktop.
Flurry iOS Native Ad view composition
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: ();
}
}
}
@dbgrandi
Copy link

It's hard to tell from the existing Pod documentation if nativeAd.space and nativeAd.assetList are guaranteed to be non-nil. You may want to add a guard before using them.

@dbgrandi
Copy link

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);
                }
            }
    }
}

@DrkSephy
Copy link

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