Great Free HTML Editor for the Mac

Although I have searched several times with no luck for a good HTML editor for the Mac, today I decided I was done using TextEdit.

Here is my criteria for a "good" HTML editor.

1) The editor must provide some semblance of context highlighting. Just displaying the text is not good enough.

2) The editor gets extra points for some of the features I have come to expect from using the Microsoft toolset--automatic completion of closing tags when I type the opening tag, outlining and the ability to collapse sections, and syntactical popups of available tags. Auto-identing would also get high marks.

3) Support for CSS.

4) Also, a huge bonus if there is some easy way to preview the output.

5) The tool MUST be free.


This all seems like a tall order, doesn't it? Especially when you add in #5.

Well, friends, there is a tool that provides all the above and then some. Navigate your browser over to Aptana and download Studio. You can either download the standalone tool (around 85M) or the Eclipse plug-in if you have already downloaded Eclipse to build apps for the Android phone.

Don't wait! Download it now. This is an unbelievable tool. And did I mention it's free?

Using DateTime.ParseExact to load a DB2 timestamp to .NET

It would be nice if the only database you ever needed to access from .NET was SQL Server, or something similar to it, that .NET understood and worked with easily. One of my clients makes extensive use of DB2, though, and not a recent version--a version old enough that there is no LINQ provider. So, part of my job is working around some of the oddities of pulling out of DB2 and sometimes writing the result into .NET.

Recently I had to pull a DB2 timestamp and save out something like a timestamp in SQL Server. As you probably know, the SQL Server timestamp is not a timestamp as all, at least not like DB2's. So when I needed to pull the timestamp from DB2 and get some semblance of a timestamp in SQL Server, I needed to convert to a DateTime in .NET.

Luckily, DateTime.ParseExact was there to save the day. DateTime.ParseExact provides methods to allow you to describe in detail the format of the value you will be passing in, and have the resulting DateTime parse out the string exactly as you describe it.

The solution is pretty simple (assuming dateField is the input string with the DB2 timestamp):


Dim cultureInfo As New CultureInfo("en-US")
Dim dateTime As DateTime = DateTime.ParseExact(dateField, "yyyy-MM-dd-HH.mm.ss.ffffff", cultureInfo)


Note the ".ffffff" at the end of the format string. This allows you to specify fractions of a second, which will be coming in on the input string. Unfortunately, this information will be stripped off, and you will lose the fraction part. But at least you now have a DateTime you can save successfully into SQL Server!

Good luck out there!

Zero to e-commerce in 2 days with CRE Loaded

If you have done any amount of freelance web development, you have probably been asked at some point to put together an e-commerce store front for someone. Most likely, the request also contained some caveats--like, the customer was just getting started, and wanted a store front for cheap, and they wanted it to be up pretty quickly (i.e. no long development cycle, with a minimum to the requirements gathering process), and they wanted some kind of admin tools so they did not have to contact you any time they wanted something simple like a price change.

As a custom build, this would be nothing short of impossible. Hopefully your next thought is to use an existing framework and customize it, to accomplish the goals and get something up quickly and cheaply. Next, you are also thinking about hosting, which can be expensive too, depending the requirements of the tool you buy.

I was presented with a similar request lately, so I started looking at some existing frameworks. My research took to through such disparate tools as DotNetNuke (of course the Microsoft guy would look at DNN) and MamboCharge, but I landed on CRE Loaded. CRE Loaded is a PHP script that has been developed on the popular OSCommerce framework. And the results are amazing.

Some of the best parts of CRE Loaded are the multitude of plug-ins available (including secure credit card processing), the availability of resources (both people and web content that can assist), and the easy and speed of getting a store up and running. CRE Loaded brags about how quickly you can get a Payment Card Industry (PCI) compliant store up and running, which promises secure processing in an industry standard framework. And it is every bit as easy as they say.

I recently got a store up in just a few days. I spent additional time tweaking the product, and learned a lot of the ins and outs. Let me tell you that if you plan to do any serious customization, or even maintain an inventory of even 100 items, you owe it to yourself to learn how to make changes in the SQL tables directly. I am sure CRE Loaded would rather you would either use their front end, or their custom load process--but if you know what you are doing, you can turn a difficult update across your entire inventory into a few minutes work.

Case in point, I was setting up shipping for the store, and discovered that shipping requires product weights. The inventory list that I had gotten from the customer's POS system was missing a lot of things, not the least of which was shipping weights. So I had 3 options--go through the admin screens and add a weight to every item (probably close to an hour's work); extract the database to a spreadsheet, update the spreadsheet, and then reload the spreadsheet, praying that the only thing that would be changed would be the product weight (probably 15 minutes work, plus an undetermined amount of time to fix anything that might have been broken due to a typo, etc); or, write a SQL query to set every product weight to 1 pound (1 minute's work).

If you are PHP person, you can also dig into the entire source, and tweak the application. This is not for those lacking caution, though, because it is a somewhat complex script, so I would not advise hacking into to it to make changes--spend some time first and figure out what it is doing. Another example, I had to make some changes to the shipping module to allow various flat rate shipping types, selectable by the customer. Currently, CRE Loaded expects that you will either use one flat rate, or chose from a number of different available shipping options. The moral of the story, though, is that will a little analysis, even this was a fairly easy change, and I could customize the app to meet the business' requirements.

CRE Loaded offers a free version that is a bit limited, but the pro version does a lot and only costs the equivalent of a few hours' work. In am impressed with the speed, ease, and flexibility. If you need to get a storefront up, make sure you include it in the list of products you research. It is worth every penny!

CRE Loaded Automated Load

I am working on a project right now that is quite a challenge--making CRE Loaded work with an automated load from a separate POS system. CRE Loaded is a tremendous tool, but I don't think it was intended to provide automated capabilities to tie into an existing POS system.

For those of you unfamiliar with it, CRE Loaded is a free, PHP-based storefront. You can find out more here. CRE Loaded is based on the popular OSCommerce platform, and is a great way to get an appealing and flexible storefront up on the web quickly. In addition to the free version, there is a paid Pro version that provides additional capabilities and features.

Generally, I have been very happy with CRE Loaded, but the loading process, called Easy Populate, is lacking. A few of the things you would expect to be there (automated loads, additional fields for the loads, and complete refresh of your entire store inventory) require stepping up to the Pro version, or are not available at all outside custom development. I believe the automated load of the store inventory is going to require hand coding of a CRON job to script out backing up the database, clearing it, and loading from a new import file.

This is a bit of surprise because CRE Loaded seems to handle the vast majority of the desired functionality simply and easily, right out of the box. Personally, it seems like a pretty common feature to require handling a load from a POS system or other external system. Because of that, I am surprised at what seems like anemic capabilities to support load imports.

That said, CRE Loaded is generally a great product, and the ability to write custom code as needed will even mitigate this issue. If you need to set up a storefront, I would put this at the top of your list to review. Just be sure you either don't need to handle external inventory, or can invest in custom code to fulfill this requirement.

Good luck out there!

Default image not showing up for iPhone app

I ran into an issue last night trying to set up the default image for the app I am about to submit to the AppStore. I have learned the first thing to look at is the capitalization when adding files to an XCode project. But there were a few other things I needed to do to get this working.

So if you don't see your Default.png image showing up, try these steps:

1) First, make sure your "Default.png" is named exactly that ("default.png" will not work).

