In my article on CoreText I mentioned that Apple left out some very useful methods from NSAttributedString, namely the ones that would allow you to create an attributed string from HTML. Now you would probably not want to create attributed strings from complex HTML documents, but use that for simple tasks like displaying one word in a different color or bold.
My first thought was that maybe I should make it a sellable component, but I dismissed this idea for two reasons:
- there is a certain likelyhood that Apple will implement the missing functions in the next major SDK refresh
- if I would get help from people who are more experienced in dealing with HTML parsing then this would benefit everybody involved
So I posted the question on Twitter as to where to put the shared code. The response was a resounding GitHub (one mention of Mercurial and Assembla each). So far I had put off dealing with GitHub, because – in contrast with many other iOS developers – I happen to very much like Subversion integration in Xcode.
Read on to learn how to get started.
Now learning about GitHub cannot be totally wrong because it will be supported as a source code management (SCM) system in coming Xcode 4. Therefore let’s not procrastinate any further and get started.
First you need to install Git. There are several methods of doing that, compiling it, installing it via brew, but those are for GitHub geeks, not me. For us Git beginners on Mac the easiest method is via the OSX Installer for Git.
While this is downloading you sign up for a GitHub account, choose “Free for Open Source”. I like the sound of that. Actually, I already had a GitHub account, so I renamed this (you get 1 rename) to Cocoanetics.
Next I went through the GitHub Crashcourse with a great deal of impatience. I saw that git was indeed installed because I was able to create a repo in an arbitrary location:
mkdir ios-attributedstring-html cd ios-attributedstring-html git init git status -s
So enough already with the toying around, on the GitHub dashboard I found the link I was looking for: “Create a Repository”. I must say GitHub provides great service, they even tell you what to set up next.
Setting user name and password is probably a smart idea, so I did that as suggested.
git config --global user.name "Oliver Drobnik" git config --global user.email email@example.com
Also I followed the steps under “Next Steps”, but got an error when I tried to push my changes to the server. I figured, I might not need this RSA public key mumbo-jumbo.
The authenticity of host 'github.com (184.108.40.206)' can't be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'github.com,220.127.116.11' (RSA) to the list of known hosts. Permission denied (publickey). fatal: The remote end hung up unexpectedly
Well, that’s what you get if you try to use the ssh connection without GitHub having a public key on file. So I followed the guide on how to generate SSH keys on OSX. I did not get any output using pbcopy, but just the cat of the id_rsa.pub file worked like a charm to add it on the dashboard.
With my public key in place I returned to the project folder and this time the push worked. Upon inspecting the online browser of my new repository I indeed found the README present that I had created earlier.
Now the next step I figured would be to add all the current project files (minus the build folder) to the repo.
Getting the Source Online
I copied the files from my folder on my desktop to the git folder with finder, the rest I did on the terminal from within the project folder.
git add * git status -s A Classes/CoreTextExtensionsAppDelegate.h A Classes/CoreTextExtensionsAppDelegate.m A Classes/CoreTextExtensionsViewController.h A Classes/CoreTextExtensionsViewController.m A Classes/NSAttributedString+HTML.h A Classes/NSAttributedString+HTML.m A Classes/NSString+HTML.h A Classes/NSString+HTML.m A Classes/TextView.h A Classes/TextView.m A Classes/UIColor+HTML.h A Classes/UIColor+HTML.m A CoreTextExtensions-Info.plist A CoreTextExtensions.xcodeproj/Oliver.mode1v3 A CoreTextExtensions.xcodeproj/Oliver.pbxuser A CoreTextExtensions.xcodeproj/project.pbxproj A CoreTextExtensionsViewController.xib A CoreTextExtensions_Prefix.pch A MainWindow.xib A main.m
git commit -m "Added Project Files" [master cb449bb] Added Project Files 20 files changed, 3959 insertions(+), 0 deletions(-) create mode 100644 Classes/CoreTextExtensionsAppDelegate.h create mode 100644 Classes/CoreTextExtensionsAppDelegate.m create mode 100644 Classes/CoreTextExtensionsViewController.h create mode 100644 Classes/CoreTextExtensionsViewController.m create mode 100644 Classes/NSAttributedString+HTML.h create mode 100644 Classes/NSAttributedString+HTML.m create mode 100644 Classes/NSString+HTML.h create mode 100644 Classes/NSString+HTML.m create mode 100644 Classes/TextView.h create mode 100644 Classes/TextView.m create mode 100644 Classes/UIColor+HTML.h create mode 100644 Classes/UIColor+HTML.m create mode 100644 CoreTextExtensions-Info.plist create mode 100644 CoreTextExtensions.xcodeproj/Oliver.mode1v3 create mode 100644 CoreTextExtensions.xcodeproj/Oliver.pbxuser create mode 100755 CoreTextExtensions.xcodeproj/project.pbxproj create mode 100644 CoreTextExtensionsViewController.xib create mode 100644 CoreTextExtensions_Prefix.pch create mode 100644 MainWindow.xib create mode 100644 main.m
git commit -a # On branch master nothing to commit (working directory clean)
At this point I had expected to see the new files also on my online repository as was the case with SVN commits. But not so. I had only commited to my local repository. Now I still needed to push my changes to the master. Another git push origin master did the trick.
Tower (currently in BETA) was highly recommended to me as a Mac app to deal with git visually. At first glance it looks “Easy. Efficient. Powerful.” as stated on their website. I’ll have to use it a bit before I can say more.
So far my GitHub experience – however short – has been an enjoyable one. What’s still left for me to explore is how to actually go about the business of having other people contribute to the project. I know that they can clone my repository, but how do the changes I like get merged back in.
People who recommended GitHub to me also highlighted the social aspect stating that you can easily follow other people’s code. I know how following on Twitter works, but social coding is quite new to me. So I am excited to see that comes out of that.
I am looking forward to git integration with Xcode 4 as is present in the current BETA. With this the workflow is almost identical as with Subversion. The only difference that I can see as of now is that git additionally provides mechanisms to have each clone of the master repository be functional by itself. I hear that’s great for working while traveling. 🙂
Any additional hints and tips please put in the comments.