Posts by Matt Knott

The book that started it all

When I was asked to write Beginning Xcode I initially said no. Mostly this was because of a fear of exposing myself to international criticism – I had enough trouble coping with the local kind, dealing with anxiety issues like imposter syndrome and the like without opening myself up to harsh reviews and the inevitable burning rage that meant my reply would probably be far form professional.

I thought about my decision for a couple of days and then changed my mind – decisive as always. I have a tendency to self-deprecate (after all it’s harder to be mocked for something if you’ve said it first), but I’ve still felt that for someone who wasn’t particularly academic I’ve done really well – mostly because I love what I do and that makes learning easier.

I don’t really remember where my interest in computers started, but I have early memories of my uncle having a ColecoVision back when I was 5 or 6 and that was my first introduction to computer gaming. From there I ended up getting a Spectrum and Commodore 64 (about 10 years after they came out – we were poor) and while the tapes were fun and all, I wanted to make these machines do the things I wanted them to do and for this I turned to books.

One book in particular sticks out in my mind; Sixty Programs for the ZX Spectrum. There were such variety in the applications, and there was no lengthy explanation for why you were doing something it just gave you pages of code and left you to it. My sister and I would sit for what seemed like hours typing in the code on the unforgiving rubber keyboard of the Spectrum 48k, typing feverishly to get to the point where we could press run and hope that each of the 200 or so lines were perfect; they never were.

Yes, the book is as memorable for its infuriating bugs as its brilliant code but that was all part of the charm. Back then there was no popping online to the publishers website for the errata updates, you had to figure things out for yourself – learning by necessity.

I’m writing this post because as I sit outside in the garden (making the most of the sun but struggling to read what I’m writing on the screen) I’m uploading zip files of images for the Swift 3 version of Beginning Xcode and got to reminiscing about how I got here and why. Even today – the era of digital everything, programming and development books have a huge part to play in helping the next generation of coders find their way. I hope the crippling neck pain writing these books has caused me goes on to help another person somewhere make their dreams come true.

So you started your iOS 8 project with a specific size class

When you move from Xcode 5 to version 6 for iOS development, one of the concepts that can be difficult to wrap your head around is Size Classes. Size Classes are great, they allow you to use a single storyboard for multiple form factors and screen orientations, but they can become very complicated as you add more and more class-specific settings and constraints.