2) Second, make sure Default.png is in the root directory of your project.

3) Do a clean of your target.

4) Then build and your default image should show up.


I also noticed that running this in the simulator was not helpful because my app started very quickly and the default image only displayed for a second. So, make sure you give this a try on the device to see how long the default image really displays.

Good luck!

iPhone / Objective-C Code Examples (and .NET equivalents)

I have found myself reusing a lot of the same code throughout my apps. I thought I would publish some tips and code snippets to hopefully get you going on your next project. Here they are, in no particular order. I also tried to put a .NET equivalent for those of you who, like me, are coming from a long .NET background.


1) Use NSLog liberally to debug as you write your code. You can really do a lot with it. A few standard statements are for printing strings or numbers, and you can also print an Objective-C object. Here are the three I use most often:


NSLog(@"%@", aString); // prints any NSString, for example
NSLog(@"%d", anInt); // prints a signed int
NSLog(@"%f", aFloat); // prints a float



2) If you have a UITableView that you need to have updated, you can reload it with this command (assuming it is named "tableView"):


[self.tableView reloadData];



3) Need to iterate through a collection? Try this handy code snippet, which is similar to For Each in .NET languages:


for (id currentObject in allObjects) {
// do something here with currentObject
}



4) Here is how you can check to see if an object is certain type. This is a bit like using gettype or typeof in .NET to find a specific type:


if ([myObject isKindOfClass:[TheClassIAmLookingFor class]]) {
}



5) Here is how you cast an object in Objective-C, like using CType or DirectCast in .NET:


TypeToCastTo typeCastObject = (TypeToCastTo *)myObject;



6) I show alert pop-ups a lot in my apps. Here is a simple example for an alert launched when a button is clicked, and responded to to perform some action.


-(void)resetClicked:(id)sender withEvent: (UIEvent *) event {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reset" message:@"Are you sure you want to reset this puzzle?\nAll of your work will be lost!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert show];
[alert release];

}

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked the OK button
if (buttonIndex == 1)
{
// reset logic goes here

}

}



7) Split is a great function in .NET. Here is the Objective-C equivalent:


NSArray *arrayOfValues = [commaDelimitedListOfValues componentsSeparatedByString:@","];



More code snippets next time! Good luck!

Update your AdWhirl SDK to 1.2.7 (and Include the AddressBook Framework)

If you have not recently updated you AdWhirl SDK to 1.2.7, you may notice some big benefits if you do. I was seeing leaks before the upgrade, and a few random crashes. Both of these seem to be much improved, but also there seems to be a speed improvement as well.

There is one caveat--when I replaced my 1.2.5 SDK with the 1.2.7 SDK, I started getting all kinds of compiler errors. I sent a note to AdWhirl support (which responded really quickly--thanks!) and they said you need to include the AddressBook framework into your app because it is by AdMob. I did not see this anywhere in the documentation, and of course, I did not expect it simply going from 1.2.5 to the more recent version. It is a 10 second fix, but I wanted everyone to be aware.

Good luck!

My Favorite iPhone Development Books

OK, so the next most common question I am asked after blogs that I follow: what are good books that I can recommend. Here are some really popular ones right now. Books are a great way to get up to speed quickly. If you are getting started with iPhone development, this can shave weeks off your time to deliver a project.

Beginning iPhone 3 Development: Exploring the iPhone SDK


Are you a programmer looking for a new challenge? Does the thought of building your very own iPhone app make your heart race and your pulse quicken? If so, Beginning iPhone 3 Development: Exploring the iPhone SDK is just the book for you. Updated and revised for iPhone SDK 3, many of the discussions in the original book have been clarified to make some of the more complex topics easier to understand. In addition, all of the projects have been rebuilt from scratch using the SDK 3 templates.



Programming in Objective-C 2.0 (2nd Edition)


From the author of Programming in C, Programming in Objective-C 2.0 provides the new programmer a complete, step-by-step introduction to the Objective-C language. The book does not assume previous experience with either C or object-oriented programming languages, and it includes many detailed, practical examples of how to put Objective-C to use in your everyday programming needs.


iPhone SDK 3 Programming: Advanced Mobile Development for Apple iPhone and iPod touch (Wiley)


Apples iPhone is the hottest mobile device on the planet. More than one million iPhone 3G phones were sold in the first three days of release and millions more are sure to be in the hands of iPhone fans each year. Apples iPhone SDK has been updated and includes more than one thousand new APIs that developers will want to get their hands on.

How To Make An iPhone App


Do you want to start developing iPhone apps, but find the technology confusing? Here is the ALL-IN-ONE iPhone development resource that will give you everything to need to make an iPhone App!

iPhone App Secrets - Making Money With iPhone Apps

