Archive for July, 2010

29 JulHelpful primitive #DEFINEs I really like

#define cgp(x,y) CGPointMake(x,y)#define cgpz CGPointMake(0,0)
#define cgf(x,y,w,h) CGRectMake(x,y,w,h)

#define NUM(i) [NSNumber numberWithInt:i]#define NUMF(f) [NSNumber numberWithFloat:
f]// etc if you need, I don’t// this one is MUST HAVE EVER#define IMG(name)
[UIImage imageNamed:name]#define NSTR(patern, value) [NSString stringWithFormat:patern,
value]#define NSTR2(patern, value1, value2) [NSString stringWithFormat:
patern, value1, value2]#define INT2STR(i) [NSString stringWithFormat:@"%d",
i]#define FLT2STR(i) [NSString stringWithFormat:@"%0.2f", f]// etc if you needThe most important reason not to use #DEFINE is code readability, imho. Everyone can define own abbriveations and then get dead code. I mean not an Unreachable code, but code that nobody else can support effectively, even an author after months left. It’s easy to get addicted and generate shorteners for every partially doubled line.

That’s why I propose only helpful #defines I use almost in every project. They look very simple and even obvious. I place them in the precomiled *.pch file to be availabe in all sources.

This helps with coordinates:

This helps with string creation and constants:

Other small objects:

There are 10 more for subviews creation, but it’s more like an approach than promitive shortneners. I’ll describe that later with samples.

I’ve googled a little and there are some nice links on that:

I suppose it helps begginers only. Others can write their experience in comments.

18 JulWhat I’ve already used more than once from iOS 4.0

Want several code snippets on iOS 4.0 features to share.


1. iAds and ADBannerView

In Alterplay we have free apps with US targeting. It’s good possibility to try iAds with that.

Check if there is a ADBannerView and then create it programmatically. You can’t do it in Interface Builder if iOS 3.0-3.1.3 is required to support.

#import

bannerIsVisible = NO;if (NSClassFromString(@”ADBannerView”)){ // create banner banner =
[[ADBannerView alloc] initWithFrame:CGRectZero]; banner.frame = CGRectMake(0, -50, 320,
50); banner.alpha = 0.0; banner.requiredContentSizeIdentifiers = [NSSet setWithObject:
ADBannerContentSizeIdentifier320x50]; banner.currentContentSizeIdentifier =
ADBannerContentSizeIdentifier320x50; [self.view addSubview:banner]; banner.delegate=self;
bannerIsVisible=NO;}

Then just two methods from ADBannerViewDelegate. I show iAds on views with browser, so I
need to move a UIWebView also. ‘TrackPage()’ is Google Analytics function to gather
statistics. I’ll make separate post for it.

- (void)bannerViewDidLoadAd:(ADBannerView *)aBanner{ TrackPage(@”bannerViewDidLoadAd”);
if (!bannerIsVisible) {  [UIView beginAnimations:@"animateBannerAppear" context:NULL];
aBanner.alpha = 1.0;  aBanner.frame = CGRectOffset(aBanner.frame, 0, 50);  webBrowser.
frame = CGRectMake(0, aBanner.frame.size.height, 320, 416-aBanner.frame.size.height);
[UIView commitAnimations];  bannerIsVisible = YES; }} – (void)bannerView:(ADBannerView *)
aBanner didFailToReceiveAdWithError:(NSError *)error{ if (bannerIsVisible) {  [UIView
beginAnimations:@"animateBannerDissappear" context:NULL];  aBanner.alpha = 0.0;  aBanner.
frame = CGRectOffset(aBanner.frame, 0, -50);  webBrowser.frame = CGRectMake(0, 0, 320, 416);
[UIView commitAnimations];  bannerIsVisible = NO; }}

2. inApp SMS and MFMessageComposeViewController

Again, it’s need to support older iPhoneOS, so ‘NSClassFromString’ helps. iPodTouch is
one more case to check. When it’s older iPhoneOS my suggestion is to copy message and
ask user to open SMS app. ‘LOCAL’ is my define for localized text constants. Think,
I’ll share my defines with another post.

+ (void) SendSMSFrom:(UIViewController*)ctrl withText:(NSString*)text{ if
(NSClassFromString(@”MFMessageComposeViewController”) &&
[MFMessageComposeViewController canSendText]) {  MFMessageComposeViewController *picker =
[[MFMessageComposeViewController alloc] init];  picker.messageComposeDelegate =
[Sharer Shared];  picker.body = text;  [ctrl presentModalViewController:picker animated:YES];
[picker release]; } else  {  [UIPasteboard generalPasteboard].string = text;   UIAlertView
*alert = [[UIAlertView alloc] initWithTitle:LOCAL(@”sharing sms title”)
message:LOCAL(@”sharing sms message”)
delegate:[Sharer Shared]              cancelButtonTitle:LOCAL(@”No”)
otherButtonTitles:LOCAL(@”Open”), nil];  [alert show];  [alert release]; } }
- (void)messageComposeViewController:(MFMessageComposeViewController *)
controller didFinishWithResult:(MessageComposeResult)result{
[controller dismissModalViewControllerAnimated:YES];} – (void)alertView:(UIAlertView *)
alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if ([[alertView title]
isEqualToString:smsAlertViewTitle] && buttonIndex == 1) {
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"sms://+"]]; }}

Now looking Locating and Timing on background. Will post soon.

13 JulWhy you may not need an iPhone

It’s not a pop talking about iPhone 4 and its mythical or real hardware problems. It’s about the iPhone revolution in common.

This is a retelling of a real story where I was asked to advise to buy or not to buy an iPhone. It’s necessary to clarify that an iPhone isn’t sold in our country, Ukraine, and the mobile internet revolution isn’t started yet. I mean there aren’t too much people use mobile internet everyday. It’s not only because its cost and quality, but also because most of people have no need to communicate so intensively and continuously. Also I should remark that a good internet access penetration grew up last few years only, now it’ll take time to understand mobile internet and services.

In spite of this I meet iPhone users everyday on the streets, shops, metro, bus stations, wherever. Could you imagine why they buy it? They don’t know about App Store, except IT guys. Most don’t use iTunes. I don’t mean buying songs and other media, but not using iTunes even to store backups of the data. Never. So why do they buy an iPhone? it’s simple, they do because it’s modern and somebody said it’s cool. I totally believe it’s cool, but not it’s cool to have. The problem is that some users is/will be disappointed. Not most, because not everybody likes to feel like a fool paid $1000. And when I was asked I had to find the reason people are upset – they bought a mobile phone, but have an iPhone:

I think, you probably don’t need an iPhone because:

1. iPhone isn’t a mobile phone. 

It’s more than isn’t a mobile phone only. It does the phone functions indifferently. After years of Nokia-style it looks too complicated or too different and need time to adopt. Battery life time will never be a week, it’s usually 1–2 days with often call or long calls.

2. Call function is secondary. 

iPhone and 200.000 App Store applications creates a special way of using. Buying an iPhone you’ll have to accept its ecosystem rules. You even will need to know that you should accept them. It’s not so obvious in the country without official sales. iPhone helps internet educated people. Other won’t find lots of benefits.