Posts

Showing posts from 2014

Where is my string.replaceAll in Swift?

So prior to getting into Swift , I spent most of my time coding in either Java or Javascript, and there is one Java method that I need to use from time to time at work and that is String.replaceAll . So naturally when I started coding in Swift, one of the things that I was thought about was, does Swift have a String.replaceAll method? Well Swift does have a replaceAll method, its just called something else i.e. String.stringByReplacingOccurencesOfString . So the name is fairly logical and does make sense, its just that my brain was wired to replaceAll, prior to coming across this method.

Knowing which notification caused the iOS app to resume

This was another one of those rather simple problems that took me a while to work out, mainly because of my approach to iOS development. So how am I approaching it? well since I am new to it, I tend to not know a lot of stuff so I generally have lots of problems that I need to solve. So if I get stuck on a problem, I will simply move onto one of the other problems that need to be solved and that I can solve and later, revisit the problem I was originally stuck on. This will allow my iOS knowledge to grow and something that is  not immediately obvious initially, will be so later on. Problem I have an iOS app that uses local notifications to create a more engaging user experience. The local notifications fall into one of two distinct categories and when the app resumes via a notification, it performs a set of "app resume/awake" behaviours that are relative to the notification category. Wow, even I would have trouble following what I just wrote, so let me just make this beyon...

Xcode starter project with the Ionic HTML5 framework using Swift

I mentioned in one of my previous posts, about how I thought the Ionic framework for building HTM5 apps was totally awesome. Well it is and the way I was trying to use it was to build a Hybrid iOS app. Ok, when I say hybrid, I mean the logic for the app is almost equally split between both native code and HTML5 code. Why Hybrid app? So the question is, without knowing any native iOS code, how did I come to the conclusion to build a hybrid app? Ok so apparently you can make a fully HTML5 app and use the Apache Cordova framework to build an iOS app without getting your hands dirty with native code, that sounds awesome! However, to me it was important to understand native code, just in the event that something goes wrong in the app and I need to fix it by tweaking native code, I am not completely clueless about how to go about fixing it. So in an attempt to know more about iOS internals, I started off by building simple native programs(apps?) using Swift with Xcode 6 beta at the ti...

Displaying HTML content stored in a folder within an iOS app

Image
Through this journey of getting to know "how to build apps for the iPhone with Swift", I have encountered a fair share of problems. You can have a read of my previous posts on Getting started with iOS programming Serving HTML content in an iOS app Discovering Core Data migrations And  how to deal with Core Data migrations  to get an idea of the sort of problems I have faced. In this post, I am going to share about a rather simple issue that I encountered very early when I started building my app. The app I am building is an HTML5 app, actually no; A more accurate statement for this would be that it is a hybrid app, as the logic is almost equally distributed between native code and HTML5 code. Problem The problem I had was that when I would add a folder to my iOS app with web content and tried to refer to it using let path = NSBundle.mainBundle().pathForResource("index", ofType: "html", inDirectory:"webContent") It simply wouldn...

Dealing with Core data migration(lightweight) in an iOS app written in Swift

Image
So a while ago, I wrote on how I discovered the existence of this thing call Core Data migrations, by having my app crash when I added a new entity to my core data model . So the solution to that is actually not very hard, the only issue I had was that a majority of the solutions out there, deal with it in apps built with Objective-C and I needed a solution in Swift. I am not very surprised by this given how new Swift is. Problem I have defined the problem in mode detail in one of my previous  post s, its a small post and I would suggest having a read of it to better understand the problem. Anyway in a nutshell, I was working on my iOS app which used Core data, so I added an entity to my data model, deployed the app, it was saving and retrieving data and it was all working fine. Then I added another entity to my data model and then the app crashed. So then I was looking for a solution to why my app was crashing and what I can do to fix it. Solution Once again in a nutshell th...

My app crashes when I add a new entity to the core data model in my iOSapp

Ok so I was looking for a way to store data to persistent storage in my app. Since I am building an HTML5 app, my first solution was to store data using the now deprecated WebSql database ( indexedDB was only introduced in iOS 8!).  Now i had the basic data storage going and then I realised some of the storage limitations in the WebSql database, so I began searching for another solution and during that search I met  Core Data !! It was almost like love at first sight, it had all the storage awesomeness that I was after. Since there was so much love, I just wanted to build something and see how it works. So I followed an online tutorial rather than reading Apple's documentation on it, what I did was,  I added an entity to my core data model and wrote the logic to store data and it was all working! So now my app could store and retrieve data from a persistent data source without the storage limitation imposed by WebSql in a webView. It was all going great and thanks...

Modifying scope variables outside it's AngularJS controller

So, work involves a fair bit of AngualrJS  from time and again and for the most part, I am lucky enough to work with pure AngularJS code, however there are times that I have to work with non-AngularJS code that is supposed to interact with AngularJS code. Please have a look at the example below to get a clearer picture of what scenario I am talking about. Now AngularJS is awesome, but it was not always around i.e. it was released recently compared to some other Javascript libraries. So at times, integrating it with an application that already has well-written javascript, can be a bit challenging. So you may think, why angular is being added to something that already has well-written javascript code? To be honest, I do not know the exact answer to this, but if I were to guess, I suppose it would be to "leverage" some of the benefits that come from using angular. Scenario Say we have an input element in the container that we name TestCtrl, as follows ...

