Saturday, May 23, 2009

Google Analytics Data with Objective c, part two

Well it is time for part two. Part one was easy and the second part is easy as well.
Now that we got the authentication token can now get analtyics data.
The data we are getting is the accounts and profiles for analytics:

NSURL *urlAccount = [NSURL URLWithString:@"https://www.google.com/analytics/feeds/accounts/default"];
NSString* param = [NSString stringWithFormat:@"GoogleLogin auth=%@",authString];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:urlAccount];

[req setHTTPMethod:@"GET"];
[req addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[req setValue:param forHTTPHeaderField: @"Authorization"];
[req addValue:@"Authorization" forHTTPHeaderField:param];

NSHTTPURLResponse *res;
NSError *err;
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&res error:&err];

[req release];
return data;

Finally u get a NSData. I personal use libxml2 for parsing the xml data and XPath queries. Check out http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html for more info :)

Friday, May 22, 2009

Google Client Login, analytics with Objective c, Part one

Now that Google Analytics API is out and lot of people want to get access to the analytics data.
It is not to hard to retrieve the data. First thing that u need to do is access the Client Login and retrieve authentication token.
The easiest way to do this is:

NSString *post = [NSString stringWithFormat:@"accountType=GOOGLE&Email=%@&Passwd=%@&service=analytics&source=%@", email, password, source];
NSURL *url = [NSURL URLWithString:@"https://www.google.com/accounts/ClientLogin"];

NSMutableURLRequest *authRequest = [[NSMutableURLRequest alloc] initWithURL:url];
[authRequest setHTTPMethod:@"POST"];
[authRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[authRequest setHTTPBody:[post dataUsingEncoding:NSASCIIStringEncoding]];

NSHTTPURLResponse *authResponse;
NSError *authError;

NSData *receivedData = [NSURLConnection sendSynchronousRequest:authRequest returningResponse:&authResponse error:&authError];
NSString *authResponseData = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];

NSArray *lines = [authResponseData componentsSeparatedByString:@"\n"];
NSMutableDictionary *token = [NSMutableDictionary dictionary];

for(NSString *s in lines)
{
NSArray *kvpair = [s componentsSeparatedByString:@"="];
if([kvpair count]>1)
[token setObject:[kvpair objectAtIndex:1] forKey:[kvpair objectAtIndex:0]];
}

[authRequest release];
[authResponseData release];

return [token objectForKey:@"Auth"];

Well that's the first part of how to retrieve analytics data from Google Analytics API

Monday, May 18, 2009

iPhone

Well it been a while since I wrote something. Things has changed the main focus is iPhone. I'm going to some easy tutorial of things I hard time with.
Let's begin with some basic UI elements:

UITextField
Code:
UITextField *username =[[UITextField alloc] initWithFrame:CGRectMake(24.5, 65, 270, 30)];
username.delegate=self;
username.textAlignment=UITextAlignmentCenter;
username.borderStyle=UITextBorderStyleRoundedRect;
username.placeholder=@"Username\n";
username.autocorrectionType=UITextAutocorrectionTypeNo;
username.autocapitalizationType=UITextAutocapitalizationTypeNone;
[self.view addSubview:username];

UIActionSheet
Code:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"This is where the information will go"
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil
otherButtonTitles:@"Email", @"Tech Support",@"Website",@"Cancel", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
actionSheet.destructiveButtonIndex = 3; // make the second button red (destructive)
[actionSheet showInView:self.view];
[actionSheet release];
Selecting a button on UIActionSheet
Code:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0){
// do stuff
}
}

UISwitch
Code:
UISwitch *switchIt = [[UISwitch alloc] initWithFrame:CGRectZero];
switchIt.on=NO;
[self.contentView addSubview:switchIt];
UILabel
Code:
UILabel *usernameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
usernameLabel.text=@"Personal Location Checkin";
usernameLabel.adjustsFontSizeToFitWidth=YES;
[self.contentView addSubview:usernameLabel];
UISearchBar
Code:
UISearchBar *mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 45)];
mySearchBar.delegate = self;
mySearchBar.showsCancelButton = YES;
mySearchBar.barStyle=UIBarStyleBlackOpaque;
mySearchBar.placeholder=@"Enter Name or Phone Number";
mySearchBar.keyboardType=UIKeyboardTypeNamePhonePad;


[self.view addSubview: mySearchBar];
UIView
Code:
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor grayColor];
self.view = contentView;
[contentView release];
UITableView
Code:
UITableView *myBeaconsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 45, self.view.bounds.size.width, 375)];
myBeaconsTableView.delegate=self;
myBeaconsTableView.dataSource=self;

[self.view addSubview:myBeaconsTableView];
Or to make a grouped table view (like in preferences)

Code:
UITableView *settingsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 40, self.view.bounds.size.width, 375) style:UITableViewStyleGrouped];
settingsTableView.delegate=self;
settingsTableView.dataSource=self;
[self.view addSubview:settingsTableView];
UINavigationBar
Plain no Title
Code:
myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 45)];
[self.view addSubview:myNavBar];
Black Opaque (no title)
Code:
myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 45)];
myNavBar.barStyle=UIBarStyleBlackOpaque;
[self.view addSubview:myNavBar];
Black Translucent (no title)
Code:
myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 45)];
myNavBar.barStyle=UIBarStyleBlackTranslucent;
[self.view addSubview:myNavBar];
To add title and other buttons

Code:
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"Around Me"];
[myNavBar pushNavigationItem: navItem];
To add buttons with image
Code:
refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(action:)];
[navItem setLeftBarButtonItem:refreshButton];
Add button with Text
Code:
refreshButton = [[UIBarButtonItem alloc] initWithTitle:@"About Us" style:UIBarButtonItemStyleBordered target:self action:@selector(action:)];
[navItem setRightBarButtonItem:refreshButton];
How to add comments
Code:
When you want to add a comment about a line of code you do this

// Comment

To do a multi-lined comment you would do this

/* This
is
multi-lined
comment
*/
UIWebView
Code:
dailyWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 45, self.view.bounds.size.width, 375)];
dailyWebView.delegate=self;
dailyWebView.scalesPageToFit=YES;
[dailyWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
[self.view addSubview:dailyWebView];
UITextView
Code:
UITextView *textView = [[[UITextView alloc] initWithFrame:frame] autorelease];
textView.textColor = [UIColor blackColor];
textView.font = [UIFont fontWithName:@"Helvetica" size:15];
textView.delegate = self;
textView.backgroundColor = [UIColor whiteColor];

textView.text = @"TEXT TO GO IN HERE";
[self.view addSubView:textView];

UISegmentedControl
Code:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:@"segment_check.png"],
[UIImage imageNamed:@"segment_search.png"],
[UIImage imageNamed:@"segment_tools.png"],
nil]];
frame = CGRectMake( 0,
0,
self.view.bounds.size.width,
35);
segmentedControl.frame = frame;
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 1;
[self.view addSubview:segmentedControl];
UIAlertView
Code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"Your message"
delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Button1", @"Button2", nil];
[alert show];
[alert release];

This is just some UI elements that can be added through code

cms and stuff - new iPhone :)