Online Book Reader

Home Category

iPhone Game Development - Chris Craft [121]

By Root 1580 0
flavors. For example, SOAP (originally defined as Simple Object Access Protocol) is a mature protocol that has been around for a while. However, there is currently no native support for SOAP in the iPhone SDK. You could spend countless hours building a SOAP client from scratch. Fortunately, there are tools available that have already done the work for you. For example, RemObjects Software (www.remobjectscom/iphone) has a SOAP Web service framework that fills this gap. They also offer a free tool for quickly setting up a push notification server. Third-party tools do come with a price, but they can be huge timesavers and the cost is almost always less than what it would cost you to take the time to do it yourself.

As we've mentioned, connectionDidFinishLoading is called whenever the complete response has been received from the server. When this happens, you call receiveMessage:ofType:, which sends the complete message to the delegate GameControllerDelegate:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

GameConnectionMessageType messageType = GameConnectionMessageData;

[self.delegate receiveMessage:self.responseData ofType:messageType];

}

When an error is encountered, connection:didFailWithError: is called. You retrieve the message description and again call receiveMessage:ofType:, which sends the complete error message to the delegate GameControllerDelegate:

- (void)connection:(NSURLConnection *)connection

didFailWithError:(NSError *)error {

GameConnectionMessageType messageType = GameConnectionMessageError;

NSString *message = [error localizedDescription];

[self.delegate receiveMessage:message ofType:messageType];

}

Accepting messages from Apple Push Notification

You can completely implement an Internet-ready game without push notification. However, push notification adds that extra piece that allows your game to behave as if it were always running. You should still use the methods we just discussed to send, receive, and manage game messages. It is better to use push notification to accomplish tasks that enrich your game that only push notification can achieve.

Sending a message with push notification must be done from a server connected to the Apple Push Notification server. This means that for your app to send a push notification to another device, it must do so through a server that you build. Since this is the case, you can choose any method you like to send the message to your server.

Coding your application to receive push notifications is another matter. Before you can receive a message, you must first register your app to receive notifications. You can register your app for notifications by calling the method registerForRemoteNotificationTypes:. Apple recommends that you call this in the method applicationDidFinishLaunching as follows:

- (void)applicationDidFinishLaunching:(UIApplication *)app {

[[UIApplication sharedApplication]

registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |

UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

}

If the registration is successful, you will receive notification via the delegate method didRegisterForRemoteNotificationsWithDeviceToken. When you receive this message you'll know that you have successfully registered for push notification. After this, the GameConnection can send the device token and the user's e-mail address to your server as needed for this example:

- (void)application:(UIApplication *)app

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {

self.devTokenBytes = [devToken bytes];

self.registered = YES;

}

If the registration fails, you will receive a call to the following method. Be sure to alert the user and, if necessary, disable any options that depend totally on push notification:

- (void)application:(UIApplication *)app

didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {

NSString *message = [NSString stringWithFormat:@” Error in registration.

Error: %@”, err];

Return Main Page Previous Page Next Page

®Online Book Reader