Search code examples
objective-cswiftmobfox

ObjectiveC framework in Swift app causing errors (MobFox ad network framework)


I'm trying to implement MobFox ad network into my Swift app. The framework is built in ObjC which I'm not familiar with.

I have added my bridging header.

Im getting 2 errors which I cannot figure out. This is the code that is producing the errors:

class ViewController: UIViewController, MobFoxNativeAdDelegate {

...

    func nativeAdFailedToLoadWithError(error: NSError!) {
    }

    func nativeAdWasClicked() {
    }

    func nativeAdWasShown() {
    }

    func publisherIdForMobFoxNativeAdController(controller: MobFoxNativeAdController!) -> String! {
    return "PUBLISHER_ID_HERE"
    }

    func nativeAdDidLoad(ad: MobFoxNativeAd!) {
    }
}

These are the issues:

class ViewController: UIViewController, MobFoxNativeAdDelegate {

The above code causes an error: Type 'ViewController' cannot conform to protocol 'MobFoxNativeAdDelegate' because it has requirements that cannot be satisfied

I'm sure there are no other methods that I need to implement - unless Im mistaken.

The other issue is:

func nativeAdDidLoad(ad: MobFoxNativeAd!) {
}

On this line I get the following error: Use of undeclared type 'MobFoxNativeAd'

The strange thing is XCode doesn't suggest this method when typing, so it seems to be invalid. For example if I type native, xcode suggests nativeAdFailedToLoadWithError, nativeAdWasClicked and nativeAdWasShown - but it does not suggest nativeAdDidLoad.

However, it is a required method.

Looking at the ObjC framework this is what it contains:

@class MobFoxNativeAdController;
@class MobFoxNativeAd;

@protocol MobFoxNativeAdDelegate <NSObject>

- (NSString *)publisherIdForMobFoxNativeAdController:(MobFoxNativeAdController *)controller;

- (void) nativeAdDidLoad:(MobFoxNativeAd *)ad;

- (void) nativeAdFailedToLoadWithError:(NSError *)error;

- (void) nativeAdWasShown;

- (void) nativeAdWasClicked;

- (UIViewController*) viewControllerForNativeAds;

@end

@interface MobFoxNativeAdController : NSObject

@property (strong, nonatomic) NSString *requestURL;
@property (nonatomic, assign) IBOutlet __unsafe_unretained id <MobFoxNativeAdDelegate> delegate;
@property (nonatomic, assign) BOOL locationAwareAdverts;
@property (nonatomic, assign) NSInteger userAge;
@property (nonatomic, strong) NSString* userGender;
@property (nonatomic, strong) NSArray* keywords;

@property (nonatomic, strong) NSArray* adTypes;

- (void)setLocationWithLatitude:(CGFloat)latitude longitude:(CGFloat)longitude;

- (UIView*)getNativeAdViewForResponse:(MobFoxNativeAd*)response xibName:(NSString*)name;

- (void)requestAd;

@end

Solution

  • I have figured it out. It was caused by a bug in the framework. The file MobFox.h needed to import MobFoxNativeAd.h