Visit my site @ mugunthkumar.com


Tags » ‘tutorial’

iPhone Tutorial: Elegant way to send formatted In-App email

March 7th, 2010 by Mugunth Kumar

By now, most of you know how to send emails using the Message UI framework without quitting the app. In this tutorial, we will briefly discuss the techniques for sending a HTML formatted email with user generated content. Though sending a HTML formatted email is as easy as setting the option to HTML

[picker setMessageBody:emailBody isHTML:YES];

There are some cases that this method that this method doesn’t take care of. In short, this method works great for sending “Tell a friend about this app” email. But if your app needs much more sophistication, especially, if it needs to email data created by the user, you can follow the approach I suggest. Using this method, you can also adapt your code for International support and send the same email in multiple languages without any “hard-coding


Though the method promises some great features, it is ridiculously simple. :)

Step 1:

Create a HTML File with the contents of your email and add it to your project. You should NOT create any css or link it to any external CSS files. All your styles whether embedded withing the <STYLE> tag or kept as a separate CSS file will mostly be stripped by email clients. The reason could be because email clients don’t want your CSS to mess around with their client CSS. Read this article for more details. A sample is given below.

Step 2:

Insert placeholder markers within your HTML file. Your HTML file should look something like this.

<font size = '1' color= "#222222" style = "font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial;"><i>//TEXT_PLACEHOLDER//</i></font>
<font size = '2' color= "#000000" style = "font-family: 'Lucida Grande', 'Lucida Sans Unicode', Helvetica, Arial;"> Have a great day! <br/>//FRIENDNAME_PLACEHOLDER// <br/></font>

The bold markers are the placeholders. You can assume them to be variables in the email. We will be replacing them later.

Step 3:

Do exactly the same steps for sending an in app email explained in one of my previous tutorial, but rather than hardcoding the email body like this,

NSString *emailBody =
[NSString stringWithFormat:@"%@\n\n
Sent from MyGreatApp on iPhone. Download yours from AppStore now!
", content, pageLink, iTunesLink];

Read it from your HTML File you created in Step 2.

NSString *langString = [[userDefaults dictionaryRepresentation] objectForKey:@"NSLocaleCode"];
NSString *emailFileName = [NSString stringWithFormat:@"email_%@.html", langString];
NSString *emailFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: emailFileName];
NSString *body = [NSString stringWithContentsOfFile:emailFilePath encoding:NSUTF8StringEncoding error:nil];

Note that, I’m appending the locale code with the email filename. That means you should create your email file by name email_en_US.html and similarly for other language files.

Step 4:

Replace placeholder texts with your contents generated within the app.

body = [body stringByReplacingOccurrencesOfString:@"//TEXT_PLACEHOLDER//" withString:myText];
body = [body stringByReplacingOccurrencesOfString:@"//FRIENDNAME_PLACEHOLDER//" withString:myFriendName];

The values myText and myFriendName should be passed to this email function.

With this architecture in place, you can format your email, change language and do a lot more without much changes to your code. After all, writing cleaner code is more important in the long run.


Mugunth

iPhone Tutorial: Follow Cost API and a open source wrapper

December 22nd, 2009 by Mugunth Kumar

What is Follow Cost?

Follow Cost is a interesting and powerful statistic that helps you check the “cost” you would incur by “following” a person on twitter. This is a very useful statistic that I think every twitter client should adopt.  Tweetie for iPhone was the first to implement follow cost and I use this feature of tweetie to check whether a follower is “worth the pain” (as follow cost puts it).


In short, Follow Cost gives you an approximate count of the number of updates a person tweets in a day. As a thumb rule, I don’t follow anyone with a follow cost of over 40. A celebrity with a high follow cost is @guykawasaki. (80+ at the time of this writing) His tweets are interesting, but is it worth the pain? Depends. To me, definitely not.

The API

Follow Cost has a relatively simple API. But unfortunately, even after 3 months of Tweetie 2 launch, no other twitter client has implemented. You just have to make a GET request to followcost.com server with the twitter username as follows.=

http://followcost.com/<username>.json

The resulting output is a json formatted string which can be parsed using any JSON Framework.

Objective C Code

To make life easier, I wrote a helper class MKFollowCost. To use the class, first download the JSON framework and follow their installation instructions. If you are writing a twitter client, chances are that, you would probably have done this. Download the code from below and drag the two files, MKFollowCost.h and MKFollowCost.m into your project.

You can instantiate a followcost object by

MKFollowCost *followCost = [[MKFollowCost alloc] initWithTwitterName:@”mugunthkumar”];

All of the variables like, milliscobles, tweets per day etc, can be accessed from this object. The object is designed to be embedded without your Twitter Profile object.

Downloads

FollowCost Objective C Wrapper: FollowCost v1.0

Rights

You can use it in your own Twitter client, royalty free. Attributing me is upto you. However, if you modify the source code, please make it open source.

iPhone Tutorial – Enabling reviewers to use your In-App purchases for free

November 15th, 2009 by Mugunth Kumar

In-App purchases is a great way for developers to upsell by giving away their app for free and then allow them to charge for features when users start using it. This freemium model has indeed worked very well for upselling your app in the AppStore. But unfortunately, there isn’t an Apple allowed way to allow reviewers to “download” your in-app purchases for free (like giving away promotional codes for your in-app purchases). So most developers again resort to the same “lite”, “pro” model.


After raising the issue to Apple, I even got a official reply that it’s not possible currently to allow reviewers to use your in-app purchases for free.

noinappurchases

However, developers’ creativity knows no bounds. In this post, I’ll present a method to allow reviewers to use your in-app purchases for free without having multiple versions of the same app on the app store. The source code for the same is also available royalty-free (as always) for using it in your own apps. Before diving in, it’s advised that you read through my previous tutorial on how to do in-app purchases

(more…)


7 visitors online now
7 guests, 0 members
Max visitors today: 19 at 05:00 pm SGT
This month: 58 at 03-01-2010 02:57 am SGT
This year: 59 at 02-12-2010 06:23 am SGT
All time: 59 at 12-19-2009 09:48 pm SGT