Optional types in Swift

At work, I code in either Java, Javascript (AngularJS, Node.js) or CSS via bootstrap, so when I am coding in Swift, it is strictly something I do in my free time. Hence I make a point to keep notes of stuff that I learn which can potentially serve as a future reference for me. Swift (Apple's new programming language), allows for the presence  optional type or optionals . This concept is very well explained in this Ray Wanderlich podcast , at time 27:03 (mins:secs). As mentioned in that podcast,  the designers of Swift decided to ensure that you have no null ( nil ) references in your code. So in languages such as Java, say you have an object called person which represents a person , that has information about where the person lives i.e. address property. There is some more detail in where the person lives i.e. the name of the street i.a. street property. So now if we want to what street the person lives on, you would do person.address.street . That way you tr...

Serving HTML content in an iOS app that works in iOS 7 and later (using Swift)

As I have mentioned in an earlier post , I really enjoying coding in Swift. Now what am I doing with it? Well I am trying to build an HTML5 app that must work on devices with iOS 7. So in iOS8 apple has introduced a whole bunch of features that facilitate easy communication between web content and lets just call it back-end Swift code, but those features are not in iOS 7. So why do I want to build something that would work in an older OS? well I do not expect existing iOS users to upgrade to iOS 8 straight away and i also know a couple of people who would be very reluctant to upgrade their iPhones to iOS 8. Now in case you do not, you can have a read of the "Working with WebViews" section of this post , to know how to serve HTML content with WebViews. So when I started building my app, I wanted to know: How do I invoke some Swift code from my HTML content? Well the solution to this may feel a little bit "hacky" but it is a solution to achieve this.  The followi...

Getting started with iOS programming using Swift (Part 1)

I have not been too fond of Objective-C, which was the primary reason for me to stay away from making iOS apps till now. So what changed? Well Apple has done something very interesting recently and that is the introduction of a new programming language i.e. Swift. Swift is awesome, it almost feels like Python, C++ and Objective-C had a baby with some of their good parts in them. So I have been getting to know Swift and it is an awesome language to program in. What I am going to share with this and a series of blog posts are solutions to some problems that i have encounter while i am trying to finish my first iOS app. The one hurdle that I have encountered while getting started on developing an iOS app is that a majority of the solutions for iOS specific problems provide solutions to them using Objective-C. Which is fair, because Swift has not been around for that long. Anyway let us get started with a few basics, A few basics I would highly recommend having a read of this book...

Sending browser independant notifications from a web-app

So I came across a data-mining product that presents very useful information via a web-app. What gets delivered to the users of the product is new information  in the form of reports at regular intervals. So the process is, new data is acquired  all the reports are run again on newer data a manager overseeing the product sends an email to the users that notify them that the reports have been updated. Motivation So this process is great and it requires very little effort on part of the manager emailing the users. So the question is "why make that manager use even little effort, when the manager's time could be better utilized?". While the manager can add some more personalized info in the email, the entire process can potentially have the following disadvantages human error: the manager may get busy and may forget to notify the users of the updated reports distractions: if the user has to navigate away from the web app and check their email, this could potent...

Open source HTML5 visualisation library to draw charts on the web (morris.js)

At work(ongoing,full-time contract) we use the Google Visualizaton  library for our various data visualisation needs. So naturally when I was building this tool to streamline the process of Post Traumatic Amnesia(PTA) assessment, my first thought was to use Google Visualization library for the tool's data visualisation needs. However that was only till I realised that the tool needs to work offline. Google Visualisation is a very good library but it only works when the the web app is online, so after some looking around I found a tool that works offline too i.e. morris.js . It uses the fantastic Raphael.js  library to render beautiful SVG based charts, on the web. Problem The objective is to visualise data in a web application, so it can be easily interpreted without having to go through raw numbers. The web application needs to be demoed on....say a laptop and the places where it could potentially be demoed may not have internet connectivity. It could also be in a plac...

Reading a Sqlite database using client-side javascript

Image
Ever had one of those problems at work that seems almost impossible (i.e. very hard) to solve at first? well i did and not so long ago, and tuns out that there is a solution to it. Problem Read the contents of a  Sqlite  database client-side and present them in an HTML based GUI. So my first reaction was, even if there is a solution to this, it surely cant be very efficient reading an entire Sqlite database client-side? well those were the requirements and there were some genuine reasons to back up those requirements, so i had to stop whinging and direct my efforts towards an actual solution. Solution Turns out that there is a solution for this! Ever heard of  emscripten ? Funnily enough i had used it while doing my research to port the  OpenCV  library to javascript. Anyway there is  sql.js , which is a javascript port of the Sqlite database, which could do what i wanted it to do. The problem is the documentation of ...