Search code examples
swiftswift2vungle-ads

Vungle playAd API not working in Swift


I'm trying to use the playAd API of the VungleSDK Advertisement framework, and the following two functions are depecrated. It says to use the next two with an error parameter:

- (void)playAd:(UIViewController *)viewController __attribute__((deprecated));
- (void)playAd:(UIViewController *)viewController withOptions:(id)options __attribute__((deprecated));


- (BOOL)playAd:(UIViewController *)viewController error:(NSError **)error;
- (BOOL)playAd:(UIViewController *)viewController withOptions:(id)options error:(NSError **)error;

In Swift, how would I call such API?

I tried multiple things, including the following:

let sdk = VungleSDK.sharedSDK()
var error : NSError?

do {
   try sdk.playAd(viewController, error:err)
} catch let err as NSError {
   // error handling
}

I also tried passing &err since it's expecting a pointer to a NSError pointer (and also tried to define error as a NSErrorPointer. They all fail compiling in the new Swift 2.2.

What am I missing??


Solution

  • It seems that this works (although it's not documented anywhere as far as I can tell):

    try! sdk.playAd(viewController, error: ())
    

    Here is the same working solution, with error handling:

    do {
      try sdk.playAd(viewController, error: ())
    } catch let error as NSError {
      print(error)
    }