Pascal and Mapbox

Part 3: macOS and Xcode


Contents

Introduction
Setting up
Installing
Testing a simple Xcode project
BoxCast ObjP: an example app
Misc. notes


Introduction

When developing with Pascal in Xcode, you use the same Objective C classes that you would in a Swift or Objective C app. You do this via code that you write in Free Pascal's Objective Pascal dialect.

With Objective Pascal, you work directly with the Mapbox classes; there is no wrapper for the Mapbox framework, just an interface unit that declares the Mapbox classes.


Setting up

  1. Follow the steps given in "Setting up" in Part 2.

  2. Download ProjectXC from here. ProjectXC contains a template for creating a Pascal project to work with Mapbox in Xcode.

    Double-click the .zip and move the resulting folder anywhere you like.

  3. Optionally, add Xcode support for Pascal syntax highlighting.


Installing

  1. Run the compile-appkit.sh script in the MacOS_10_10-master folder.

    This script will compile common macOS interface units (AppKit, CoreLocation, etc.) to /Developer/ObjectivePascal/units/x86_64-darwin/macOS/NoCocoaAll.

  2. Run the compile-mapbox.sh script in the mapbox-macos-pascal folder.

    This script will compile the Mapbox interface unit to /Developer/ObjectivePascal/units/x86_64-darwin/Mapbox/NoCocoaAll.

  3. Compile ProjectXC's makeproj utility.

Testing a simple Xcode project

  1. Create a new Xcode project from the ProjectXC osx-mapbox template like this (substitute your own project path):

    ./makeproj osx-mapbox ~/MyXcodeProjects/testmb1
    

  2. Open testmb1.xcodeproj in Xcode.

  3. Copy Mapbox.framework from the mapbox-macos-sdk folder to your project's top-level folder (don't put it in the source subfolder or in the .xcodeproj subfolder).

  4. Paste your Mapbox access token into the value for the MGLMapboxAccessToken key in the Info.plist file.

  5. Choose Product | Run to compile and run your app. You should see a map of the world.

Now enter some more interesting default settings for your map.

Click on Main.storyboard in the Project Navigator, then select the Map View and click the Attributes inspector in Utilities. Enter these values for the Map View:

Now run the app again to zoom in on the San Juan Mountains of southwest Colorado in the U.S.


BoxCast ObjP: an example app

The source for BoxCast ObjP, a more extensive example, is included in the mapbox-macos-pascal folder. BoxCast ObjP is an Objective Pascal version of the original Boxcast JavaScript app given here.

BoxCast ObjP uses the NDFD framework for retrieving weather forecast data to display on the map. BoxCastObjP also demonstrates how to do the following:

If you want to compile and run BoxCast ObjP yourself, here are the steps:

  1. Copy Mapbox.framework from the mapbox-macos-sdk folder to the top-level BoxCast_ObjP folder.

  2. Create NDFD.framework and copy it to the BoxCast_ObjP folder (alongside Mapbox.framework). Instructions for creating NDFD.framework are here.

  3. In the ndfd folder, run this script to compile the NdfdLib interface unit to the "standard" location under /Developer/ObjectivePascal/units:

    ./compile-ndfdlib-macos.sh Framework
    

  4. Open BoxCast_ObjP.xcodeproj in Xcode.

  5. Paste your Mapbox access token into the value for the MGLMapboxAccessToken key in the Info.plist file.

  6. Choose Product | Run to compile and run the app.


Misc. notes

  1. Note the things that Cocoa provides without any effort on your part. For example, it saves the app window's position and size. And Mapbox saves its map location and zoom level automatically. You don't have to do these things yourself.

    (With the standard Cocoa menus, you also get an About box without any additional work. You can easily customize it by setting the credits in the app delegate's displayAboutBox method provided by the template.)

    Where does Cocoa save this information? Well, under ~/Library of course, using the app's bundle ID (eg, com.mydomain.myapp). That's one reason why it's important to use a unique bundle ID for each app.

    Here are the ~/Library locations that your app uses:

    From the user's perspective, uninstalling an app on macOS is simple: just drag the app to the trash. But the above files stay behind. That's fine if the user installs the app again (for example, a newer version), because it will use the previous installation's preferences and settings. But to completely uninstall an app you really need to delete the above files as well. You can also delete them to reset an app, for example during testing.

  2. By default, Xcode compiles all intermediate files and the .app bundle to a folder for the app under ~/Library/Developer/Xcode/DerivedData. You can change this for your app (under File | Project Settings) or for any new app (Xcode | Preferences, on the Locations tab).

  3. Note that there are two ways to set your app's Mapbox access token. Setting it in the app's Info.plist file, as given in the examples above, is the recommended way. However, since any user can look at your app's Info.plist file (it's just XML), you might want to set the access token in code.
On to Part 4 ...


Copyright 2018 by Phil Hess.

macpgmr (at) icloud (dot) com

First posted March 16, 2018.