ConnectionKit updates no.1: Connection creation

Update: This has been superseded by ConnectionKit 1.2.

At Karelia, we're hard at work on Sandvox 1.6 which will feature a brand new publishing engine (still built atop ConnectionKit of course). To do this, we're actually working with a branch of ConnectionKit, where work is underway to modernise and simplify some of the API. I'm going to be posting a series of short articles on the changes as work wraps up; first up today is the new connection registry.

The CKConnection protocol has been overhauled to operate a little more like NSURLConnection. There is now a single initializer method, -initWithURL:. If the URL is unsupported, it will return nil. Generally you only need to use this method if you're interacting directly with a single connection type. e.g.

id <CKConnection> connection = [[CKFTPConnection alloc] initWithURL:[NSURL URLWithString:@""]];

For connections like CKFileConnection which require no URL to operate, you can pass in nil, or use the convenience -init method.

Of course, most developers are using ConnectionKit at a higher level where they support a range of connection types across the single CKConnection protocol API. In which case, you will want to interact with the connection registry which handles automatically creating the right kind of connection. e.g.

connection = [[CKConnectionRegistry sharedConnectionRegistry] connectionWithURL:aURL];

This will look at the scheme of the URL to determine the best type of connection available and then attempt to initialize one. As before, if the URL is unsuitable, nil is returned. If you don't know the URL scheme, but do know the connection type's name (for example, an app like Flow or Transmit), these sister methods exist:

- (id <CKConnection>)connectionWithName:(NSString *)name host:(NSString *)host port:(NSNumber *)port;
											- (id <CKConnection>)connectionWithName:(NSString *)name host:(NSString *)host port:(NSNumber *)port user:(NSString *)username password:(NSString *)password error:(NSError **)error;

Hopefully this of some help to you. There are some extra supporting methods in CKConnectionRegistry if you need them, and we're slowly increasing the amount of documentation in the header files. As I said at the start, this is all currently taking place a branch (named NSURLConnection-style authentication), but will trickle back to the trunk in due course. If you want to use it now, FTP and WebDAV are basically stable to work with on the branch, but SFTP still has some kinks to iron out.

© Mike Abdullah 2007-2015