Tuesday, November 11, 2014

Building Cross Platform iOS and Android Apps using Xamarin and C# - 14-16 Jan 2015 (OSLO, NORWAY)

In collaboration with ProgramUtvikling, I will be teaching the Writing Cross Platform iOS and Android Apps using Xamarin and C# course on the 14-16 Jan 2015 in Oslo, Norway. This is a 3-day course that teaches the fundamentals of iOS and Android programming using Xamarin and the C# language . If you and your team are interest to get into cross platform iOS and Android development, this course provides the most cost effective way to get started! At the end of the course, you will be able to develop applications that runs on both the iOS and Android devices. 


What's more, this course will be taught using the latest version of Xamarin, and it will cover the latest Xamarin Forms, which allows you to build cross platform user interfaces for iOS and Android devices. For this course, you will get a lab-intensive workbook that illustrates the many important concepts of building an iOS and Android app.



Sunday, November 09, 2014

Tip - Redirecting Users to the Setting Page (iOS 8)

In iOS, you often need to redirect the user to the Settings page. For example, if you detected that the user has turned off WiFi and your application needs to access the Internet using WiFi, you might want to automatically launch the Settings page so that the user can turn on the WiFi directly. In iOS 8, you can do so via the UIApplicationOpenSettingsURLString constant, like this:

        if UIApplicationOpenSettingsURLString != nil {
            var appSettings = NSURL(string: UIApplicationOpenSettingsURLString)
            UIApplication.sharedApplication().openURL(appSettings!)
        }


The above code snippet will redirect the user to the Settings page. 

Fundamentals of Android Programming - 26-30 Jan 2015 (OSLO, NORWAY)

In collaboration with ProgramUtvikling, I will be teaching the Fundamentals of Android Programming course on the 26 - 30 Jan 2015 in Oslo, Norway. This is a 5-day course that teaches the fundamentals of Android programming, such as Activities, Intents, Broadcast and Broadcast Receivers, Networking, Notifications (local and remote), Data Storage, and more. If you and your team are interest to get into Android development, this course provides the most cost effective way to get started!


What's more, this course will be taught using Google's latest IDE - Android Studio. For this course, you will get a lab-intensive workbook that illustrates the many important concepts of building an Android app.

Fundamentals of iOS Programming - 19-23 Jan 2015 (OSLO, NORWAY)

In collaboration with ProgramUtvikling, I will be teaching the Fundamentals of iOS Programming course on the 19 - 23 Jan 2015 in Oslo, Norway. This is a 5-day course that teaches the fundamentals of iOS programming, such as Storyboard, View Controllers, Networking, Notifications (local and remote), Data Storage, and more. If you and your team are interest to get into iOS development, this course provides the most cost effective way to get started!


What's more, this course will be taught using Apple's latest language - Swift. The Swift language will be discussed as you work through the various lab exercises as this is the best way to learn a new programming language. You will receive a copy of my upcoming book - Beginning Swift Programming, as well as a Swift language quick-reference guide.


Tuesday, November 04, 2014

Updated Course Schedule from Nov 2014 to April 2015

I have just updated the new course schedule from Nov 2014 to April 2015. Among the courses, these are some of the new courses that I will be introducing:

1. Apple Watch Programming
2. Getting Started with Arduino
3. IoT (Internet of Things) Programming

If you are a mobile developer, this is an exciting moment as Apple has just released the new iPhone 6 and iOS 8, while Google has just released the new Android 5.0 Lollipop.

Year 2015 is the year of the wearables, as Apple and Google look set to change the world with Apple Watch and Android Wear, respectively. Also, another buzzword that is getting a lot of attention lately is IoT (Internet of Things). The role played by sensors and wireless technologies (such as Bluetooth Low Energy and ZigBee) is becoming more significant.

So, prepare yourself to embrace the new technologies coming in 2015 that will definitely change the way you build apps.

Learn Swift Programming - 1-2 Dec 2014

If you are an iOS developer, you should definitely get a head start in Apple's new programming language for developing iOS and Mac OS X applications. Designed to be an easy to learn and type-safe language, Swift allows developers to get productive in a very short time. Come and join us and learn all the important features of Swift in 2 days!

All attendees will get a copy of my latest book -Beginning Swift Programming, when it is published in Dec 2014. In addition, you will get  a Swift language reference guide that will come in handy as you code your way through Swift.

