Search Unity

Native iOS with Swift

Discussion in 'Unity Ads & User Acquisition' started by shopguy, Sep 8, 2015.

  1. shopguy

    shopguy

    Joined:
    Jan 12, 2013
    Posts:
    282
    Trying to get Unity ads to work in a native iOS app (app not using Unity game engine) using Swift. I added a "bridge header" which seems to work, so I can use the UnityAds class now. I defined my view control like so:

    Code (CSharp):
    1. class ViewController: UIViewController, UnityAdsDelegate
    I added these 3 lines in the viewDidLoad:

    Code (CSharp):
    1.         UnityAds.sharedInstance().setTestMode(true)
    2.         UnityAds.sharedInstance().startWithGameId("XXXXXX", andViewController: self)
    3.         UnityAds.sharedInstance().delegate = self
    4.  
    I added this delegate method:

    Code (CSharp):
    1.    func unityAdsVideoCompleted(rewardItemKey : String, skipped : Bool){
    2.     }
    I added this code to display the video ad:

    Code (CSharp):
    1.                 if(UnityAds.sharedInstance().canShow()){
    2.                     UnityAds.sharedInstance().show()
    3.                     return
    4.                 }
    5.  
    It works... the video plays. However, as soon as the video ends, my app crashes with EX_BAD_ACCESS error.

    I'm thinking this is something to do with my delegate method. Unity trying to notify my delegate, but calling in to an invalid function or something like that.

    If anyone isn't sure what my issue is, are there any Swift examples available, so I can see what works, and what I might be doing wrong?

    So far I hate Swift, even more than the awfulness that is Objective-C, but I wanted to do at least one complete project before giving up on it :)
     
  2. Artem Yarulin

    Artem Yarulin

    Joined:
    Sep 8, 2015
    Posts:
    1
    Hi,

    You can check this example project that I've created: https://github.com/artemyarulin/unity-ads-swift . Source code looks about the same as yours and it works just fine.

    But yeah, it looks like you have some memory problem - maybe your view controller got cleaned up and delegate cannot be called then?
     
  3. shopguy

    shopguy

    Joined:
    Jan 12, 2013
    Posts:
    282
    Thanks. It does appear to be something like you mentioned. I think some issue with the viewcontroller trying to be accessed while it is hidden. Odd because iOS doesn't destroy view controllers anymore, unless super low on memory, and that isn't the case here. The delegate works up until the point the video is displayed though (if I add all of the optional events, the ones that fire before the video is displayed do get called).

    At any rate, I'm going to look at Apple's iAd SDK instead. The Unity SDK isn't documented very well, so I'm just having too many issues finding answers to basic questions. For example, while it is fetching ads "in the background", my FPS drops really low, so I'd rather have that done during some "loading.." screen, but the docs really don't give any info about how to accomplish that type of thing. They also don't say what happens if anything fails, other than onFetchFailed is invoked.. but nothing about retry or all of that important stuff.