The mobile phone application market has exploded recently. Never before in history has it been so easy for you to tap into a ready made army of cash rich customers! Right now, there are around 20 million iPhone owners. These iPhone owners spend upwards of $50 million dollars every single month on new iPhone applications.

iPhone Blogs That I Follow

I get the question now and then about the blogs that I follow to keep up with what goes on in iPhone development. It seemed like a nice thing to provide links to this since all of these resources have been helpful to me. So, in no particular order, here are some great iPhone development resources. Enjoy!

  • iPhone Developer/Development Blogs | Travis Dunn - Imagine my surprise when the internet failed to produce a select list of iPhone development blogs more than 10 links wide. Well, I've beaten the internet at its own game and collected the links myself, like a neurotic SERP.

  • A More In Depth Look At The How To Make An iPhone App Book - ... where you should have no trouble figuring out how to use those libraries on your own. You can get more information on the How To Make An iPhone App homepage by clicking here. ©2009 iPhone Development Tutorials and Programming Tips. ...

  • creating a calander - iPhone Dev SDK Forum - I want to create a calendar. which is can be scrolled vertically and contains dates of the next seven weeks. I am new to iphone development therefore.

  • iPhone Game Development With Core Animation | iPhone Development ... - I've made quite a few posts about OpenGL ES, and the available game engines on the iPhone, and I'd like to iterate that you do not need to use OpenGL ES or.

  • FurdLog » Messing With Developer Infrastructure Conventions - By creating games and other programs for the iPhone, software developers hoped to find millions of new customers. But they didn't expect to feel muzzled. The software development kit that Apple Inc. distributed to programmers bound them ...

  • WordPress for iPhone + Source Code Available! < A Fool's Wisdom - Earlier this week my Automattic colleagues and Effigent released WordPress for iPhone and iPod Touch. Now, the source is also available and Trac is set up for reporting bugs and participating in development! ...

  • The iPhone App Store is Pure Genius - Apple's iPhone App Store allows third-parties to release software for the iPhone (built with an API provided by Apple) to all iPhone users. They can charge what they want for the app, and delivery and payment is managed beautifully within the iPhone interface through your iTunes account. By providing the medium (iPhone) and the delivery service (App Store), Apple has created a completely self-sustaining business model that generates passive income for them and increases demand for the product with each awesome release on the App Store.

  • Top 15 iPhone Web Development Resources - Here is a list of links to the best available iPhone development resources such as simulators, development plugins, wikis, and other JavaScript/HTML/Safari documentation. iPhoney – A free iPhone web simulator for designers. ...

  • How Much Is Too Much For An iPhone App? » Webomatica ... - Just tossing this out there, since I have come down with a case of iPhone app price sensitivity.

Object disposal order in .NET

I got a crazy error yesterday while refactoring my data access layer for a client. I wanted to provide a mechanism to get the output parameters from a stored procedure call. I typically use delegates or events to give the developer the option to get the output parameters if they want them.

So I made a change to the order of events to dispose of the connection earlier (rather than hold onto it longer than necessary--"acquire resources late and release them early," as Microsoft says). My pseudocode looked something like this:

...
Close and dispose of connection
Call output parameter access callback
Dispose of command object
...


I was getting back my data, but I started getting an unusual error:

System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.


The error referenced the following:

System.Data.Common.UnsafeNativeMethods.IAccessor.ReleaseAccessor(IntPtr hAccessor, Int32& pcRefCount)


As you might guess, I did not suspect the order of object disposal causing this, so I spent a lot of time looking at other potential issues before nailing this down. I believe there is probably a solution via closing the command's underlying connection rather than destroying it without it realizing. Unfortunately, I have not had time to tinker with this yet.

If you run across this error, try the simple solution. Try closing and disposing of your connection after the command object is closed.

Good luck out there!

Use a Dynamic Data Web Site for simple database table administration

Since its release last year as part of the ASP.NET Extensions (and its subsequent inclusion in the 3.5 framework with SP1), the Dynamic Data Web Site has been a fantastic way to simply and easily develop administrative pages for database tables literally without writing a line of code. But recently, I had a client who wanted to take one of these sites that I generated, and change it. They wanted the look and feel more like their standard apps--I assumed this would be pretty easy. But they also wanted to limit and modify some functionality--take away the edit and delete function, for example, unless the user explicitly had access to maintain data. I was reticent to commit to the cost on this because I had no idea how hard or easy this would be.

To my surprise, the site generated by the Dynamic Data Web Site is amazingly flexible, clear, and easy to configure. There is a master page and css class included with the site. As you would imagine, but replacing the master page in the site with the client's standard master page, and by changing the colors and styles in the css, I had the dynamic site matching the client's standard look and feel in minutes. To do this, look at the DynamicData/PageTemplates folder. There are 5 aspx pages that you will need to modify. Change the master page to point to the standard one. Then rename the content section to the master page's content body, and maybe (as I did) move some content into new content sections to rearrange the page a bit. It was that easy.

But what about the security functionality? My client uses a well-designed page inheritance hierarchy that exposes security information. I was able to go into the code behind for the aspx pages I mentioned above and change them to inherit from the client's standard base page. This allowed me to use logic in the aspx pages to filter out functionality I didn't always want to display. For example, I would wrap the Edit buttons on the list page in an if block like this:


If CanPerformAdminFunctions() Then
...
End If



In short, I was able to filter out any and all functionality from users that I wanted to be able to see the data but not edit it.

On a more detailed note, we also had an issue with bit fields in the database--sometimes we had nullable bit fields. This means the standard Yes/No check box display for a bit is not sufficient; you need a tristate control. The easiest solution is either a drop down or radio button list. Either way, this change was easy to manage--just swap out the control in FieldTemplates/BooleanEdit for a new control and add in some logic to set the field value to nothing if it is not set.

All in all, I have been extremely impressed with the ability to change, edit, and extend the functionality of the Dynamic Data Web Site. This is a great tool to have in your tool box, whether you need a quick and dirty admin site, or you want to start with a simple site and extend it to meet your needs.

Good luck out there!

Using the UITableView, Part 1