Topics that you will learn
• Introduction to Swift
• Data Types
• Strings and Characters
• Basic Operators
• Functions
• Collections
• Control Flow and Looping
• Classes, Structures, and Objects
• Class Inheritance
• Closures
• Protocols and Delegates
• Generics

Sunday, November 02, 2014

Beginning Swift Programming - For Educators

My latest book- Beginning Swift Programming will be released in Dec 2014. Designed to be a gentle introduction to the new language, it is ideal for readers who are already familiar with object oriented programming, as well as existing iOS programmers who need a quick guide to the features and syntax of the language. Educators teaching the Swift Programming language will find this book a handy learning guide and resource. In addition, companion slides for educators are also available for adopters of this book. 

I am also currently conducting a 2 day course on Swift Programming worldwide. To run this class in your school/faculty, contact me for more details. 

Thursday, October 30, 2014

Why Swift Flies for iOS Developers

I recently wrote an article for MobiForge outlining the importance of Swift and the effect it has on the industry. I also took this opportunity to compare it with Objective-C. Here is the abstract of the article:

"Now that the dust has settled somewhat on Swift, the new language on the block for developing iOS and OSX applications, we take a look at its impact and improvements over its predecessor, Objective-C. Apple claims Swift to be a modern, safe, and powerful language for developing for iOS and OSX. Just how powerful is Swift compared to the venerable Objective-C? And how does it make developing applications easier and safer? In this article, I will walk you through some of the important features in Swift and how it improves on Objective-C. In a follow up article, I will show you how you can use Swift and Objective within the same project."
Check out the full article here. 

Monday, October 13, 2014

Swift - Dealing with pointers in Swift

When using certain APIs in Objective-C and C, you often have to deal with pointers. For example, you can write data to the NSOutStream object using the write:maxLength: method:

-(void) writeToServer:(const uint8_t *) buf {
    [oStream write:buf maxLength:strlen((char*)buf)];

}

The write:maxLength: method takes in a pointer of type unint8_t, containing the data you want to send across the stream. The equivalent of the above function in Swift is:

func writeToServer(dataToWrite:NSData) { 
    oStream!.write(
        UnsafePointer(dataToWrite.bytes), 
        maxLength: dataToWrite.length) 
}

For reading of data, in Objective-C, you would supply an array of uint8_t:

uint8_t buf[1024]; 
unsigned int len = 0; 
len = [(NSInputStream *)stream read:buf maxLength:1024];

In Swift, you can use the following:

let buf = NSMutableData(capacity: 1024) 
var buffer = UnsafeMutablePointer(buf.bytes) 
let len = (aStream as NSInputStream).read(buffer, maxLength: 1024)

Tuesday, October 07, 2014

iOS 8 - Display an alert

