Mopub usage with TwitterKit for iOS

mopub
ios
xcode
swift
twitterkit

#1

Hi,

I am desperately trying to find some documentation regarding the usage of Mopub with TwitterKit in iOS. I have the latest TwitterKit (3.2.1) and the current documentation is less than useful-

I can’t get it to work with this example from the Twitter Dev documentation. I am on Xcode 9 and iOS 11.

// Swift
import TwitterKit

class UserTimelineViewController: TWTRTimelineViewController {
  override func viewDidLoad() {
      super.viewDidLoad()

      let client = TWTRAPIClient()
      let dataSource = TWTRUserTimelineDataSource(screenName: "twitterdev", apiClient: client)
      let adConfig = TWTRMoPubAdConfiguration(adUnitID: "myAdUnitID", keywords: nil)
      self.init(dataSource: dataSource, adConfiguration: adConfig)
  }
}

I also tried the method explained here by @val but it the app keeps crashing at

required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

Is there a proper example of how to go about using TWTRMoPubAdConfiguration with TWTRTimelineViewController? Also, is TWTRMoPubAdConfiguration only usable with TWTRTimelineViewController? If I have my own custom TableView where I load tweets using TWTRTweetTableViewCell into the tableView, how would one go about initializing the dataSource with TWTRMoPubAdConfiguration in that case? or are there limitations to be able to do that currently?

Any help will be greatly appreciated.


#2

Hey @itsrealannjawn

You shouldn’t be calling self.init(..) in viewDidLoad. Your view controller is already initialized at that point. Try wrapping that code in an init() call:

    convenience init() {
        let client = TWTRAPIClient.withCurrentUser()
        let dataSource = TWTRUserTimelineDataSource(screenName: "twitterdev", apiClient: client)
        let adConfiguration = TWTRMoPubAdConfiguration(adUnitID: "myadUnitID", keywords: nil)
        self.init(dataSource: dataSource, adConfiguration: adConfiguration)
    }

Let me know if that helps!


#3

Thanks, that helps, although I had to take a different approach by using Mopub separately since I wasn’t able to figure out a way to make this work with a UITableView since I am not using TWTRTimelineViewController. MoPub documentation is a little sparse and outdated but the Cannonball iOS project from GitHub was very helpful.