I approached the UITableView for iPhone development with some trepidation. It looked pretty complicated to get up and running--I assumed I would be spending a significant amount of time getting it working. To my surprise, I found adding this to my app, configuring it, and adding functionality to it have been remarkably easy.

If you are a novice, you may still be learning to get a view transition to occur when a UITableItem is clicked. Here is code that you need in the tableView:didSelectRowAtIndexPath: method. This assumes you have a NIB named "PageTwo" and that its class is named pageTwo.


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

pageTwo *anotherViewController = [[pageTwo alloc] initWithNibName:@"PageTwo" bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];
[anotherViewController release];
}


Now you can transition to a new page, any new page, when a record in the UITableView is selected. Note that this can be animated, so you immediately get the great sliding animation effects standard in all iPhone apps.

Consider adding buttons to the Navigation bar at the top. I found some complicated examples of this, but Apple provided some good direction to keep this simple. This is all I needed to do to provide a button on the right side of a Navigation bar. I put this code in my viewDidLoad method:


UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:self action:@selector(myFunction:)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];



For my next trick, I will demonstrate how to use your controller class as a delegate for the UITableView, so your class can respond to calls from the UITableView, allowing you to populate the class on the fly. See you next time!

John

Load and Parse a File on the iPhone

Right now, I am hard at work on my next game. It is a puzzle style game, and I am using a variety of different tricks to pull it together. One of which is using my .NET skills to generate the actual puzzles on the Windows platform, then transfer them and use them on the Mac. I had to do some computations that I knew how to do easily on the PC, so I decided to use those skills (read: take a shortcut).

Anyway, this means that I had to be able to read in the file once I got it over to XCode. Luckily, like most things, there are an impressive amount of examples to do the load....assuming you don't need to parse the results and pass them into a custom object.

The format of my file is pretty simple: I am using an identifier for a node which is a string, then I am using 3 floating point values for the x-, y-, and z-coordinates of a point in 3-dimensional space. So I need to be able to read in the rows from the file, and parse out the results that I can use to create these custom nodes.

It was pretty tedious getting there, because the main method to parse out delimited strings, componentsSeparatedByString, returns types that you might not expect. Coming from a non-C background, I was slowed down by having to convert each node to something to be able to use it. I am a .NET guy...I expect some implicit conversion!!

Hopefully this bit of code helps you. This will load in all the lines in your string, parse out the values, then create a custom object for each one. Some of the supporting code is left out, so you can see the meat of the example; I also converted this to loading ints instead of floats because it made the code a little simpler. Enjoy!


NSString *filePath = [[NSBundle mainBundle] pathForResource:@"nodeOutputFile" ofType:@"txt"];
NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSASCIIStringEncoding error:NULL];
if (fileContents) {

NSArray *lines = [fileContents componentsSeparatedByString:@"\n"];
for (int i = 0; i < [lines count]; i++) {

JTNode *newNode = [JTNode alloc];

NSArray *tokens = [[lines objectAtIndex:i] componentsSeparatedByString:@","];

NSString *identifier = [tokens objectAtIndex:0];

NSNumber *x = [NSNumber numberWithInt:[[tokens objectAtIndex:1] integerValue]];
NSNumber *y = [NSNumber numberWithInt:[[tokens objectAtIndex:2] integerValue]];
NSNumber *z = [NSNumber numberWithInt:[[tokens objectAtIndex:3] integerValue]];

[newNode createWithX:[x integerValue]
Y:[y integerValue]
Z:[z integerValue]
Identifier:identifier];


// do something here with newNode

}

}



Good luck to you!
John Tabernik

Load SQL Server from .NET Much Faster Than With Insert Statements

SQL Server is a developer's friend--it is everywhere, it is easy to use, there are tons of resources available for it, and you can distribute a free version with your apps. Odds are SQL Server is either your primary database platform, or you have some data in it and work with it fairly regularly.

One of the most common questions I hear is: "How can I get data into SQL Server?"

Most of the time the answer lines somewhere between creating insert statements for a few records or developing a DTS package to handle a file import. But every client has different needs, and sometimes you run across a situation where you need a different tool in the toolbox.

A client I work for needed a robust solution to get a lot of data into SQL Server while providing detailed reporting on the process, high availability of the database, and a good deal of fault tolerance. Due to some environmental issues, DTS was not feasible. As a result, I needed a fast way to get a lot of data (hundreds of thousands of rows per load) into the database with essentially just .NET.

Luckily, I happened on the SqlBulkCopy class in System.Data.SqlClient. SqlBulkCopy abstracts the BCP utility that has been around in SQL since version 7. The best part--this class, via BCP can bypass standard SQL overhead like logging, triggers, etc. When loading the data without executing any constraints is acceptable--i.e. when you really just want to bulk load the table--SqlBulkCopy is unbeatable.

The process couldn't be simpler. You just need to load a datatable with data, create a SqlBulkCopy class, set up the column mappings, and execute the copy. Loading up the datatable is a great method because there are many, many ways in .NET to get data into a datatable natively, and you can get data there by hand if necessary (manually adding rows).

Here is some example code. Pass this method a datatable, set up the connection string, and you will get great performance!


Private Sub BcpRecordsToSqlServer(ByRef MyTable As DataTable)

Dim s As New SqlBulkCopy(SQLConnectionString, SqlBulkCopyOptions.Default)

s.DestinationTableName = "MyDestinationTableName"
s.BulkCopyTimeout = 36000

For Each field As DataColumn In MyTable.Columns
s.ColumnMappings.Add(field.ColumnName, field.ColumnName)
Next

s.WriteToServer(MyTable)
s.Close()

End Sub



A few notes about this code. First, you can set a property on the class initialization to determine if triggers will fire or not, etc. Second, you can set a time-out on the copy. Third, I am simply looping through the column in the datatable and setting them to output to the same column in the target table--assuming that you set up the input columns to be named whatever the target table's columns are. This is nice because you only have to populate the fields you want in the destination table.

This process is great for reading chunks of data and uploading them to SQL Server. I originally ran a process pulling data and uploading via individual insert statements. I stopped this process after 6 hours. In contrast, using BCP I was able to upload 100,000 rows in under 10 minutes!

