Swift Federal Data SDK Sample App
Important: This SDK was built using pre-release technology. This SDK is in beta until it has been successfully tested with a production release version of Apple's Swift language. Because of this, if Apple makes any changes to the underlying language, tools, and/or compiler, this SDK may break until fixes are made.
note: To use DOL's API you must register at https://devtools.dol.gov/developer and request an API key for each application that will access the API. Registration and API keys are free. A separate API key is not necessary for each application, but if you reuse your key across multiple apps, your apps' metrics will be combined and of little value.
API key requirements vary between federal agency's APIs
Preparing Your Project
Create New Project
note: If you're already very familiar with creating a single view app with a table view, you can skim or skip most of this section.In Xcode's File menu, choose New -> Project and then select iOS -> Application -> Single View Application. Click Next.
Give your project a Product Name. If Organization Name and Organization Identifier are blank, take care of that here. Choose Swift for the language and iPhone for the device. Click Next and save your project in the folder of your choice.
Set up your interfaceOpen your main storyboard and drag a Table View (not a Table View Controller) to the main window. Make sure it snaps into place with the top edge just below the battery indicator. With the table view selected, choose the icon near the bottom right of the storyboard and second from the right. Its tool tip is "Resolve Auto Layout Issues" and it looks like a TIE Fighter™. In the pop-up menu, choose Reset to Suggested Constraints.
Next, right-click (alternate: ctrl-click) on the table view and drag your cursor over to the View Controller in the View Controller Scene. Choose the dataSource outlet. Do the same for delegate.
Set up your View ControllerLet's first get some of the basic required code in place before we get to the SDK. Since this view controller contains a table view, we need to make some changes to the class declaration:
Below this line and before the viewDidLoad function, add the following:
In order for our view controller to conform to the UITableViewDataSource protocol, we need to add a few functions within the class:
Next, let's make the connection between our outlet and the table view itself. Near the top right of the window is an icon that looks like a tuxedo. That is the Assistant Editor. Choose that. Your window will split into two. On the left-hand side should be ViewController.swift. The window on the right will likely be empty. Near the top of that window are a backwards and forwards button and another button, likely text just to the right of those. Click that. Choose Manual -> project name -> project name -> Main.storyboard -> Main.storyboard (base). In your swift file, click on the empty circle to the left of @IBOutlet and drag it to the Table View in the View Controller Scene. Once you have done this, you can switch back to the Standard Editor (the icon to the left of the tuxedo).
Adding the SDK
Install the SDKUsing the link above, download the SDK to your computer. Copy GovDataRequest.swift and the SWXMLHash folder to your project by dragging it from a Finder window to your project's file structure in Xcode. Make sure Copy items if needed is checked.
Apply the SDK
Conform to the protocolFor the SDK to function properly, we need to ensure the view controller conforms to its protocol. Make one more change to the class declaration:
Now, add two more functions to the class:
note: The first two lines of code we just added inside didCompleteWithDictionary are based on the structure of the JSON response that DOL's V1 API returns. If you use this with another API, what you do here may be different.
Prepare To Send A RequestBack up in the viewDidLoad function, add the following:
You're done. Run your app!