[Part.2] A solution to “Using URLSchemes to send game level through e-mail”

3.Sending a e-mail inside your app

Before we start to use the URLSchemes we’ll need to add the “in-game-send-email” feature in our project.
That’s also easy to implement using the MFMailComposeViewController, you can check the demo project I wrote before:

http://www.supersuraccoon-cocos2d.com/2011/03/29/send-e-mail-demo/

For this tutorial, we’ll focus on the email-body part since we want embed the json content into a hyberlink such as “lauch my game level” like this:

NSString *emailBody = [NSString stringWithFormat:@”Launch Demo!!!“, jsonString];

Now we know how to send the email inside our app, let’s begin with the URLSchemes stuff.

4.Playing with the URLSchemes feature (sending and receiving)

For how to setting up the URLSchemes for your project, here is a great tutorial you can follow:

http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html

and for our project we’ll use the “URLSchemesDemo://” to launch our demo.

And before we sending and receiving the data with URLSchemes we’ll need to encode/decode the json content, we can do it like this:

[cc lang=”objc”] – (NSString *)encodeToPercentEscapeString: (NSString *) input
{
// Encode all the reserved characters, per RFC 3986
// ()
NSString *outputStr = (NSString *)
CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)input,
NULL,
(CFStringRef)@”!*'();:@&=+$,/?%#[]”,
kCFStringEncodingUTF8);
return outputStr;
}

– (NSString *)decodeFromPercentEscapeString: (NSString *) input
{
NSMutableString *outputStr = [NSMutableString stringWithString:input];
[outputStr replaceOccurrencesOfString:@”+”
withString:@” “
options:NSLiteralSearch
range:NSMakeRange(0, [outputStr length])];

return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
[/cc]

4.1 sending the game level with URLSchemes:

In order to send the “game level” we need to embed our “game level” info (json) into the URLSchemes so that when your launch the project by clicking our URLSchemes the “game level” info will be sent to our project:

[cc lang=”objc”] jsonString = [[GameDataManager sharedGameDataManager] encodeToPercentEscapeString:jsonString];
NSString *emailBody = [NSString stringWithFormat:@”Launch Demo!!!“, jsonString];
[/cc]

4.2 receiving the game level with URLSchemes:

To receive the data sent with URLSchemes is quite simple:

[cc lang=”objc”] – (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if (!url) {
return NO;
}

[[GameDataManager sharedGameDataManager] processReceivedGameLevel:[url absoluteString]];

return YES;
}
[/cc]

Then the “processReceivedGameLevel” function will decode the content and then save the level locally.

5.The summary

By now, we have finished our demo project with the following features:

1.generate a “game level” and save it to a json file locally
2.send the “game level” through email
3.launch the project with URLSchemes feature
4.receive the data attached inside the email and parse it into our json file(game level)
5.load the sent/received “game level”

Something to mention:

1.For a real app, you can make a “game level editor” for the user instead of generating the “game level” automatically like we do in the demo project

2.For the length of the data length limit of the URLSchemes, I haven’t found an actual number yet but I have tried with data with length over 10,0000 so I think that should be enough for most of the occasion πŸ™‚

Demo ScreenShot:

To Download Page…

β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†

If you feel all the stuffs in this site helped you a lot and you would buy me a beer πŸ™‚

Or get a game I made πŸ™‚


β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†β˜…β˜†