Clearly insert statements versus BCP is an apples to oranges comparision--if data integrity is not certain from your data source, you may find BCP if the wrong choice because constraints are bypassed. These statements are not logged, so you cannot recover them in a partial back-up or log shipping situation. Also, you cannot run the BCP load in a transaction.

But, if you are able to scrub the data first, or even scrub the data as you load the datatable, or if you are certain the data is acceptable, you cannot beat the raw speed of using this process to insert into SQL Server from .NET.

Under certain circumstances, this is an invaluable tool to have in the toolbox!

Good luck!
John Tabernik

Put AdWhirl Ads into Your iPhone App

Monetizing an iPhone app comes in many flavors these days--you don't just need to charge for your app. Today I want to give you a step-by-step guide to getting ads into your application. You will be shocked how easy it is!

When I decided to release a lite ad-supported version of my app, I searched around to find out about ad providers. Luckily, in my search, I happened upon AdWhirl. AdWhirl is not actually an ad provider--instead, they provide an interface to other ad provider networks, allowing you to add one ad API into your app, and get 8 ad providers instantly (including the ability to put in your own ads).

AdWhirl has outstanding step-by-step instructions on their site here. I was trying to incorporate Quattro Wireless into my app and was having some issues, not the least of which was the lack of cut-and-paste of example code. I was searching on example code when I found AdWhirl, which includes Quattro Wireless as a provider. And AdWhirl made it easy for me to set up my ads, so it was a no brainer. [Update--I have received word from Quattro Wireless that they do provide cut and paste code in their developer wiki. My mistake--I guess I did not look in the right place.]

But now, let's look at some code. For my sliding tile game, I used the cocos2d framework. I was worried this would make it hard to incorporate in AdWhirl, but it was no problem at all! Take the steps in the instructions document above, then do a few simple steps to incorporate AdWhirl into your app:

First, I recommend adding a few constants into a common header file:


#define PUBLISHER_ID @"12345678901234567890"
#define SITE_ID @"MyApplication"
#define ADWHIRLAPPKEY @"1234567890abcdef1234567890"



These are the constants needed by the AdWhirl interface.

Include these variables in your scene header file:


ARRollerView *rollerView;
BOOL isPaused;



Next, I went into my main Scene file and put the following at the end of the init method:


rollerView = [ARRollerView requestRollerViewWithDelegate:self];
[ARRollerView startPreFetchingConfigurationDataWithDelegate:self];

rollerView.center = CGPointMake(160, 454);
UIView *myView = [[Director sharedDirector] openGLView];
[myView addSubview:rollerView];

isPaused = FALSE;


This code will begin prefetching data for ads. Also, you will notice I specifically placed the ads at the bottom of the view--that took some trial and error to get it to line up perfectly. isPaused is going to be the notifier to pause the game timer while an ad would be displayed if the user clicks on one during game play.

Now, your scene will need to provide a method to return your app key:


- (NSString*)adWhirlApplicationKey
{
return ADWHIRLAPPKEY; //Return your AdWhirl application key here
}



Also, add these two methods to pause and unpause the game if you have a game timer:


- (void)willDisplayWebViewCanvas {
isPaused = TRUE;
}
- (void)didDismissWebViewCanvas {
isPaused = FALSE;
}



You will also need to add some code in your timer method to pause timing if isPaused is true. For example, I have a tick event, which I bail out of immediately if isPaused is true.

I was also getting bunch of errors until I added a special line in the dealloc, to set the ad delegate to nil before you release it. So don't forget this part!!


