Skip to main content

Posts

Showing posts from February, 2014

Important differences between UIButton, UIBarButtonItem, and UITabBarItem in iOS (Xcode)

UINavigationBar In Xcode, when you create a Navigation Controller or a Table View Controller (within a navigation controller) you are given the option to display a top bar and/or a bottom bar. The top bar is known as a Navigation Bar and we are allowed to drag buttons, text fields, segmented controllers and so on here. But space is limited.  Note: There are a total of three items allowed on the top bar of the iPhone but a segmented controller allows us to add more than one option, and no matter how many segments it contains it only ever counts as one item. When it comes to the Navigation Bar, it is important to grasp the difference between a UIButton and a UIBarButton. The former can display text and images, but the latter has all the system icons available: things like Share (or Action) buttons are available in the UIBarButton class. Further, it must be noted that there are some unusual things to be aware of. First, a plain Navigation Item can be used as a central l

Collections: Dictionaries, Arrays, Lists and Maps in iOS, Windows Store apps and Android

Arrays (or lists) and dictionaries (or maps) are a  common way to store values when programming. This post looks at some of the differing conventions between iOS, Windows and Android. iOS (Xcode) NSArray, NSDictionary and NSSet are part of the iOS Collections framework . Arrays and dictionaries in iOS are free to hold any combination of objects, and it isn't necessary to declare types that the arrays can hold. An NSArray and an NSMutableArray are the basic array (or list) classes. The only difference between the two is that an NSArray is immutable and cannot be added to, or subtracted from, once created (except by certain sorting methods) but an NSMutableArray can have objects removed and inserted. (The methods associated with each class reflect this accordingly.) A basic example of creating an NSArray: NSArray *arrayWords = [[NSArray alloc] initWithObjects: @"Hello", @"World", @"Have a super day", nil]; or more simply NSArray *arrayWords

Numerical: Calculator Without Equal

Anyone programming for iOS can build a calculator. Let's not pretend otherwise. The value in Numerical  is in the design rather than the programming, and there is no question that this app has not only nailed the iOS 7 look and feel, but by adopting iOS 7 sensibilities it makes itself quicker and easier to use than the skeuomorphic alternatives. It has larger hit areas for the numbers, and nifty little features like greying out the buttons that are not valid. For instance if you pressed multiply, then it knows you won't want to press multiply again. It also, as the developer is keen to point out, doesn't have an equals button. But what it does have is brackets front and foremost, and a great history view for those with complex restaurant bills to divide. Looking at the competition there are surprisingly few iOS 7 styled calcs in the App Store, and the ones that are available are slightly different to this. Some have fewer features and this one  has far more scientifi

UITableView (iOS; ObjC and Swift 3, Xcode 8), ListView (Windows Store apps) and ListView (Android)