[av_image src=’http://www.mattknott.com/wp-content/uploads/2015/03/Screen-Shot-2015-03-02-at-18.43.10-495×400.png’ attachment=’2097′ attachment_size=’portfolio’ align=’right’ animation=’pop-up’ link=’lightbox’ target=” styling=” caption=’yes’ font_size=” appearance=”]
A basic project started in the wCompact hAny size class
[/av_image]

One pitfall that’s easy for developers to make is to start laying out their app using a specific size class, for example an iPhone app may be started using wCompact hAny because the design area in the storyboard takes on a familiar landscape shape, which is less disorienting that the default wAny hAny layout you are presented with for all apps.

The challenge with this approach comes when you’ve set all of your constraints and have your layout working perfectly on a 4-inch iPhone, but when you rotate it or try it on a larger device, it all goes wrong and elements become unaligned, too large or small, and sometimes disappear altogether. The reason for this is that the constraints you set under wCompact hAny will often only be valid when those conditions are met. thankfully there is a straight forward way to get your constraints to be applied in different size classes.

Select a constraint from the Document Outline and then open up the Attributes Inspector. As you will see in the image below, you will have a tick against wC hAny, meaning this constraint is only valid in that layout, but the blank item above it (which should really be labelled wAny hAny) is not ticked. Click this to make the constraint apply across any layout.

Screen Shot 2015-03-01 at 10.02.27

HOW TO: Start a New Xcode Project From a Git Repo

I’m currently putting the finishing touches on the Swift edition of Beginning Xcode, when I thought it would be nice to take a break and do some blog writing instead. Specifically, I realised that Xcode doesn’t make it really clear how you go about turning a url for a Git repository, say https://github.com/MattKnott/Bokeh.git into a new project without downloading it.

It’s actually super easy to do this, and save yourself the hassle of managing the download, extracting files etc.

1. Start by choosing a Git repo that you want to work with, I’m going to use the one from my SpriteKit particle effects tutorial, https://github.com/MattKnott/Bokeh.git

2. Next, Open Xcode, and go to  Source Control > Check out

tutorial_xcode_git_1

 

 

 

3. Enter the url of your Git repo and click Next

tutorial_xcode_git_2

 

 

 

 

 

 

4. Finally, choose a location to create the project and click Check Out.

After a short while depending on the size of the project, Xcode will have your project set up, ready to go.

HOW TO: Write an SQL Webpart

To start with, the tools used. I’m using visual studio 2005, and have the wsssdk installed, the sharepoint 2007 sdk, and the wss 3.0 extenders for Visual Studio 2005.

Start up Visual Studio and create a new webpart. It’ll be called webpart1 by default, lets leave it at that for now. First off, right click the project in the solution explorer, and click add reference. Scroll down and select System.Data. Once you’ve added this we’re ready to begin.

Now, make your code look like this:

using System;
using System.Runtime.InteropServices;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Serialization;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace WebPart1
{
[Guid("493c926d-f885-4965-b72d-ee18e0292630")]
public class WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart
{
private string _sqlStatement = "SELECT * from tblTest";
[WebBrowsable(true),
Personalizable(PersonalizationScope.Shared),
WebDisplayName("SQL Statement"),
WebDescription("Query to return a set of data")]
public string SQLstatement
{
get { return _sqlStatement; }
set { _sqlStatement = value; }
}
protected override void CreateChildControls()
{
if (string.IsNullOrEmpty(SQLstatement) || !SQLstatement.ToUpper().TrimStart().StartsWith("SELECT")|| SQLstatement.Contains(";"))
{
Literal lit = new Literal();
lit.Text = "Only single SELECT statement allowed"; Controls.Add(lit); return;
}

DataGrid grid = new DataGrid();

//Attempt connection
using (SqlConnection conn = new SqlConnection("server=SP-TESTdatabasearea; Initial Catalog=spTest; User ID=sharepoint; Password=password; Trusted_connection=yes;"))
{
SqlCommand cmd = new SqlCommand(SQLstatement, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
grid.DataSource = reader;
grid.AutoGenerateColumns = true;
grid.DataBind();
reader.Close();
conn.Close();
}
Controls.Add(grid);
}
}
}

Now just build the control and your webpart should be working well. Lets take a look at some of the key parts.

private string _sqlStatement = "SELECT * from tblTest";
[WebBrowsable(true),
Personalizable(PersonalizationScope.Shared),
WebDisplayName("SQL Statement"),
WebDescription("Query to return a set of data")]
public string SQLstatement
{
get { return _sqlStatement; }
set { _sqlStatement = value; }
}

Here we have created a webpart property to contain the SQL statement. We set the title and description that a user will see when they edit the page in SharePoint and look at the webpart properties.

using (SqlConnection conn = new SqlConnection("server=SP-TESTdatabasearea; Initial Catalog=spTest; User ID=sharepoint; Password=password; Trusted_connection=yes;"))
{
SqlCommand cmd = new SqlCommand(SQLstatement, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
grid.DataSource = reader;
grid.AutoGenerateColumns = true;
grid.DataBind();
reader.Close();
conn.Close();
}
Controls.Add(grid);

Here we instantiate the connection to the SQL database, bind the results to a DataGrid and display the results on the page. Note the server name SP-TESTdatabasearea has two backslashes, this is to counter the escape effect of a backslash. Alternatively you can use @”SP-TESTdatabasearea.

Using a web.config connection string

On the previous blog, someone asked how to do this using a connection string held in a web.config file. Firstly, create your entry in the web.config like this:


Next, instantiate an instance of System.Configuration.ConnectionStringSettings. You will have to reference System.Configuration for this to work.

ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings["MyDB"];

Finally modify your SqlConnection to use the new object.

using (SqlConnection conn = new SqlConnection(connectionStringSettings.ConnectionString))