-(void)dealloc{

if (rollerView) {
[rollerView setDelegateToNil];
[rollerView release];
}

.
.
.



And that's it! Hopefully using these few code snippets, you can avoid some of the learning I went through and get ads into your apps in less than an hour.

Obviously, you also need to go through the process on the AdWhirl web site to set up the ad providers, etc, but this is pretty simple. There can be some lag time for some of the providers waiting for codes and things to be emailed to you, but it does not take much of your time to submit the requests.

I hope this is a pretty complete overview. If you have questions, please feel free to send me a note. Good luck!!

Get Quality Backlinks Today

In addition to standard PPC, I have spent a lot of time trying to build quality backlinks to my marketing sites. This is a very difficult and time consuming process. Anyone who has tried to drive more traffic to their sites knows...you can spend an hour a day for months before you get any valuable traffic.

But what if you don't want to wait? Well, I have discovered a great resource in AutomaticBacklinks.com. They are easy to use, easy to set up, and deliver high-quality traffic to your site quickly.

AutomaticBacklinks.com is like most of the back link services in that it offers backlinks in trade for either cash or other links. However it differs in terms of its great pricing and the speed with which you will get traffic. I have tried several of these services and tracked the impact to my volume in Google Analytics. Two things that impressed me...that both the volume was higher, but more importantly, that the quality of the traffic seemed much better. The traffic I got from AutomaticBacklinks really seemed to be *reading* my content, not just finding it. That is testament to the fact that these guys have built a better process than their competitors.

If like me you have some sites you want to get into the limelight--there is no faster way to get it that AutomaticBacklinks.com. Give it a try and see if you don't agree!

Ask Uncle Boris, Live in the AppStore

iPhone/iPod Touch application #3, Ask Uncle Boris is live in the AppStore. This app is not very technically challenging, but it was fun to make and helped me learn a few more things about iPhone development. This was my first app using xib files and standard views. No cocos2d was needed for this one. I am however working on a new app that makes extensive use of TileMapAtlas--it is really easy to use and I cannot believe how quickly the app is coming together.

Here is a demo video I made about the app. You can also look at my marketing page for the app, AskUncleBoris.com.

The Wild World of AppStore Applications

It is really an emotional rollercoaster--waiting and refreshing your email constantly looking for a reply. Sometimes you get rejected. But it is all worth it for those golden moments of approval.

I am of course talking about submitting apps to the Apple AppStore. A quick process for me so far, getting your code ready, submitting, and waiting for approval is both exciting and nerve-wracking. I have had two apps approved, one taking five business days, the other taking ten. Neither were rejected, so I count myself in the lucky category. I have friends who have had apps waiting months for approval. A scary thought!

I currently have two other apps waiting for approval. One got rejected this morning, but it was criteria around a free "Lite Version" of one of my paid apps (apparently you are not allowed to upsell in your Lite Version). It took about 15 minutes to modify and resubmit. We will see what happens!

A word of caution if you have never submitted an app before--the first one takes time! And I am not even talking about the approval process. I spent a few days getting my app compiled and signed, getting my icons the right size for the AppStore guidelines, blah blah blah.... The second one took a few days as well, but since then, I have been submitting in minutes. If you have issues you can't get around, feel free to shoot me a response to this post--I may well have stumbled on the same issue.

If you are interested, I have a demo of one of my apps on YouTube. Enjoy!

My Foray into iPhone Development

OK, not many posts lately. I have been very busy with my new Mac. My goal was to learn enough Objective-C / Cocoa Touch / etc. to be able to build some iPhone apps. Shockingly, I was able to get an app up and into the AppStore very quickly thanks to the strong developer community with tons of examples and great documentation. Check out Spin the Bottle '09 on the AppStore here.

Apple also deserves a big hand I think for having built a solid platform and great development tools. I have been a PC guy since 1984 (I started developing when I was 13 years old), and I never expected to turn away from PCs. But I have to tell you, the Mac is astonishing in terms of performance, stability, and usability. The best part--no anti-virus software!! If you have ever thought about trying a Mac, I would highly recommend it. The best part--you get to right cool stuff for the iPhone and iPod Touch.

I also want to quickly thank a colleage, Dave Ferrell. Dave showed me around OS X at the Apple Store at the end of last year and his enthusiasm was contagious. I had been intrigued by Macs since they first came out in 1983, but after that day, I had to get one!

I will post more over the coming months about iPhone development on the Mac, but unlike my PC work, I am going to be primary focused on game development for the iPhone. If you haven't heard, killer apps in the AppStore can hit hundreds of thousands of downloads a month. This could bring me closer to my goal of having a few million people give me $1....

File-Based Caching, Part 1

There are a variety of caching methods available to you when writing a web app. I am sure you have used session and the intrinsic .NET caching component to cache data. But there are times when neither of these solutions is the right solution.

Let's consider when session and caching are most valuable. Session is extremely useful for user-specific data that is needed across a variety of applications. (I will assume that you are in a web farm environment, and therefore storing your data in the native SQL-Server session store. Even if you are not, this discussion applies to a lesser degree.) Since it is retrieved and stored on every page transition, it is typically good to keep values stored in it small. Sometimes I store large data elements there, but I try to keep that to a minimum, and only to data that must be access all the time.

Caching is also a great mechanism, highly efficient and proven to work well. But think about the usage of it--if your all of individual users will have their data cached there, you will be eating up a lot of precious web server memory. Compound this by the fact that each web server will need to cache the data, meaning that whatever process is needed to create the data, it may execute once for each web server you have. If this process is expensive, you haven't gained a whole lot when different servers all need to process the same request. Finally, however much memory is used per user, you are eating up with memory on every web server. The result is a large expense.

Caching is very valuable when the data is not user specific. If you have configuration settings, for example, or lookup values or whatever that can shared across all users, caching is a great approach. Not only will you significantly reduce the time to fetch the data, but your load on the infrastructure (for example if the data is being retrieved from a database) will be reduced as well.

Now let's consider a common situation that is not a good fit for session or caching. Often, data for an application that a user would retrieve is specific to the user. Maybe some other users would get the same data, but it is not enterprise-wide. Let's also suppose that it is expensive to materialize this data initially--a common example is executing a SQL query that might require several seconds to run. You know you should cache this, rather recreate it each time. Session is not a good candidate because the results coming back are large--it would be too inefficient to save and retrieve on every page transition, and it is specific to one app anyway (i.e. you don't want this data following the user around for the rest of their session).

.NET caching is not a good option because (1) we don't want to re-execute the expensive query potentially for each web server, and (2) the data is large and each user's data will be different, meaning you will be eating up a lot of memory every machine.

I recently wrote a file-based caching component to deal with this exact problem. I created a shared directory that can be accessed by every web server, and I write a file there with anything I want to store and retrieve. The process uses a BinaryFormatter to take any object is input, and be able to serialize it and deserialize it. Plus, I compress the BinaryFormatter results to shrink the size of the file written and reloaded.

Results have been impressive. Expensive requests can now be cached, and the repeated calls to the pages that use them are lightning-fast. Best of all, the solution is completely generic, so I can easily cache results from a web service call, a database call, or custom objects with no additional work on my part, thanks the BinaryFormatter.

Next time, I will provide some code and walk through usage of the component. I will also explain the use of expiration, which is a critical requirement to any caching mechanism.

Create a Custom Trace Listener, Part 1

Visibility into components can be a real challenge in a web application. Often, there is functionality in a low level component that we need to trace because it is misbehaving or throwing a new error that it didn't before. Typically some kind of scaffolding needs to be created to put output somewhere so we can see it. This require work to be done in every component.

A better solution would be to have a lightweight tracing process that can be incorporated into a solution easily, preferably with minimal coding. Since this is such a universal need, Microsoft has provided a great means to do exactly this through built-in functionality in .NET.

.NET includes tracing capability out of the box in every component through the Debug.Trace function, which can write output into the Visual Studio IDE. This is nice for local development, but not much value when your component is running on a remote machine. Thankfully, .NET allows us to hijack the output of Debug.Trace and do anything we want with it.

Often in a framework, I will have many levels of components, and have a need to debug one small part of one component. Using a custom trace listener, I am able to put tracing informaion into a component, recompile it, and place it in the GAC on the server in question. Immediately, I get the trace output, regardless of where in the many layers of the framework it may live. This is a huge time savings--there is no need to develop custom functions to write or respond to the tracing output, and I have complete control over where the messages go.

Developing a custom trace listener will require the following:

1) Building a self-contained .dll that can be GACed which will consume the trace messages
2) Changing the web.config file of the application to tell it to use custom tracing


