bhamm@mac.com

iOS App SDK 17 + Swift App Delegate (walkthrough)

Discussion created by bhamm@mac.com on Jul 2, 2018

What You'll Learn

How to build a simple FIAS project in Xcode with a Swift App Delegate.

How to trigger a script from the App Delegate.

 

What This Post Is /Not/

A tutorial on Xcode

A tutorial on Swift

A tutorial on Terminal

 

Requirements

iOS App SDK 17+  // 17.0.2 as of this writing

Xcode 9+         // 9.4.1 as of this writing

 

Here's What We're Going To Do

Navigate to FIAS directory and create a project

Add a SwiftAppDel class

Edit Bridging-Header.h

Finish the SwiftAppDel class

Build

Navigate to DerivedData, MyProject.app

Get an object reference for SwiftAppDel using `otool`

Assign object reference in `configFile.txt`

Build and run app

Profit! (lol)

 

 

 

Ok, Let's Make A Project!

In Terminal, cd to your FIAS directory, wherever that is. Mine lives in `/Applications`, so:

> cd /Applications/iOSAppSDKPackage_17.0.2  // use tab auto-complete: "cd /App[tab] iOS[tab]"

Create a project. Don't forget the leading dot.

> ./makeprojdir MyDirectory MyProject com.domain.MyProject  // mind your spaces!

After FIAS returns a prompt, you can open the project with:

> open MyDirectory/MyProject.xcodeproj  // alternately, you can open the project from the Finder

 

 

 

Xcode: Create SwiftAppDel File

In the Project Navigator (left sidebar), right-click on the Custom Application Resources folder and choose `New File`. This will be our Swift App Delegate class. Choose `Swift File`, name it `SwiftAppDel`, and click Create. Xcode will ask you about adding a bridging header. Choose `Create Bridging Header`.

 

This will drop you off in `SwiftAppDel.swift`. We can't do anything in here yet, we'll come back in a minute.

 

 

 

Edit Briding-Header.h

Open `MyProject-Bridging-Header.h` from the Project Navigator and add these 2 import statements:

#import "UIKit/UIKit.h"  // required to bridge NSString, NSDictionary, and bool classes in FMX_Exports.h

#import "FMX_Exports.h"  // fias class header for queueing/firing scripts, no idea why FMI named this 'Exports'

Build the project (Command-B) and watch for errors. You shouldn't have any.

 

 

 

SwiftAppDel Class

Open `SwiftAppDel.swift` from the Project Navigator and build it out like this:

import Foundation  // already present

 

 

// copy-paste or type the following:

class SwiftAppDel: UIResponder, UIApplicationDelegate {

   

    var window: UIWindow?

 

    // did finish launching

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

       

        print("\n\n*** swift app delegate! \n\n")  // disco!

 

        return true

    }

 

    // return foreground active

    func completedReturnToForegroundActive() {

 

        print("\n\n*** return foreground active! \n\n")

 

        // firing a script requires the 'fmurlscript' extended privilege in your .fmp12 file

        FMX_Queue_Script("PlaceHolder.fmp12", "MyScript", FMX_ScriptControl(kFMXT_Resume), nil, nil) 

    }

}

* edited 5:45 AM CST

 

 

 

Build Project (Command-B)

Take care of any errors or typos before proceeding.

 

 

 

Terminal: Navigate To DerivedData/...

DerivedData is where Xcode stores your build data. To get FIAS to 'see' our Swift App Delegate, we need to use a command line tool called `otool`. First, cd to DerivedData/ all-the-way-to /MyProject.app (which is a directory):

> cd ~/Library/Developer/Xcode/DerivedData/MyProject-gznmjbw.../Build/Products/Release-iphoneos/MyProject.app/

If you're familiar with Terminal, this can all be done rather quickly using [tab] auto-complete.

 

 

 

Get Object Reference To SwiftAppDel

When you've successfully landed in `MyProject.app`, do this:

> otool -o MyProject

This outputs metadata for the `MyProject` Unix executable inside of `MyProject.app`. Check the output for a reference like `_TtC4MyProject10SwiftAppDel`. Copy this value to the clipboard. Include the leading underscore.

 

 

 

Xcode: Update FIAS Config File

Return to Xcode, open `configFile.txt` from the Project Navigator, and update these settings:

launchSolution            = PlaceHolder.fmp12 (or your solution file)

solutionCopyOption        = 1

applicationDelegateClass  = _TtC4MyProject10SwiftAppDel

 

 

Run (Command-R)

Click the 'Play' button in Xcode (or Command-R) to run the project. Shortly after your app launches you should see a "swift app delegate!" message in the console/debug area. High-five yourself or the person nearest you.

 

Now press your device Home button and re-launch the app (from the device). This time, completedReturnToForegroundActive() should fire and post a "return foreground active!" message to the console. If you go back and include a 'MyScript' in your solution file (and enable fmurlscript), that will fire as well.

 

 

 

Further Reading

There are lots of app lifecycle (delegate) methods. You can read more about them here:

UIApplicationDelegate - UIKit | Apple Developer Documentation

 

 

 

Happy Coding!

Outcomes