The UIAlertView class is deprecated in iOS8. In place of it is the more adaptive UIAlertController class. Here is a quick example of how to display an alert in iOS 8:

    func displayAlert(title:String, content:String) {
        var alertController = UIAlertController(
            title: title,
            message: content,
            preferredStyle: UIAlertControllerStyle.Alert)
        
        var okAction = UIAlertAction(
            title: "OK", style: UIAlertActionStyle.Default) {
                (action) -> Void in
                println("Tapped on OK")
        }
        
        alertController.addAction(okAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }

To display an alert, you can call the displayAlert() function like this:

self.displayAlert("Hello", content: "Hello, iOS 8")

The above will display an alert like the following:

Xcode 6 - iPhone Simulator Folder

In Xcode 6, the location of the applications on the iPhone Simulator has changed. Prior to iOS 8, the location of the Documents folder of your app can be found here:

/Users/weimenglee/Library/Application\ Support/iPhone\ Simulator/<iOS_Version>/Applications/<application_id>/Documents 

In xCode 6, the location of the simulator has changed to:

/Users/weimenglee/Library/Developer/CoreSimulator/

However, this location contains a list of folders for different types of simulators. If you go into this folder, you will see something like this:


Each folder correspond to a specific iPhone Simulator. To know which folder corresponds to which simulator, use the following command in Terminal:

Weis-Mac-mini:~ weimenglee$ xcrun simctl list
== Device Types ==
iPhone 4s (com.apple.CoreSimulator.SimDeviceType.iPhone-4s)
iPhone 5 (com.apple.CoreSimulator.SimDeviceType.iPhone-5)
iPhone 5s (com.apple.CoreSimulator.SimDeviceType.iPhone-5s)
iPhone 6 Plus (com.apple.CoreSimulator.SimDeviceType.iPhone-6-Plus)
iPhone 6 (com.apple.CoreSimulator.SimDeviceType.iPhone-6)
iPad 2 (com.apple.CoreSimulator.SimDeviceType.iPad-2)
iPad Retina (com.apple.CoreSimulator.SimDeviceType.iPad-Retina)
iPad Air (com.apple.CoreSimulator.SimDeviceType.iPad-Air)
Resizable iPhone (com.apple.CoreSimulator.SimDeviceType.Resizable-iPhone)
Resizable iPad (com.apple.CoreSimulator.SimDeviceType.Resizable-iPad)
== Runtimes ==
iOS 7.0 (7.0 - Unknown) (com.apple.CoreSimulator.SimRuntime.iOS-7-0) (unavailable, runtime path not found)
iOS 7.1 (7.1 - Unknown) (com.apple.CoreSimulator.SimRuntime.iOS-7-1) (unavailable, runtime path not found)
iOS 8.0 (8.0 - 12A365) (com.apple.CoreSimulator.SimRuntime.iOS-8-0)
== Devices ==
-- iOS 7.0 --
-- iOS 7.1 --
-- iOS 8.0 --
    iPhone 4s (8C1303A2-C590-4822-BB9B-12EC32035C8C) (Shutdown)
    iPhone 5 (402C822D-567F-4F3A-B113-1499C5A7949C) (Shutdown)
    iPhone 5s (B6D71F6A-E94B-47ED-AF25-882FDEF8E6F5) (Shutdown)
    iPhone 6 Plus (5BCD1507-6F06-4D66-B71D-47670EB703B2) (Shutdown)
    iPhone 6 (00100565-4E4D-4D9A-A875-544E0A6E4F4F) (Booted)
    iPad 2 (187D75DD-824D-4381-AE2F-BDF57FEB87E0) (Shutdown)
    iPad Retina (604667D3-8366-4147-B0AD-F7840198713C) (Shutdown)
    iPad Air (01D6AEBC-F12A-47EE-82CB-0140AF117AE0) (Shutdown)
    Resizable iPhone (9C138BCD-BFB9-4AEB-A218-F0F99F678220) (Shutdown)
    Resizable iPad (4966C908-AAA7-43EC-8E3A-EADF8BFA3C05) (Shutdown)
Weis-Mac-mini:~ weimenglee$ 

In my example here, I am looking for iPhone 6, which has the device id of 00100565-4E4D-4D9A-A875-544E0A6E4F4F. You can then go into this folder and the following path shows the folder that contains all the applications installed on this simulator:

/Users/weimenglee/Library/Developer/CoreSimulator/Devices/00100565-4E4D-4D9A-A875-544E0A6E4F4F/data/Containers/Data/Application/

You can now locate your specific application and find the Documents folder:

/Users/weimenglee/Library/Developer/CoreSimulator/Devices/00100565-4E4D-4D9A-A875-544E0A6E4F4F/data/Containers/Data/Application/FD7949EA-7D39-4BC6-95F4-77F320592651/Documents/


Tuesday, September 30, 2014

Swift - Calling Objective-C code

In Xcode 6, you now have the option to create a project using either Swift or Objective-C. In addition, you can also use Objective-C code in your Swift project. This post shows you how. 

1. First, create an iOS project that uses the Swift language

2. Add a Objective-C File file to your project. Name it as MyClass. 

3. You will be prompted with the following:


4. This will add a file named UsingObjC-Bridging-Header.h to your project. Behind the scene, Xcode automatically sets the Objective-C Bridging Header setting to this file:


5. Add a Header File file to your project. Name it as MyClass. 

6. Populate the MyClass.h with the following:

#import

@interface MyClass: NSObject

@property (strong, nonatomic) NSString *property;

- (void) method;

@end

7. Populate the MyClass.m with the following:

#import "MyClass.h"

@implementation MyClass

- (void) method {
    NSLog(@"In method now");
}

@end

8. Populate the UsingObjC-Bridging-Header.h with the following:

#import "MyClass.h"

9. To use the Objective-C class that you have added to your project, add the following statements in bold to the ViewController.swift file:

    override func viewDidLoad() {
        super.viewDidLoad()

        // create an instance of the class
        var myClass: MyClass = MyClass()

        // set its property
        myClass.property = "some random strings here"

        // call its method
        myClass.method()
        
        // retrieve its property
        println(myClass.property)

    }

Monday, September 29, 2014

Swift - Checking if a method exists

A lot of changes has happened in iOS 8, and a lot of APIs require that you call certain new methods before they can work. Examples are locations, notifications, and more.

To ensure that your app can work on older versions of iOS as well as the new iOS 8, it is important that you only call the new API when your app is running on iOS 8. The best way is to test if a method exists. You can do so via the respondsToSelector() method. The following example tests to see if the UIApplication object contains the registerUserNotificationSettings() method:

if application.respondsToSelector("registerUserNotificationSettings") {

}

Another example:

if self.locationManager.respondsToSelector("requestAlwaysAuthorization") {


}

iOS 8 - Displaying Local Notifications

Displaying local notifications has changed a little in iOS 8. You now need to seek the user's permission in order to display notifications. You can do so via the AppDelegate, like this:

    func application(application: UIApplication, 
    didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        application.registerUserNotificationSettings(
            UIUserNotificationSettings(
                forTypes:
                UIUserNotificationType.Sound |
                UIUserNotificationType.Alert |
                UIUserNotificationType.Badge, categories: nil))
            
        return true

    }


Once user's permission is granted, you would now be able to display a notification as usual:

        var localNotification =  UILocalNotification()
        //---the message to display for the alert---
        localNotification.alertBody =
            "You have entered the region you are monitoring"
        
        //---uses the default sound---
        localNotification.soundName = UILocalNotificationDefaultSoundName
        
        //---title for the button to display---
        localNotification.alertAction = "Details"
        
        //---display the notification---

        UIApplication.sharedApplication().presentLocalNotificationNow(localNotification)

Sunday, September 28, 2014

Swift - Using Selector in Swift

In Objective-C, you often encounter methods that accepts a Selector as the argument. A good example is the NSTimer's NSTimer.scheduledTimerWithTimeInterval() method:

- (void)onTimer {
}

    [NSTimer scheduledTimerWithTimeInterval:0.05
                                     target:self
                                   selector:@selector(onTimer)
                                   userInfo:nil
                                    repeats:YES];   


A selector is basically a name of a method that will be executed by another method. In Objective-C, to pass in a selector argument you use the @selector() keyword together with the name of the method. 

In Swift, you can simply pass in the name of the method as a string, like this:

func onTimer() {
}

        NSTimer.scheduledTimerWithTimeInterval(0.05,
            target:self,
            selector:"onTimer",
            userInfo:nil,
        repeats:true)

Swift - Generating a random number

If you want to generate a random number in Swift, use the arc4random_uniform() function. The following code snippet generates a number from 0 to 9:

var i = Int(arc4random_uniform(10))

Thursday, September 25, 2014

iOS 8 - Getting Location Data

Getting location data in iOS 8 has changed drastically. Here is what you need to do if you are using the CLLocationManager class to get a location in iOS 8.

In iOS 8, you need to add the following two keys in your info.plist file:

For getting location in the foreground

Key
NSLocationWhenInUseUsageDescription

Value
"This app will display your current location" (up to you to write anything you want here)

For getting location in the background

Key
NSLocationAlwaysUsageDescription

Value
"This app will log your location into a database" (up to you to write anything you want here)

Before you use the startUpdatingLocation() method, you have to request for user's permission. To use the location in the foreground, call the requestWhenInUseAuthorization() method:

var lm: CLLocationManager!
...
        if (UIDevice.currentDevice().systemVersion as NSString).floatValue>=8.0 {            
            //---request for foreground location use---
            lm.requestWhenInUseAuthorization()
        }
        
        lm.startUpdatingLocation()


If you want to continue getting location data in the background, call the requestAlwaysAuthorization() method:

        if (UIDevice.currentDevice().systemVersion as NSString).floatValue>=8.0 {
            //---request for background location use---
            lm.requestAlwaysAuthorization()
        }


Note that if you want the app to access location information both in the foreground and in the background, it is sufficient to call the requestAlwaysAuthorization() method.