I take this a step further, and output the trace messages to a database, and check to see if a component's tracing setting is turned on or off. This allows for turning tracing on or off in an environment with no coding changes. But this requires a few more steps:

3) Set up a database to receive the test messages
4) Set up a mechanism to determine if an application's trace setting is on or off


Next time, I will discuss how to develop a custom trace listener and allow an application to use it. Then I will discuss the steps to turn this on and off in an environment.

Data Access Using Delegates, Part 2

Before jumping into the code for the data access, I want to post a quick overview of delegates for those of you not familiar with them.

Delegates are essentially function pointers that you can pass into a method like a parameter. This delegate is a function the called method can call back to, to perform work, to provide updates, etc.

Consider this example. Suppose I have a method that is a long-running process, and I have a status bar that I want to update. The method could take as a parameter a delegate that will get status updates that could be displayed. The code could look like this:


Delegate Sub ProcessUpdates(ByRef UpdateMessage As String)

Public Function LongRunningProcess(ByRef ProcessUpdatesDelegate As ProcessUpdates)

    ProcessUpdatesDelegate.Invoke("Starting...")
    ' do something
    ProcessUpdatesDelegate.Invoke("Step 1 Completed")
    ' do something
    ProcessUpdatesDelegate.Invoke("Step 2 Completed")
    ' do something
    ProcessUpdatesDelegate.Invoke("Process Completed")

End Function


Next, we would make a call into LongRunningProcess to kick it off, providing the function to call back to to process the update messages. We would do that this way:


Public Sub ProcessUpdates(ByRef UpdateMessage As String)
    txtStatus.Text = UpdateMessage
End Sub

' do this somewhere in the main line code:
LongRunningProcess(AddressOf ProcessUpdates)



That's all there is to it! Notice that the signature of the ProcessUpdates method has to match the delegate signature exactly. By following this straightforward process, we can make a delegate, pass it to a method, and have the called method make calls back to the delegate as needed.

This is exactly the method we will use next time in our data access layer!

Clean Up Your Session Access With Session-Backed Classes

One of my favorite techniques to refactor ugly web code into cleaner, OO code is creating session-backed classes. Often, developers go directly after session values, setting and getting them throughout their web pages. I hate this for a number of reasons--the biggest two are (1) it is very difficult to deal with decentralized session access, i.e. if you need to change a variable name, or manage everywhere a variable is called, it can be spread out all over your site; and (2) if you misspell the variable name somewhere (clearly session variable names bypass compile-time checking), your code will not behave the way you expected.

Session-backed classes free you from this problem, give you a nice quasi-OO wrapper over the session, centralize access to the variables, and even provide intellisense when you are developing against them. Additionally, since creating a session-backed class is a trivial process, you can spend about an hour and create a session-backed class generator to take out the grunt work.

Let's look at the fundamentals you need for a session-backed class:

1) You need a new class that will represent a logic unit (a user, an employee, a sale)
2) You need a constructor to take a reference to your session management; this is optional if you want to assume you can use the current HTTP context
3) You need a list of properties that will be made available by this class
4) A best practice is to use a prefix for the variables to allow the values to be easily grouped, and to clearly show what class created the variables


Now, let's take a look at some session-backed class code. Here is a simple class with one property, making use of the current HTTP context for session management:


Public Class Employee

    Public Property Name() As String
        Get
            Return HttpContext.Current.Session(Prefix & "Name")
        End Get
        Set(ByVal value As String)
            HttpContext.Current.Session(Prefix & "Name") = value
        End Set
    End Property

    Private Function Prefix() As String
        Return "Classes.Employee."
    End Function

End Class


That's really all there is to it. Referencing this variable from anywhere else in your application will give you a great OO-style interface into the session:


    SessionValue = Employee.Name


Meanwhile, you are not responsible for any additional code behind the scenes to serialize and deserialize the object, which is perfect for classes that are working with values used by legacy apps that call directly into session (i.e. apps that you can't easily change).

Service Unavailable In IIS 6.0

If you are not aware, IIS 6.0 has a special feature that monitors failures on your web site. If enough errors occur in a certain period of time, your app pool is shut down. The default is 5 errors in 5 minutes.

I have not seen a whole lot of errors that have registered in this category, i.e. enough of an issue for the app pool to shut itself down. Our dev box gets beaten up pretty well with our apps in progress, and today was the first time I noticed the app pool being down.

This is an important thing to know if you notice a "Service Unavailable" error when your page is being served up. It is also a good thing to be aware of, since it is probably the setting on your production and test boxes, if it has not been modified by your admins.

You can adjust this setting or turn it off in IIS.

A special thanks to Elliot Swan for making my job easier today. I just discovered his tool, Postable. Thanks, Elliot!

Reflection and Data Output, Part 1

Reflection is extremely beneficial for outputting data dynamically. Often, we need to be able to change column order or even what displays quickly. Reflection gives us the mechanism to perform late-binding on columns that will be output.

Let's consider an example. Suppose you want to output a table with a few columns--a user's name, country, and ID. My data access layer is going to provide me a list of objects with properties of the same names. I can hardcode my output process to use these names. But what if I expect them to change a lot? Or what if users are going to be able change them themselves? Hardcoding is not an option.

Reflection allows us to solve this problem by reading configuration information and outputting our data columns based on this data.

You will need to add the following declaration:


Private pInfo() As System.Reflection.PropertyInfo
Private PropertyIndexes() As Integer


Then populate this array:


Dim t As Type = MyObjectCollection(0).GetType
pInfo = t.GetProperties


Then set the indexes of the fields to display:


ReDim PropertyIndexes(TotalColumns - 1)
' loop through your array of property names to display
PropertyIndexes(i) = FindPropertyName(PropertyNameToDisplay(i))
' next


Finally, for each of your objects, you will loop through the list of fields to display, using this code to pull the correct fields out of the data access object:


Dim output As Object
output = pInfo(PropertyIndexes(i)).GetValue(MyObjectCollection, Nothing)


Yes, I am throwing a lot at you, and this may not make sense right now. If you are an expert with reflection, you probably see exactly what I am doing. If not, I will dive more deeply in the details next time.

Data Access Using Delegates, Part 1

Data access layer code is almost universally the same--you get a connection string, you open a connection, you execute your query, you turn your data into objects, you do some clean up of objects you created. You have probably written hundreds of components to do this.

One of my biggest annoyances with data access code is that it is usually decentralized, with each DAL opening and closing the connection, creating and destroying objects, etc. Maybe I want to go back and add in logging to each component and be able to turn it on and off. I can't really do that if I have hundreds of tiny DALs. Also, how do I know that all the developers on the project have been diligent about closing and disposing of all their objects? I don't want to have to verify this is true across everyone's components.

These exact concerns led me to a different design approach--one that would centralize the tedious, repetitive pieces into a component that could be easily managed, while providing all the necessary functionality and efficiency to the developers. This is not an easy task because the simplest approach to solve a data problem (datatables, for example) are typically not the fastest.

My requirements were to design a solution that would:
1) Centralize connection string management, so connection strings existed in exactly one place
2) Centralize object creation, and closing and disposing of objects, to ensure this always occurs
3) Centralize significant events, such as opening a connection or executing a command, to allow capturing of information for troubleshooting or profiling
4) Decentralize necessary functions, such as adding parameters to a stored procedure call
5) Allow for efficient data retrieval, rather than require an expensive retrieval method to meet the above criteria


