Ionic Android Debugging: There was a network error

Debugging an Ionic app using Visual Studio (Cordova Project) is very simple using Ripple; however, once you deploy to a device it becomes a lot more challenging. One way to help debug issues deployed to your android device is to use the livereload and consolelogs options:

$ ionic run android -l -c

This enables you to see console.log() statements in your code. Not as good as being able to step through the code in Visual Studio, but still very helpful for troubleshooting.

$ cordova plugin add cordova-plugin-whitelist@1.0.0

As a bit of a head scratcher, you can end up with an error once your app starts running on your android that says, “There was a network error” running on port 8100 by default. If that happens, open your index.html file add the following META tag.

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap:; style-src 'self' 'unsafe-inline'; media-src *">

iOS Development with Swift

Several months ago I made the mistake of updating my iPhone OS and it was rendered almost unusable. So I decided that Apple could pound sand and bought an Android Galaxy 6 Edge. One of the nice things about Android for a developer is how easy it is to create and deploy an application. I ran through some tutorials using Android Studio and was very impressed even though I’m not a professional Java developer. Coming from a very strong C# background, the switch was not difficult and was enjoyable. To make things even more fun, I started playing around with Ionic since I have been doing Angular development lately. The best bang-for-the-buck from a business perspective seems to be Ionic (hybrid apps); however, there is still a very solid demand for native Android and iOS development.

Having the opportunity to take a nice, long, much-needed Christmas and New Years vacation – I of course play the obligatory NHL16 and Witcher 3 on the Xbone…but like when I decided to learn to play the piano, I also committed to chipping away at becoming a better mobile developer – specifically, native iOS/OS X development on a Mac using Xcode. I bought a Macbook Pro and immediately went to The most trusted advice that I’ve read is that the first place to start is learning Swift and then going back to learn Objective C. In college, C was my favorite language and I haven’t felt to need to revisit it much since then, so this took some self-prodding. I managed to read through The Swift Programming Language (Swift 2.1) while trying out code examples using the Playground in Xcode, and while I doubt I retained everything I need to hit a running pace with Swift, I certainly think it was worth the time and effort. Besides that, this is exactly the same approach I’ve used with other languages that I’ve learned including C# and SQL – and I am pretty darn good in that arena.

So if you are just starting out with OS X and Xcode because your entire life has been based in DOS, Windows, and Linux here are a few things that I found to make life easier for myself starting out:

Karabiner – this is absolutely amazing if you are like me and very proficient with hotkeys in Windows and use them A LOT. Karabiner will let you map PC hotkeys such as CTRL-C, CTRL-V, END, HOME, etc.  While I certainly think if you are comfortable making the full switch to using the Command hotkeys in OS X, then by all means do that. Additionally, you have some flexibility built into OS X to swap the CTRL and Command keys; however, I found this lacking and ended up being painful for some applications as well as using Microsoft Remote Desktop to get to my PC.

System Preferences > Mouse > Scroll Direction Natural – I turned this option off so that the scroll-wheel up and down worked the same as the PC (it is reversed in OS X). However, admittedly when I do not have a mouse and keyboard attached, I like all of the default OS X settings on the Macbook Pro trackpad and keyboard just fine. I still use a natural PC keyboard after many years when I’m sitting at my desk.

If you are starting out with iOS development – I highly recommend the advice from John Sonmez about starting with Swift. Swift is a really nice language if you come from C# and javascript background and you will appreciate the ability to do new things like provide multiple return values via Tuples, and the ability to create both static and class methods (similar to C# static methods but can be overridden in the derived class).

Happy New Year!

Programming For Yourself Using System.Diagnostics

In the day-to-day grind of software development with .NET, some minor concepts are sometimes forgotten…and no…I’m not talking about design, unit testing, and QA (isn’t that what customers are for??) but implementing Tracing, or Assert functions. Remember Assert()???  No, you probably don’t because you are releasing DEBUG versions of your assemblies instead of RELEASE (that’s what the pros do, right??).

Let’s pretend for a few minutes that you are working on your own software project where you compile for deployments using RELEASE builds. We know that structured exception handling is important, but when should you Assert? The simple answer is: when you make an assumption that a condition should be impossible.

If the variable “amount” ends up NaN, your application will halt and you’ll get a nice dialog box with a stack trace and message (unless it is an ASP.NET application, which you can configuration a listener for as I will explain later). You should write assert statement liberally in your code. The benefit is the additional documentation your code provides explaining assumptions (and having those assumptions challenged and put in your face), and are removed when compiling in RELEASE configuration. If you look at your project properties in Visual Studio in the Build tab, you’ll see that both DEBUG and TRACE constants are defined.

Regarding tracing, I honestly prefer to simply implement log4net and call it a day; however, splitting hairs technically there is a difference between tracing and logging but do as you will…there are other options such as Elmah, or simply using a TraceListener. I’ve worked with other developers who don’t like having the log4net dependency in every assembly, so you could follow the anti-pattern of wrapping the ILog class.  Another alternative is to use the Trace class, if you aren’t doing that already, because RELEASE builds include the TRACE definition by default. Just like log4net allows you to define multiple loggers, the built-in Trace class also provides that ability. This is very handy in ASP.NET because you can turn in trace output and view the results on the page.

As an example, I’m going to create a TraceListener that is implemented using the log4net rolling file appender. This way I can simply use Trace methods, and have log4net do all of the heavy lifting. While you can simply using Trace.Write or WriteLine, I prefer to use TraceInformation, TraceWarning, and TraceError which give the distinction of the trace level as well as the string format ability. For example, I could use the following along with argument place holders ({0}, {1}, etc)

So the first step will be to use NuGet to install log4net. Then we need to write our Log4netTraceListener class:

Once we have that, we can configure both <log4net> and <system.diagnostics> to work together:

And that’s that!  Now any reference you make (for example a class library named “LibraryWithoutDependencies”) can use the Trace class and you can control the logging level using the application configuration file under <system.diagnostics><trace><listeners><add><filter initializeData> attribute as shown in the above example using the value “Information” – while log4net is left at the DEBUG level so everything will output. You can simple change Information to Warning when you release to production and modify when you need to troubleshoot.

So if our combined code-base consisting of a DLL named “LibraryWithoutDependencies” as well as a Console application named “Tracing” with our custom Log4netTraceListener, the code looks like the following. Notice how LibraryWithoutDependencies does not have any reference to log4net, it simply uses Trace.TraceError() in the exception handler.

When we run the application, there are a number of exceptions and our log file is written out to the logs folder in our application root containing the following information:

2015-12-12 14:51:15,952 [9] INFO  - Starting Main() function in Tracing.Program
2015-12-12 14:51:15,969 [9] WARN  - Remote service endpoint not found. Working offline.
2015-12-12 14:51:17,644 [9] ERROR - What could possibly have gone wrong? Seriously, who would call this method...much less instantiate this class?!,    at LibraryWithoutDependencies.Wtf.Go() in LibraryWithoutDependencies\Wtf.cs:line 8
2015-12-12 14:51:17,652 [9] ERROR - Query for an existing customer resulted in an Id value less than or equal zero. Value was 0.
2015-12-12 14:51:19,652 [9] ERROR - Exception caught while processing stuff: Customer Id not found.
Parameter name: id,    at Tracing.Program.Main(String[] args) in Program.cs:line 28

The source code is available at the following URL if you are interested:


1 2 3 5
Page 1 of 5