13th Sep 2009
Google Map, Camera, iPod Library and Facebook Connect with iPhone

Displaying Google map
The iPhone SDK 3.0 offers a class of map view in MapKit framework. You just need to declare an object of MKMapView and add it to your current view.
MKMapView *mapview=[[MKMapView alloc]initWithFrame:(CGRectMake(0, -20, 320, 320))];
[self.view addSubview:mapview];
Now, to display the user location on map, I was trying to insert a pin annotation on map. Soon, I realized that was wrong approach. User location is meant for changing. It will be updated many time.
The solution for this was to use the MKUserLocation class object. The object of this class will return “Blue dot” of built-in Google map application on iPhone. It will also show animated circle with radius of probable range of user location.
MKUserLocation *ulocation=[[MKUserLocation alloc]init];
[mapview setShowsUserLocation:TRUE];
[mapview addAnnotation:ulocation];
That’s it. You don’t need to worry any more about the current location of user. The ulocation object will change its co-ordinates and display the dot whenever the position is updated.

Sending encoded image
You can send images to web service either by mail or by encoding it to string. I have used base64 encoding method to encrypt the image taken from iPhone camera and to send it to web service.
Generally, the image taken from iPhone camera is of very large size (800 kb to 2 mb). So, it may take very long time to encode the image.
I found another way to do the same thing. After taking the picture, you can display the image taken in full view of screen (320*480). Then take the screenshot at that stage and fetch the new image for encoding. In this way, you can make your encoding faster as this was the same picture taken using camera.
I have used following process to take the screenshot.
UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
UIGraphicsBeginImageContext(screenWindow.frame.size);
[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *screenshotPNG = UIImagePNGRepresentation(screenshot);
Now, since you have the image in screenshotPNG, you can encode it using your base64 method.
NSString *dstring=[[NSString alloc]init];
dstring=[self base64StringFromData:screenshotPNG length:[screenshotPNG length]];
Playing a specific song
With iPhone SDK 3.0, we have the access to iPod library on iPhone. The apple documentation “iPodLibraryAccess Guide” covers most of the part needed for understanding this functionality. Here, I will describe how to play a specific song stored on iPhone.
Let’s assume our song name is in songtitle which is NSString. To begin, we need objects of
MPMusicPlayerController and a MPMediaQuery.
MPMusicPlayerController* iPodMusicPlayer;
MPMediaQuery *everything = [[MPMediaQuery alloc] init];
everything=[MPMediaQuery songQuery];
NSArray *itemsFromGenericQuery = [everything items];
for (MPMediaItem *song in itemsFromGenericQuery)
{
NSString *mysong = [song valueForProperty: MPMediaItemPropertyTitle];
if([mysong isEqualToString:songtitle])
{
iPodMusicPlayer=[MPMusicPlayerController iPodMusicPlayer];
[iPodMusicPlayer setNowPlayingItem:song];
[iPodMusicPlayer play];
break;
}
}
Using Facebook Connect on iPhone
Facebook Connect used in iPhone lets user connect to their facebook account. I am facing one problem in maintaining session of facebook. The sample application provided with facebook sdk allows user to stay logged-in even if iPhone application is terminated. So, the user will not have to enter facebook account username and password next time.
We can know the status of user if he is logged in by using -
(void)dialogDidSucceed:(NSURL*)url and – (void)sessionDidLogout:(FBSession*)session method from FBLoginDialog.m.
Whenever user logsin using facebook account, the session will stored using NSUserDefaults. We can know this by tracking -(void)save; method in FBSession.m file.
The save method in FBSession.m stores 4 objects in userdefaults: FBUserId, FBSessionKey, FBSessionSecret and FBSessionExpires.
I am facing problem here. Whenever I try to resume previous session, using -(BOOL)resume; method in FBSession.m, I get correct userid, but rest of the values will be nil. i.e.session key, sesison secret and session expirary date are nil. So, I get one fatal error at that time saying: “attempt to insert nil value (key: session_key)” I am still trying to find out the solution for that.
Well, the post is actually the greatest on this worthy topic. I concur with your conclusions and will thirstily look forward to your upcoming updates. Saying thanks will not just be adequate, for the exceptional clarity in your writing. I will directly grab your rss feed to stay privy of any updates. Gratifying work and much success in your business endeavors!
The reply above me was by a bot.
Might just not use fb sessions… same error here.