Looking at these requirements, it didn't take long to realize that delegates were the best solution. Delegates allow efficient calls to be made to functions outside the centralized code, while providing a means to keep all the important code within a main data access component.

I will discuss delegates briefly, then jump into how you can use this approach to simplify and enhance your data access components.

Hint - Use String.Format

If you are not currently using String.Format, you owe to yourself (and your co-workers) to start.

String.Format changes code from looking like this (in VB.NET):
Dim MyString as String = _
"Value of " & MyVariableName & " equals " & _
MyVariableValue & "."


To looking like this:
Dim MyString as String = _
String.Format("Value of {0} equals {1}.", _
MyVariableName, MyVariableValue)

Let's look at the differences. First, the String.Format route is much more readable. It is pretty clear what the result of the concatenation will be without needing to understand the code. Second, the String.Format approach is significantly less error prone--no more have to recompile because you missed an ampersand, quote, etc.

Plus, your coworkers will thank you. Whoever needs to read or maintain your code after you will save time and be able to make changes more easily. Plus, they may pick up the important skill of using String.Format....

Date Validation in ASP.NET

I don't think ASP.NET uses the most intiative validator for data type validation. I find every time I need to add date validation to a form, I have to go back and look up which validator I need to use (compare validator? range validator?) and what fields to set.

So, for reference, here is what you need to do:

  • Use a Compare validator

  • Set the ControlToValidate to point to the date textbox

  • Set the Type to "Date"

  • Set the Operator to "DataTypeCheck"


  • That's all there is to it!

    Using Web Service for Legacy Data

    Web Services are a tremendous way to encapsulate data access. If I think my data access layer may be hit from multiple types of clients, I create a web service to deliver the data. This ensures easy access, no matter who may want to consume the data.

    One of my current clients has an existing classic ASP site. This site handles the majority of their sales traffic. It has been in place for some time, and with plenty of initiatives for new systems, they have no desire to rewrite it in .NET. In addition to classic ASP, their site contains a number of COM components for data access. So, my goal was to use .NET to develop data access that could be used by the classic ASP and COM components, but allow a transition to new technologies when it made sense.

    Note first that threading can be a big issue when it comes to connecting legacy technologies to .NET. Per Microsoft, you should not have a COM component calling a .NET component directly. This is another way that the Web Service protects you.

    Architecturally, Web Services also give you a great means of load balancing. My client, like any large company, had a good-sized web farm that was handling requests. By exposing the data access layer on HTTP, the server that is most able to handle the request will do so--even if it is not the server that is processing the COM component or ASP page. In other words, a server may actually hand of this request to a more idle server, and both machines have a hand in processing the web page.

    As I mentioned above, Web Services are my tool of choice when it comes to providing data to a myriad of types of clients, so this is the perfect choice in this case. Next, the problem is how to make the connection between the old technologies and a web service.

    Long ago, the SOAP toolkit was the way to solve this problem. But with this technology deprecated by Microsoft, it is clearly not the solution.



    So when I need to connect classic ASP or COM components to a web service, I use a direct XMLHTTP request using the MSXML component. This is a very straightforward process, and setting up the configuration information is largely a matter of copying and pasting information from the .asmx page into your program.



    First, you need to set up a few functions that you will use to make the call. I put the following code in a include file for classic ASP:




    strWSUrl = "http://server/webservices/MaterialRelease/service.asmx"
    strAction = "http://tempuri.org/GetAvailableForecasts"
    strXml = _
    "<?xml version=""1.0"" encoding=""utf-8""?>" & _
    "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" & _
    " <soap:Body>" & _
    " <GetAvailableForecasts xmlns=""http://tempuri.org/"">" & _
    " <SupplierId>" & SupplierID & "</SupplierId>" & _
    " </GetAvailableForecasts>" & _
    " </soap:Body>" & _
    "</soap:Envelope>"

    result = PostWebService(strWSUrl, strAction, strXml)

    Hide a SharePoint Field

    Often for workflows, or to manage documents, it is useful to have a hidden field. I have seen a lot of tricks to do this (programatically add the field as hidden, set up a UI to be able to edit a list's hidden attributes, even changing the javascript on the edit page), but is there a way to do this out of the box, without needing to program?

    Fortunately, the answer is yes, and it is pretty easy. You just need to do the following:

    1) You need to allow management of content types. This option is in the List advanced settings. Make sure this is set to "Yes." Making this change will present additional configuration options in the List settings pages.

    2) Now, find the column you want to hide in the List settings page. When you click on the column, you are presented with options for the column, including the ability to hide the column.


    It's that simple! Now you can create a column for many purposes--I frequently use this feature to hold a status to facilitate workflows. The status can even be displayed in a view if needed, without it being editable in a New or Edit item page.

    Good luck out there!