Wednesday, July 23, 2014

Starting with iOS Development using Swift


I'm currently developing an Android-App. I thought it would be nice if this app is also available for iOS. The Android App contains standard components which maybe easy to realise at iOS.

Xcode beta

After installing the Xcode beta I was able to use the new programming language Swift which Apple will officially introduce in Fall this year. Xcode contains a fancy playground for playing with the new Swift language. If you assign variables you can check the values on the right instantly while typing.
Overall I do like Xcode. Its similar to visual studio. It contains an interface builder where I could play with the available UI components.

Approaching development

 Sometime I got bored with senseless clicking in Xcode and I felt ready for first tutorials. I started with a swift tutorial tutorial by Jameson Quave. It was interesting to see how the IB works in action. It is more than just placing components. You can actually do relations between the controller and code. Furthermore it is possible to chain multiple views.

Objective-C together with Swift

There are tons of Objective-C libraries available as open source. It would be a bad move to skip backwards compatibility so apple decided to introduce bridge header for using Objective-C-Code in Swift. It is explained in the apple developer reference.

Using SVWebViewController with Swift

I've chosen the Objective-C library SVWebViewController available on github to use in my test project.

It is a library that mocks a browser window using the internal UIWebView component of iOS. Its creating an own view with the browser window, controls for navigation, page reloading, loading indicator and page title.

Just download a stable release, extract and drag the folder SVWebViewController/SVWebViewController into your project. Xcode may ask something but I left everything at default.
My project was created as an single page Application.
It is named "webview" so I can see a folder "webview" in the project navigator. I dragged the SVWebViewController directly under that folder.

Now I needed to create the bridge header file. Just right click in the Project Navigator -> New File -> iOS -> Source -> header file. I chose the name "bridge-header.h". Then insert the references of the objective-C header files into the the file:

#import "SVWebViewController.h"
#import "SVModalWebViewController.h"

The bridge header file must refer to the projects build settings. In the Project navigator click at the root and click on build settings in the center view. Scroll down to Swift Compiler - Code Generation (or hit command+f to search for it). Click into Objectvie-C Bridging Header and insert the relative path to your bridge header file there. In my case its "webview/bridge-header.h" (<project-name>/<header-filename>.h).

The easiest way to use SVWebViewController is to add code for initialising into the application function of the AppDelegate.swift.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    // Override point for customization after application launch.
    self.window = UIWindow(frame:UIScreen.mainScreen().bounds)
    self.window!.backgroundColor = UIColor.whiteColor()
    self.window!.rootViewController = SVModalWebViewController(address: "")
    return true

No comments:

Post a Comment