Monday, 26 March 2018

Swift 4 - Adding Admob Interstitial Ads to your iOS App

  1. Sign up for or sign in to Admob
  2. Add an App in Admob
  3. Add an Ad unit in Admob - Choose Interstitial. You can set ad type, frequency capping and eCPM floor under advanced settings
  4. You should now have an Ad Unit ID and App Id which will be used in displaying interstitial ads to the user
  5. In your code, create a file InterstitialAd.swift, it should have the following code:
      let adController = InterstitialAd()
      import GoogleMobileAds
      class InterstitialAd : NSObject, GADInterstitialDelegate {
        var testAdId = "ca-app-pub-3940256099942544/4411468910"
        var adId = "[YOUR AD UNIT ID GOES HERE]"
        var interstitial : GADInterstitial!
        func createAndLoadInterstitial() {
            interstitial = GADInterstitial(adUnitID: testAdId)
            interstitial.delegate = self
        // Load new interstitial on close so that adController 
        // is ready for the next time showAd is called
        func interstitialDidDismissScreen(_ ad: GADInterstitial) {
        func showAd(_ viewController: UIViewController) {
          if interstitial.isReady {
            interstitial.present(fromRootViewController: viewController)
  6. Add the following to your AppDelegate.swift file, it will configure the app for Admob ads and load the first interstitial so that it is ready to be displayed when you try to display the interstitial:
      import GoogleMobileAds
      class AppDelegate: UIResponder, UIApplicationDelegate {
        var appId = "[YOUR ADMOB APP ID GOES HERE]"
        func application(_ application: UIApplication, 
            didFinishLaunchingWithOptions launchOptions: 
            [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
          GADMobileAds.configure(withApplicationID: appId)
  7. From the view controller where you want to show your interstitial ad, make the following call:


For testing, you should use the testAdId as the adUnitID in InterstitialAd.swift, this is so that real ads are not displayed during testing. For release, switch to using adId.

... included for brevity in ApplicationDelegate.swift code above

Swift 4 - How to share messages and images

There are many benefits of enabling sharing within your application:
  • Free advertising for your app if users share from it on social media
  • Better user experience
  • Using recent iOS features to make the app feel more modern
The code for this is simple, when an event occurs that you would like to share on, you can call the following method:

  func share(_ message: String , shareImage: UIImage) {
    let share = [message, shareImage] as [Any]
    let activityViewController = UIActivityViewController(
      activityItems: share, applicationActivities: nil)
      .sourceView = self.view
    self.present(activityViewController, animated: true, 
      completion: nil)

Friday, 23 March 2018

Swift 4 - The app's Info.plist must contain an NSCameraUsageDescription key

As of iOS 10, some features require an entry in the Info.plist before access will be allowed. This is for privacy purposes.

You can read more about this setting on Apple's developer website.

To solve this issue add the following to your Info.plist:
 <string>[why you need camera access]</string>

Monday, 4 September 2017

C# - Automapper vs Factory Methods

When converting data from one model to another, there is often the thought over whether it would be beneficial to use automapper or whether factory methods should be used to explicitly convert between models. I'm going to explain what each of these is, and explain a couple of advantages and disadvantages of each.


Automapper is a NuGet package that allows you to "get rid of code that mapped one object to another". It is convention based, and if there are a large number of properties that are identical on 2 classes it can save a lot of developer time converting between objects. It also means that less code can be written for this conversion... Yay! However, there are a couple of disadvantages that come with convention based as opposed to explicit programming, and Automapper is no exception!

If you are using Automapper with default mappings and refactor the name of the source/target model, then you won't necessarily know until runtime because it won't cause a compile time error. The issue is that if there is a rename of the property, automapper will no longer be able to map correctly. Whereas if you were to explicitly code the conversion, there would be a compile time error so you can identify the error earlier. The risk of this failed mapping occurring can be mitigated by unit testing the mapping.

A similar issue to this is that there is a lack of static analysis. For example, if you find all references of a property it may look like nothing is referencing that property so you may be tempted to remove it. However, automapper could be mapping to this property without you even realising.

Overall, Automapper is a well tested library that can save you time writing uninspiring conversion code, but there are downsides and different scenarios may mean that it is beneficial or not.

Factory Methods

Factory methods allow you to explicitly convert between objects. Due to the explicit conversion, it is easy for developers working on the same project to see where values are coming from. This is because it is not hidden behind a library that the developer may or may not be familiar with. It is also easier to debug. If mapping is done incorrectly there will also be compile time errors which can help catch issues earlier.

Of course, there is a reason the Automapper NuGet package exists and this is because the manual conversion is tedious and takes time that could be spent on other things. The manual conversion also means that there is more code sitting in your project. Additionally, with anything manual there is the chance of human error and 2 properties that don't actually align with each other could potentially be mapped. Your unit tests should pick this up though.

When trying to choose between Automapper and Factory methods, think about your code and how well the objects will map automatically without configuration. Secondly, consider the important of static analysis and compile time errors in your code. This isn't a one-size fits all comparison, it's case by case as to which will be beneficial!

Friday, 1 September 2017

C# - Extension Methods

Extension methods allow you to extend functionality of a specific type. It is a special kind of static method that you can call as if it were an instance method on the object of the specified type. Extension methods are simple to define and you can define them as follows:

  using System;
  public class Program
    public static void Main()
      string message = "Hello World";

  // Extension methods must be contained within a non-generic static class
  public static class StringExtensions 
    // Extension methods must be static
    // Extended object must be prefixed with this
    public static string GetDatedMessage(this string message) {
      return String.Format("{0}: {1}", DateTime.Now, message);

Visual Studio - How to generate a NuGet package on build

NuGet packages are extremely useful way to add libraries/components to your code base. This is similar to the traditional way of referencing DLLs except that you get notifications when there are updates.

You may find that if your code lives in separate Git repositories that if someone wants to use a project from one Git repo in another Git repo that they would need to download both. However, NuGet packages can be stored in a common location e.g. the NuGet Package Gallery which is available to all, or on a centralised private NuGet server. If you want to use a private NuGet server, you will need to add a package source in the NuGet package manager. You can do this by clicking the little cog next to "Package source".

Now for actually generating the NuGet package you can do this automatically on build, by opening a right clicking on a project and selecting properties. Then navigate to the "Package" tab and check "Generate NuGet package on build"

Tuesday, 15 August 2017

iTunes Connect - Tracking Financials

Payments and Financial Reports

You can go here to review your confirmed sales for closed months. It will give you a breakdown by currency and an estimated conversion.

Sales and Trends

This will show you a graph of your sales. You can see which apps have sold, and roughly how many proceeds you have.

When will I get paid?

Payments for a lot of countries will be after you've earned $10 USD. Some countries require a minimum payment threshold of $150 USD. The payment will happen approximately 30-45 days after the financial reports for the month in which you reached the minimum payment threshold. The payment will be made to your designated bank. You can read more about it on Apple's "Getting Paid" Resources and Help page. If you meet these criteria and you haven't received a payment, you may want to check that your bank account details have been supplied under "Agreements, Tax, and Banking". You should also check that you have a valid contract with Apple as this will also cause payments to be held.