Building a More Complex Application / Creating TinyUrl - NSScanner
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
In this video we are going to write a method that will find an existing URL if there is one in the text view, obtain a TinyURL and replace the existing URL with the Tiny one. Once we finish this method, we will have completed the TwitterMe application. To accomplish this, we will be using the NSScanner Class. I have the method ready to go and we will walk through it line by line and explain how each one works. We're in the Create TinyURL Method. In our first line we're going to set two integer variables, a starting index and an ending index. These variables are used to create the NSRange we'll need to replace the current URL with the TinyURL. Next, we are creating an NSError and setting its initial value to Nil. Remember, we learned a valuable lesson in our earlier video when we had not initialized this variable. No need to make that mistake twice. We are using that same reasoning when we create the NSString. Here we need a fully instantiated NSString so that we can pass it by reference to the Scan Up to Characters from Set into String Method. The next line is where an instantiate an NSScanner object and use its class method, Scanner with String, using the Text View String as the parameter. The NSCharacterSet will be used to find the end of the URL string. We are using the white space and New Line Character setClass Method. The definition we are going to use as the end of a URL is going to be either a new line character or white space. Either one of these will signify the end of the URL. Next, we begin to search the string for the characters http:Forward Slash Forward Slash. As you can see, we are only searching for this one URL prefix. You could change this to use an NSCharacterSet and add additional URL prefixes if you prefer. We scan up to the HTTP. If it is found, then the characters preceding the HTTP will be discarded. We do this by passing Nil as the parameter to empty string. We then set the starting index to the scanner's current location in the string. Our next line uses Scan Up to Characters from Set into String Method. The string that is found is inserted into the Found URL Variable. Again, we set the ending index to the scanner's current location. These two variables, starting index and ending index, now hold the starting and ending location of the found URL. Now we check the found URL to see if a URL was found. If a URL was not found, we do nothing. If one was found, we create an NSRange by setting its location to the starting index and the length to the number of characters contained by the URL. To do this we simply subtract the starting index from the ending index. After that we create the TinyURL API String that will return as the TinyURL. Once we have the string created, we need to create a legal URL string. We do that with String by Adding Percent Escapes using Encoding. We've used this method before. Next, we create an NSURL. This is what is required by the NSString Method, String with Contents of URL Encoding Error that we'll use next. In this method the contents of the URL are returned and stored in the variable Tiny URL. The TinyURL API returns only the TinyURL. So there is no parsing required like there was with the Twitter API. Also notice that we are not having to use NSTask or cURL to get the contents of a web page. NSString can do this as well. I do want to make a note here about this. I chosen NSString for this task not only to demonstrate its use but because the task itself was so small it seemed like the best fit. After that we check to ensure that there were no errors retrieving the TinyURL. If there were no errors, then we replace the current URL with the Tiny one. We do this by calling NSText Views, replace characters in range with string method, passing in the URL range that we created at the beginning of this method and the TinyUrl. If there was an error, you report it back to the user with a modal dialog. That's all there is to replacing an existing URL with a Tiny one. One thing to note is that this will only work on the first URL the scanner encounters. Now I think it's time to Build and Run and enjoy our finished application. And we want to click the TinyURL Button. We get a TinyURL in its place.
Tutorial Information
| Course: | Objective-C and Xcode Essential Training |
| Author: | Craig Williams |
| SKU: | 34088 |
| ISBN: | 1-936334-00-3 |
| Release Date: | 2010-02-22 |
| Duration: | 6 hrs / 68 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 