Thursday, November 13, 2014

Programming with Swift for iOS !


What is swift?
  • A new programming language for iOS and OSX app development.
  • Adopts modern programming patterns and features to make programming easier and flexible.
  • Provides seamless access to existing Cocoa frameworks and mix-and-match interoperability with Objective-C code.
  • First industrial-quality systems programming language that is as expressive and enjoyable as a scripting language.

New in swift
  • Support for functional programming patterns like maps and filters.
  • Structs support protocols, methods and extensions.
  • Support for Generics and optionals.
  • Support for tuples and hence functions that can return multiple values.
Why use swift?
  • About 2.8X faster than objective C.
  • A compiled programming language with syntax similar to scripting languages.
  • Improved readability over other languages by removing extraneous parenthesis and semi colons.
  • Enums and control flows have been vastly improved.

Tools Needed
  • Needs Apple LLVM 6.0 for compilation, which is shipped with Xcode 6.0.
  • Uses the same runtime as existing Objective-C system on Mac OS and iOS.
  • Backward compatible up-to iOS 7.0.
Introduction to “Playground”
  • A new type of file that allows you to test out Swift code, and see the results of each line in the sidebar.
  • Results appear on the sidebar as soon as any code is typed.
  • Great way to learn about Swift, to experiment with new APIs, to prototype code or algorithms.
Objective C Vs Swift
  • Closures: Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C.
  • AnyObject: The equivalent of ‘id’ from Objective C, is ‘AnyObject’ in swift.
  • Introspection: The equivalent of doing if ([obj isKindOfClass:[NSString class]]) { ... }) in swift is if obj is String.


















Monday, March 10, 2014

iOS Keychain: Sharing data between iOS apps !

Save data in keychain from one iOS app and access it from another iOS app (Xcode 5) !


Creating your first app that will write to the Keychain


Settings for project : 
  1. Add the Security.framework to your project.
  2. Download UICKeyChainStore  and add the UICKeyChainStore.h and UICKeyChainStore.m files from Lib folder to your project.
  3. Go to Target -> Capabilities -> Keychain Sharing. By default it is off. On this option.
  4. As you will on it, a file name projectName.entitlements will add in your "Project Navigator", with key "Keychain Access Groups" of type "Array" with one "Item" of type "String" and Value "$(AppIdentifierPrefix)$(CFBundleIdentifier)". For example : com.my.firstApp is the Bundle identifier then Value will be "$(AppIdentifierPrefix)com.my.firstApp". It will look like this : 



Now all you need to do is start using the UICKeyChainStore and you'll be saving data to the KeyChain.

Now, in your class import "UICKeyChainStore.h" and write the following code: 

[UICKeyChainStore setString:@"mySecretPassword" forKey:@"password" service:@"MyService"];

[UICKeyChainStore stringForKey:@"password" service:@"MyService"];

Specifying the service allows you to store keys for a particular service, maybe you have a Twitter or Facebook specific set of keys you wish to save.

As you can see we are not specifying the accessGroup. By default it will pick the first access-group specified in your projectName.entitlements when writing and will search across all access-groups when none is specified.

Allowing your second app to access your shared keychain data:

Settings for project : 
  1. Repeat above steps from 1 to 3 (for firstApp). It will look like this : 



We want access to the Keychain group $(AppIdentifierPrefix)com.my.firstApp for reading, but we still want the default writing to occur in our own keychain group so we still specify $(AppIdentifierPrefix)com.my.secondApp as the first / default group. Add one more item in "Keychain Access Groups" with Value $(AppIdentifierPrefix)com.my.firstApp

It will look like this: 


Now, read the value from the shared Keychain exactly how we did in our first app.

In your class import "UICKeyChainStore.h" and write the following code: 

[UICKeyChainStore stringForKey:@"password" service:@"MyService"];

This should return the value “mySecretPassword” :)


Happy Coding :)