crashoverride777 / swifty-ads

A Swift library to display Google AdMob ads. GDPR, COPPA and App Tracking Transparency compliant.
MIT License
129 stars 40 forks source link

Native ad type #32

Closed javaddeveloper closed 3 years ago

javaddeveloper commented 4 years ago

Hello my friend I use this framework in my apps , this framework help to implement ads very easy and efficient. thank you

I have a question about native ads type

Could you add native ads in this framework ?

crashoverride777 commented 4 years ago

Hey,

Thanks for you question, what exactly do you mean by native ads?

javaddeveloper commented 4 years ago

Google admob has 4 types of ads format that swifty ads has 3 types of them

Banner Interstitial Rewarded Ads and Native Ads

Native Ads implement between app content this is an example https://codelabs.developers.google.com/codelabs/admob-native-advanced-feed-ios/#0

Thank you for response

crashoverride777 commented 4 years ago

Hey,

Thank you for the links, I will have a look at this very soon, currently on holiday.

Thanks again

javaddeveloper commented 4 years ago

Hello my friend Could you add native ads in swiftyAds framework? Today i found a introduction that may help you to implement

https://www.youtube.com/watch?v=AfCvQ4p_1y8

Have good day

atalayasa commented 3 years ago
import GoogleMobileAds

protocol NativeAdShowable: AnyObject {
    func show(from viewController: UIViewController,
              numberOfAds: Int,
              onReceive: ((GADUnifiedNativeAd) -> Void)?,
              onFinish: (() -> Void)?,
              onError: ((Error) -> Void)?)
}

final class NativeAdManager: NSObject {
    private let adUnitId: String
    private let request: () -> GADRequest
    private var onReceive: ((GADUnifiedNativeAd) -> Void)?
    private var onFinish: (() -> Void)?
    private var onError: ((Error) -> Void)?
    private var adLoader: GADAdLoader?

    // MARK: - Init

    init(adUnitId: String, request: @escaping () -> GADRequest) {
        self.adUnitId = adUnitId
        self.request = request
        super.init()
    }
}

extension NativeAdManager: NativeAdShowable {
    func show(from viewController: UIViewController, numberOfAds: Int, onReceive: ((GADUnifiedNativeAd) -> Void)?, onFinish: (() -> Void)?, onError: ((Error) -> Void)?) {
        self.onReceive = onReceive
        self.onFinish = onFinish
        self.onError = onError

        let options = GADMultipleAdsAdLoaderOptions()
        options.numberOfAds = numberOfAds
        adLoader = GADAdLoader(adUnitID: adUnitId,
                                   rootViewController: viewController,
                                   adTypes: [.unifiedNative],
                                   options: [options])
        adLoader?.delegate = self
        adLoader?.load(request())
    }
}

extension NativeAdManager: GADUnifiedNativeAdLoaderDelegate {
    func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
        Log.i("Received native ad: \(nativeAd)")
        onReceive?(nativeAd)
    }

    func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
        onFinish?()
    }

    func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: GADRequestError) {
        Log.s("\(adLoader) failed with error: \(error.localizedDescription)")
        onError?(error)
    }
}

You need to add necessary things to protocol and then call following code inside AdManager

private var nativeAd: NativeAdShowable?
       nativeAd = NativeAdManager(adUnitId: configuration.nativeAdUnitId, request: { [unowned self] () -> GADRequest in
            self.requestBuilder.build()
        })

Also

    func showNativeAd(from viewController: UIViewController, numberOfAds: Int, onReceive: ((GADUnifiedNativeAd) -> Void)?, onFinish: (() -> Void)?, onError: ((Error) -> Void)?) {
        nativeAd?.show(from: viewController,
                       numberOfAds: numberOfAds,
                       onReceive: onReceive,
                       onFinish: onFinish,
                       onError: onError)
    }

Lastly you need to call AdManager.shared.nativeAd method. Here I've attached it for native ads without breaking actual code. You need

javaddeveloper commented 3 years ago

Thank you @atalayasa this code can help to improve swiftyAds

@crashoverride777 Hello Dominik Could you analyze this code and merge it with your framework ?

crashoverride777 commented 3 years ago

Hey,

Started native ads yesterday, you can follow the progress on the feature branch. Will finish tonight and update the sample app as well. Still reading some stuff about the preloading for native ads.

Kind regards

crashoverride777 commented 3 years ago

Hey,

I have finished the native ad implementation and updated the demo project as well.

However I am still having problems with the cocoa pod update. I am currently on holiday and only got my work laptop with me, and it has some security software on it that blocks me from using cocoa pods for my personal projects.

I won't be able to fix the cocoa pods until after New Years. So in the meantime you will have to manage it manually if you want to use the latest version with native ads. I have created release branches for the latest versions for your convenience.

Sorry for the continued delay with the pods. I will update the readme to make people more aware of this.

crashoverride777 commented 3 years ago

Hey

Managed to get the pods working again on my machine, I had to use a special VPN to get around the security software on my work laptop. I am unfortunately still stuck overseas due to corona. I should hopefully have the pods issues fixed within the next few days.

Thanks again for your patience

crashoverride777 commented 3 years ago

Hey

I am having trouble to get the pod to validate, the new arm64 slice is missing on AdMob for release

https://github.com/CocoaPods/CocoaPods/issues/10198

https://github.com/CocoaPods/CocoaPods/issues/10104

Having issues with the suggested fixes, will have to downgrade to xCode 11 if I cannot fix it the next few days.

It's super frustrating.

crashoverride777 commented 3 years ago

Hey,

version 11.2.0 is now live and on cocoa pods which includes Native Ads. Please check out the demo project for an example.

I will close this issue now, please feel free to create another one if needed. My apologies again for the delay.