Google Map, Camera, iPod Library and Facebook Connect with iPhone

September 13th, 2009 by pratik.joshi § 4

iphone_home

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.

iPhoneMap

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.

iPhone Application Deployment Steps

September 8th, 2009 by vivek.navadia § 8

Following steps may help you deploying your application successfully on the device.

  • Download your .mobileprovision and .cer certificates from developer.apple.com website.
  • Add the certificates to your keychain in Mac.
  • Add your mobile provision certificate in Xcode project. Drag the certificate to Xcode in left pane of window and drop there.
  • Select the Project and go to Edit Project Settings

1_New

  • Select Build Tab
  • Select Configuration as Release
  • Go to code signing
  • Add your certificate in Any iPhone OS Device section
  • When you add certificate in for Code Signing Identity, then it must be in list of Any iPhone OS Device option.

2

  • Follow same steps for Debug configuration also.
  • Do same change for Debug and Release configuration in Edit Active Target “Project Name” setting also

3

  • Edit the ProjectName-info.plist file in resources group and change the company name with yourcompany name on which you have registered your certificate in following value com.digicorp.${PRODUCT_NAME:rfc1034identifier} and save the project.

4

  • To solve invalid entitlement issue first right click on application and add new file
  • Under the iPhone OS select Code Signing
  • Select the Entitlements
  • Make sure it is saved as Entitlements.plist

5

  • Now uncheck the box of get-task-allow property in Entitlements.plist

6

  • Set the Any iPhone OS Device with saved entitlements certificate under Code Signing Entitlements for Debug and Release configuration.

7

  • Set the device OS for which you want to deploy your application from top left bar of Xcode window.
  • Save the Project and Build it.

It will successfully deploy your application on device.

Where Am I?

You are currently browsing the iPhone category at Digicorp.