Archive for July, 2009
Objective-C: Base64 to NSString and NSString to Base64
0While working on the CSCW Lab project, I encountered a situation in which the XML-RPC call returned an image, of course encoded as Base64. And guess what – in its known style, Apple doesn’t provide Base64 encoding and decoding – quite lame, given the fact that this encoding is used everywhere in data transfer over the internet – e-mail, browsers, web services – all of them use at some point this encoding to overcome the different local encodings on each one’s machine.
Once identified this problem, I had to solve it somehow – but guess what? – over the free sources there aren’t too many functions that provide this simple and basic encoding.
Finally, after few hours of searching, I finally found that Eric Czarny had a very successful implementation of this in its Cocoa XML-RPC Framework . After taking a quick look at its code, I end up using and importing into my project the
- NSStringAdditions.h. NSStringAdditions.m – providing the new category
+ (NSString *)base64StringFromData: (NSData *)data length: (int)length;
- NSDataAdditions.h and NSDataAdditions providing the new category:
+ (NSData *)base64DataFromString: (NSString *)string;
Being categories, they are automatically added to NSString and NSData automatically at runtime, thus their usage is straight forward :
UIImageView *uiIV;
// currentElementValue holds the string representation of the image, encoded in Base64
NSData *nsD = [NSData base64DataFromString: [dataLayer currentElementValue]];
if ([nsD ){
uiIV = [[UIImageView alloc] initWithImage:[UIImage imageWithData: nsD]];
}
Processing NSDate into an ISO8601 string
2
During the CSCW Lab, where I had the experience of working on iPhone, I had to connect to a XML RPC server. Some of the parameters of the request had to be formatted as ISO8601 standard. After some reading, I end up using the following code, managing both the conversion of a NSDate to NSString and a NSString to a NSDATE using the above format:
NSString –> NSDate
-(NSString *) strFromISO8601:(NSDate *) date {
static NSDateFormatter* sISO8601 = nil;
if (!sISO8601) {
sISO8601 = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
int offset = [timeZone secondsFromGMT];
NSMutableString *strFormat = [NSMutableString stringWithString:@"yyyyMMdd'T'HH:mm:ss"];
offset /= 60; //bring down to minutes
if (offset == 0)
[strFormat appendString:ISO_TIMEZONE_UTC_FORMAT];
else
[strFormat appendFormat:ISO_TIMEZONE_OFFSET_FORMAT, offset / 60, offset % 60];
[sISO8601 setTimeStyle:NSDateFormatterFullStyle];
[sISO8601 setDateFormat:strFormat];
}
return[sISO8601 stringFromDate:date];
}
Brainstorming 2
0In a previous post I explained the facts that forced us to re-think our project idea. Basically, our cool idea of an iPhone tied to a skateboard to create a exertion game require too much programming time. So we searched another idea who is required to be simpler and easy to be implemented in the remaining time.
So we got us a round table (the ones in BIT cafeteria are the best!) and put our minds to work.
First step : Use the Initial Design Techniques – Brainstorming (as learned in DIS I)
Our goal: collect as many ideas of exertion game as we can
Defer judgment, no criticism, no rejection of ideas
Include all ideas, allow also the leapfrog ideas
Result:
1. Virtual basket
2. Ping-pong game to the wall
3. Squash
4. Throw a ball to a target
5. Boxing
6. Throwing paper balls to garbage – fun in offices
After some debate we agreed to pursue in implementing the 4th idea. We will try to create an exertion game which will be composed from:
- a board which will act like a hit sensitive projection screen,
- a projector to display the target on the screen
- the software behind to calculate:
- hit points,
- decide if user hit the target
- scoring and level finishing
To have a clear idea of our game we created also a small storyboard:
Changing the idea of the project
0In the last meeting, our tutors, Mr. Heller and Mr. Karrer, shared with us some of their experience using the sensors which made us reconsider the project idea.
The idea
Because of the new introduction in iPhone programming and the rich set of sensors of this device, our project envisioned an iPhone attached to the board in order to track the skateboard movements. The data pulled out by the iPhone is the initial acceleration (the one when the user starts riding with the board), and lateral or up-down acceleration when he does turns or jumps with the board.
First, the iPhone accelerometer won’t do the trick of getting all the information needed from the board. As it outputs acceleration, we would have to integrate once to get the velocity and twice to get the position. This gets us into a lot of approximations which aren’t suited for movements in a few meters range.
Secondly, the iPhone sensor is not design to get an industrial-scale precision and accuracy in an outdoor activity, but more for fun activities like sensing shakes or directional changes.
Thus, combined with the above observation implies the fact that our data readed from an iPhone attached to a board would not be as accurate we need (also the integration introduces approximations first to the velocity vector and again when computing position). It also it contains a lot of noise because of the environment (the skateboard goes on the rough and bumpy surfaces).
The solution
Given the facts, our team agrees with the tutors that this project would have needed too much time in order to develop some sophisticated algorithms, compensating the noise and the two integrations approximations, hardly near impossible to be done in the time span allocated for this project.
The upper side of this discussion is that our knowledge about sensors and in particular iPhone sensors has improved thanks to our tutors. Our suggestion is that for the next lab a similar discussion and sensor presentation should be held in the first laboratory meeting, in order to guide the students even faster to concrete results.
Install Maven in Eclipse: the eclipse.ini -vm option
1While setting up the Maven for Eclipse, if you use a Java JRE instead of JDK you’ll be prompted like this:

Clicking on the first active text takes you to their Help website – Running Eclipse section where you’ll find a plethora of attributes to set in order to customize your IDE.
Objective-C: Use NSXMLParser
0
Almost any time you’ll need to pull some data from the web. Using Objective-C you have at your disposal a pretty good XML parser – the event-based version. What really means is that instead of building in-memory tree with the structure of the XML you’ll have some events raised when the parser encounters a special token – the most usual ones are tag start, tag end and found comments. The parsing goes node by node and is not nesting-sensitive. As soon as the parser returns you a node, you don’t know where in the structure you are currently anymore. As long as you have a clearly defined structure where each element is always present, you could do this using a counter. However, as soon as you have multiple nodes with no defined count, you have a problem.
The usage is simple and straight-forward. For simplicity, we assume that we have only have one level of children under the root node, but this can be easily extended to many levels. We’ll see some code and after that the detailed explanations:
- (void)parseJourneyData:(NSData *)data parseError:(NSError **)err {
// create and allocate the parser - it must be initiated with the XML data that we received or loaded
NSXMLParser *xmlParser= [[NSXMLParser alloc] initWithData:data];
self.arrResult= [[NSMutableArray alloc] init];
// Create the array for holding the resulted data
[xmlParser setDelegate:self];
// The parser calls will be redirected to methods in this class
[xmlParser setShouldProcessNamespaces:NO];
// We are not interested in namespaces
[xmlParser setShouldReportNamespacePrefixes:NO];
// neither in prefixes
[xmlParser setShouldResolveExternalEntities:NO];
// just data, no other stuff
[xmlParserparse];
// Parse that data.. here the parsing begins and the delegated methods will get called
//the parsing process ended and we check for errors
if (err && [xmlParser parserError]) {
*err = [xmlParser parserError];
}
[xmlParser release];
//cleaning the remainders
}
Objective-C: allocation and deallocation
0
While reading the book by Mr. Kochan (1st ed): "Programming in Objective C" , I noticed that he uses the following code (you can see it at pages 342-344) to explain that the initWithString is preferable to stringWithString because the AddressCard class would own the name variable contents. Also, I don’t get any errors making repeated calls to the setName version with the stringWithString method.
//I didn't added here the header file which has the needed declarations
#import "AddressCard.h"
@implementation AddressCard;
-(NSString *) name{
return name;
}
//Recommended code:
-(void) setName: (NSString *) theName{
[name release]
name = [[NSString alloc] initWthString: theName];
}
//Incorrect code according to Kochan:
-(void) setName: (NSString *) theName{
[name release]
name = [NSString stringWthString: theName];
}
(more...)
