iosswiftobjective-cios-frameworksgoogle-ad-manager

Module not found inside 'Framework' -Swift.h file


I have created this sample Swift framework which has GoogleMobileAds in it integrated through Cocoapods. I have a class NativeAdView which inherits from GADNativeAdView.

import GoogleMobileAds

@objc public class NativeAdView: GADNativeAdView {
    
}

I also have a sample target which is an Objective-C app. I import the framework header as below in my AppDelegate.m.

#import <SampleFramework/SampleFramework-Swift.h>

Now when I run the Objective-C target it throws an error inside the file SampleFramework-Swift.h

@import GoogleMobileAds; -------- Module 'GoogleMobileAds' not found.

However, if I change the parent class of NativeAdView to anything other than a Google ad class the error goes away and the build compiles and runs successfully.

Also @import GoogleMobileAds; line goes away from SampleFramework-Swift.h file

I can't figure out what is happening.

Github link - https://github.com/rishabdutta/FrameworkSample


Solution

  • First, update bundle identifiers of your targets SampleFramework, SampleSwift, SampleObjc to be different.

    Try updating Podfile as follows. Cocoapods works this way that in your setup you have to specify 'Google-Mobile-Ads-SDK' for every target which uses it, but to avoid copying that line, you can use abstract_target so the dependency is automatically added to all targets inside:

    use_frameworks!
    
    abstract_target 'Common' do
      pod 'Google-Mobile-Ads-SDK'
    
      target 'SampleFramework' do
      end
    
      target 'SampleObjc' do
      end
    
      target 'SampleSwift' do
      end
    end
    

    But this way you will face a runtime warning "Class APMAdExposureReporter is implemented in both ...", but the app will work, the warning doesn't cause real problems - it's discussed here, but personally I don't know the best practice how to deal with it.