One of the most important elements for organising and listing data (especially on modern mobile devices) is the table (or list) view. Each OS handles the task in a similar but different way, using a data source and a series of table cells. iOS (Xcode) A UITableView in iOS draws from its data source on a cell by cell basis and we are responsible for making this work. This means there are a few steps to get a simple UITableView example up and running in iOS. I go through them here: drag out a UITableViewController object in the storyboard drag the arrow pointing at your current initial view controller and point it at the new UITableViewController (in the storyboard; note that you can delete the original View Controller at this point) click on the table cell inside the table, and in the Attributes Inspector fill out the box labelled Identifier with the word Cell create a new UITableViewController subclass file called TableViewController (using File > New... or Cmd + N,

Tech debate: Whether or not Microsoft should fork (or adopt) Android

You cannot argue with Charles Arthur's findings that Windows Phone (WP) is struggling in comparison to iOS and Android, but I agree with Brad Reed that 'Forking Android is one thing if you’re Amazon and you really only want to build a tablet that acts as a broader portal to your online shopping empire. However, if you’re Microsoft and you want to sell software with a comprehensive set of mobile services that rival Google’s then it may be much more of a challenge.' I side with Reed, because Arthur's solution would have two immediate results: cause a bigger gap between WP and Windows 8 in terms of capabilities - WP codebase is a subset of WinRT (or Windows Store apps) with a good deal of crossover, much like iOS and OS X, enabling them to be increasingly merged dumb down the MS ecosystem - Android is designed to be simple and effective as a phone OS, which is why it struggles to upscale to tablets, while the MS strategy is to work into their OSes considerations s

Coding Native Text: NSAttributedStrings (iOS), RichText (Windows Store apps) and Spannables (Android)

The most popular way to format text is often HTML. In iOS developers would (prior to iOS 7) use the full-blown UIWebView class, doing something like this: UIWebView *localWebContent = [[UIWebView alloc] init]; [localWebContent loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Text/page3" ofType:@"xhtml" inDirectory:@"Brochure/"]isDirectory:NO]]]; Windows developers also have a similar WebView approach available to them, and Android developers can import HTML into their text view using fromHTML like this: TextView tv = (TextView) findViewById(R.id.myTextView); tv.setText(Html.fromHtml(getString(R.string.my_text))); Introduction The purpose of this post is to address how to get started with native (or core) text and move into sophisticated areas of text manipulation within iOS, Android and Windows Store apps. iOS (Xcode) I've already written a number of blogposts on NSAttributedStr

Classes and Constructors in iOS, Android and Windows Store apps

iOS, Windows and Andoid all include class constructors. Places where setup can be performed when a class is first initialised. So let's say we have a class called Multiplier. It is initialized with a number that later calls to its method multiplyBy are, as you would expect, multiplied by. iOS (Objective-C) After we've gone through the process of File>New to create a new file, and provided the name of our class to Xcode and the class it is to be a subclass of (in this case NSObject), we're ready to create a method -(id)initWithMultiplier:(int)multiplier; to initialize the class. The init prefix to a method signifies a constructor in Xcode, and so our Multiplier.h file would look like this: #import <Foundation/Foundation.h> @interface Multiplier : NSObject -(id)initWithMultiplier:(int)multiplier; -(int)multiplyBy:(int)number; @end and the Multiplier.m file like this: #import "Multiplier.h" @implementation Multiplier int numberToMultiplyBy; -

Changing the background colour in iOS (Xcode), Windows Store apps (Visual Studio) and Android (Eclipse)

There is very little code involved in doing something as simple as changing the background colour of the initial view in iOS, Windows or Android, but there is work involved in knowing the correct classes to use and how to address the view elements. While it is enlightening to work across three operating systems at once, you must also wrestle with three similar but different sets of logic. And inevitably at times you will miss easier ways of doing things and go the long route. Displayed here are the shortest routes that I currently know to change the background colour of an initial view in each OS. If you know better, please post a comment. iOS (Xcode) When you begin a project in Xcode an initial view controller is created. It has a header file and an accompanying .m file (used for the code itself). In addition, if you have chosen to build a universal file, there will be an iPad and iPhone storyboard containing a visual representation of your initial view controller. The view co

Sending a message to the Console in Xcode (iOS), Eclipse (Android) and Visual Studio (Windows 8)

One of the first things you're likely to want to know in any programming language is how to send a message to the console. Xcode: NSLog and the Console In Xcode sending a message to the console is achieved using NSLog. For example, a call to  NSLog(@"Hello World")  will appear in the debug area at the bottom of your Xcode project window, in the right section called the Console. Normally the console will appear automatically when there is a message, but there is also a button that is an upwards pointing triangle in a box at the foot of the project window, which will open the debug area for you. To run the code as a newcomer, place NSLog(@"Hello World"); inside the viewDidLoad: method of your initial view controller. Eclipse: Log and LogCat In Eclipse, Log messages do not appear in the Console window at the foot of the project window, they appear in the LogCat window, which you can open from Window>Show View>Other...>Android>LogCat . You creat

Going native with JSON: iOS, Android and Windows 8

There are essentially two different ways in which JSON can be handled when programming. The first is to parse the JSON objects into native Objective-C, Java, or C# objects within the respective operating systems and the second is to manipulate the JSON within a confined set of methods exclusive to the handling of JSON (or at least rely on these methods for the passing on of information and processing of objects). Advantages and disadvantages The advantage of the former method is that it opens out the entire set of system methods to the object data, while also enabling additions to the data. This might well include the addition of native objects to an array or object as a value. The disadvantage is that the JSON is then exposed to a whole range of non-specific methods and must maintain itself in a way that makes sense if it is to be returned to a JSON format at the end of being manipulated. (e.g. if we were to add values to dictionaries and arrays that are Objective-C objects rather

Android through the eyes of iOS: The app lifecycle and how to make things happen (Part 3)

If you want to test out some basic code in Xcode/iOS, the first port of call is usually viewDidLoad: in the app's first view controller, since here we can run basic stuff that fires NSLog or load things into the view. So how do we do the same in Android? Locating the Activity file If you create a new app in Eclipse (using File>New>Android Application Project ) and navigate to the src>com>[YOUR-NAME]>[APPLICATION-NAME] file you will find your main application file, usually named MainActivity.java. In here there is, after the package and import information a piece of code that reads something like this: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.

Android through the eyes of iOS: Object classes and class extension (Part 2)

When it comes to object classes and inheritance Android and iOS are extremely similar, as can be seen in the two quotations below. The one on the left describing Android and the one on right describing iOS. “The Java class Object—java.lang.Object—is the root ancestor of every class. Every Java object is an Object. If the definition of a class does not explicitly specify a superclass, it is a direct subclass of Object. The Object class defines the default implementations for several key behaviors that are common to every object. Unless they are overridden by the subclass, the behaviors are inherited directly from Object.” Programming Android "NSObject is the root class of most Objective-C class hierarchies. Through NSObject, objects inherit a basic interface to the runtime system and the ability to behave as Objective-C objects." iOS Developer Library (Apple) Object equality Objects also have similar ways of providing information about themselves despite being written w

Android through the eyes of iOS: Classes and Methods (Part 1)

In an Android app the class is defined in a single block using public and private terms to assert whether or not they are ‘public’ or ‘private’ classes, methods and variables. This is different to iOS, where the notion of public and private are established by the structure. The header file in iOS makes public the methods and properties that the programmer wishes to make public to the app and all the rest remains concealed within the .m file. We’d therefore take a class such as the one given by Mednieks et al. in Chapter 1 of their excellent book, Programming Android , and transform it into an iOS method by splitting it into two files. On the left you see the Android code (from Mednieks et al.) and on the right the iOS code (written by me). public class LessTrivial { /** a field: its scope is the entire class */ private long ctr; /** Constructor: initialize the fields */ public LessTrivial(long initCtr) { ctr = initCtr; } /** Modify the field. */ public void incr() { ctr

JSON and the JavaScriptCore Framework (Xcode/iOS)

It would appear that one of the primary reasons behind the inclusion of the JavaScriptCore Framework in iOS is for the manipulation of JSON. At least from the minimal description to be found here , this would seem to be true. Getting started with the JavaScriptCore Framework and JSON In order to use the JavaScriptCore Framework you'll need to add the Framework to your app by navigating to Targets > General > Linked Frameworks and Libraries and adding it in. Then use #import in the .h file of your view controller to include it. At present there's no documentation for the iOS version of the Framework and so you have to rely on the header files (in the Frameworks folder of your app). But to get us started I'll borrow and adapt the initial code from this Big Nerd Ranch  post. // //  JSViewController.m //  JavaScriptCore // // #import "JSViewController.h" @interface JSViewController () @end @implementation JSViewController - (void)viewDid

Some useful GREP to help recycle content.opf and toc.ncx elements in an EPUB 3 toc.xhtml file

There aren't a huge number of steps required to transform a compliant EPUB 2 into an EPUB 3 , aside from the requirement of a toc.xhtml file and specifying your EPUB as type 3.0 in your content.opf file. If you have existing EPUBs or follow a workflow in which you create EPUB 2 first, then you might well find a need to manually build the toc.xhtml file. Doing this by typing each entry into the toc.xhtml file would be tedious. Instead I'd recommend using a GREP or RegEx capable text editor like TextWrangler (OS X). Apple's sample EPUB 3 from iTunesConnect Apple includes three main sections in its sample toc.xhtml file (inside the EPUB 3 sample file available from iTunesConnect). <nav epub:type="toc" id="toc"> <nav epub:type="landmarks"> <nav epub:type="page-list"> The elements within each of these are list items arranged in ordered lists. Starting with the epub:type="toc" you will notice if you