January 20, 2012

A few recipes about dates / using NSDate

The app I’ve been working lately makes a bit more use of NSDate than my usual ones. I thought I’d share with the world a few pieces of code I’ve been using;

How to test if two NSDate dates belong to the same day:

I created a category on NSDate and added the following method to it:

-(BOOL)isSameDay:(NSDate *) otherDate
{
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents *comps_self = [[NSCalendar currentCalendar] components:unitFlags fromDate:self];
    NSDateComponents *comps_other = [[NSCalendar currentCalendar] components:unitFlags fromDate:otherDate];

    if ([comps_self day]==[comps_other day] &&
        [comps_self month]==[comps_other month] &&
        [comps_self year]==[comps_other year]) {
        return YES;
    }
    return NO;
}

I can now call [mydate isSameDay:otherdate]

Find out the first and last days in the month that contains a given date

-(void) updateMonthIntervalForDate:(NSDate*)date{
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents* dc = [[NSCalendar currentCalendar] components:unitFlags fromDate:date];
    dc.day=1;
    NSDate* startOfMonth = [[NSCalendar currentCalendar] dateFromComponents:dc];
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    offsetComponents.month=1;
    NSDate* endOfMonth = [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents toDate:self.startVisibleDate options:0];
    // ... do whatever you want with startOfMonth and endOfMonth
}

Last but not the least, print out nicely Print out the full name of the month, or the abridged name

First of all, create a NSDateFormatter; the docs says it’s not inexpensive, so if you need it several times, try and reuse it instead of recreating it over and over again.

    dateFormatter = [[NSDateFormatter alloc] init];

Next, decide the date format you want to output

Abridged version (like 01 Jan)
    [dateFormatter setDateFormat:@"dd MMM"];
Full month name (January)
    [dateFormatter setDateFormat:@"MMMM"];

Sure, this is in the docs as well, but I did quite a bit of searches to find out the part about MMMM for the full NSDate month name. So maybe it will help someone.

Once you have the date formatter ready, you can invoke it to get the nice string from your NSDate:

     NSString* mystring = [[dateFormatter stringFromDate:crtEntry.date] uppercaseString];

This was it. I wish you happy coding.

January 6, 2012

.. And a New Year

XMas is gone, New Year’s is gone, the holidays are over and I am back to work. Same old stuff. Like Dilbert says,  there shouldn’t be anything special about a  random point in the space-time continuum.

But there is something special. Each new year is the perfect reminder that time flies and we should do something with our lives.. something more than merely living them. It’s also the perfect moment to look back, draw the line and have some accounting done – what we did this past year, how it fared compared to the previous ones and, most importantly, what we expect from the future.

For me, the past year was the year of Clean Writer. First Clean Writer for Mac, then Clean Writer Pro for Mac, and at the last minute Clean Writer for iPad 2.0 . There were also major updates to Self Help Classics and Business Inspiration Classics, and a few other minor apps (like SideTodo for Mac and some simple iOS apps  that nobody cares about). I finally started my own company – Cognitive Bits, lost months negotiating business contracts which failed at the last moment, lost some more months developing products that I couldn’t possibly sell. I started a google group for Bucharest iOS & Android devs and organized a few meetups, met some really nice iOS devs at the London Tech Talk and learned a few very important lessons that I will list below.

1. If you like what you’re doing, keep doing it.

I won’t go into much details here. Bottom line is, I love what I am doing as a job(working on my own apps and projects) and I hope I’ll be able to keep doing it. I don’t like working for clients, so I hope I won’t be forced to do it.

2. Learn from the source.

Stackoverflow is great, and so are developer forums around the web. But I should rely more on Apple’s docs, the WWDC videos and iOS development books. Don’t waste time learning from bad teachers when you have access to the best ones.

3. Motivation comes from like-minded people

Meeting other iOS devs from the UK or Bucharest and listening to their success stories was the most energizing thing that could have happened to me in the past year. I hope 2012 will harvest the results of this newly found motivation.

4. Inspiration comes from non-like-minded people

I think that good ideas come from the interaction of people with different interests (you know, Art vs Technology kind of stuff). Sure, some believe their ideas are golden – “I have a great app idea, you implement it and we split the revenue“; I learned to avoid them politely(as in “sorry, I’m a bit swamped right now, maybe we’ll do this later“). But overall, talking to others is a great thing and it gives you new insights and ideas. Use this.

5. Last, but not least: Polish your app

Great ideas with no polish have little chance of being successful. Great polish with no decent idea … that’s not great either. Bottom line: don’t throw away the effort you have put into developing your app, only because you didn’t take a bit more time to polish it. Yep, this comes from my own experience.

 

I want the above five to be my guidelines for 2012, and I hope that these insights will help this year to be significantly more successful than the previous.

December 23, 2011

Adjusting UITextView size on Keyboard show/hide events, for iOS5

The time has come to, once again, blog over here as part of #idevblogaday.

Remember the project I was telling you last time? I can reveal it now, as it’s been on the AppStore for a while now – Clean Writer for iPad, 2.0, a fully recoded, feature rich redesigned version of my quite popular iPad plain-text editor for writers.

One thing that plagued the previous version was how the keyboard would cover up the text entry textview. I had tried to fix it by automatically adjusting the text view size, but there were always problems.

I ended up with a fix based on a recipe from Pragmatic Programmer’s iPad Programming book.

* Read the rest of this entry » *

December 9, 2011

UITextView, Apple quirks and cutting one’s losses

I’m in the middle of a project which excites me a lot, especially because I want to have it finished in the next couple of days, so that with a bit of luck the app would go live to the appstore before December 22 (the dreaded date when iTunesConnect will shut down for an entire week). I’ve only had 3.5 hours of sleep last night, and about 5 the night before.

One of the things I was doing at 5AM in the morning was trying to find the current point location of the cursor inside a UITextView (iOS 4.3+ or even 5.0 – whichever works).

I couldn’t.

Now, it’s not unusual to not be able to do things in iOS. What is unusual is that Apple’s docs said I should have been able to.

Let me explain. See, UITextView, according to the documentation, conforms to UITextInput protocol. UITextInput protocol, according to the documentation, has a required property selectedTextRange that gives “the range of the selected text in a document” and, in particular, the caret(insertion-point). It should be straightforward.  (PS. do not mistake selectedTextRange for selectedRange which returns just the range of the selection inside the text string.

To my frustration and despair, it didn’t work. My code would crash again and again complaining that selectedTextRange is an unrecognized selector. Even more frustrating, Google doesn’t offer any clue (except for one stackoverflow question and the fact that people enjoy giving stupid answers although they don’t understand the question). Or that others don’t think it’s possible.

So I gave up implementing what I wanted, cut my losses and moved on to implementing some other features. But if you have any idea about a decent way to achieve this (and, no, I don’t really want to use a custom 3rd party uitextview clone just to be able to know the CGPoint position of the cursor), I’d greatly appreciate your input.

Until then, ain’t it weird that a required property in a required protocol doesn’t seem to be  fully implemented in Apple’s one of the most important core UI objects?

November 25, 2011

Quick introduction to Grand Central Dispatch and why you should use it

Hello idevs! Time sure flew by lately, so here I am, once more having to post one of my development adventures, forced by the unwritten agreement I took when joining the iDevBlogADay project.

As any programmer can tell you, in most (over 95%) of your programming tasks you won’t have to deal with threads or any form of asynchronous programming. Chances are you will never need to deal with this, in most programming jobs. However, in iOS world, you have to. And unless people scare you upfront by pushing big words like threads and asynchronous programming, you should totally spend 5 minutes to at least grasp the big picture.

* Read the rest of this entry » *

November 11, 2011

What I learned from TechTalk 2011, London

I guess it's my time again for #idevblogaday. I'll keep this post short, as I'm still struggling with a bug that prevents me from submitting Clean Writer Pro.

At the beginning of this week I was at Apple's iOS5 TechTalk in London. It was great. Apart from the rare opportunity of meeting great iOS developers from around the world(I met awesome guys from UK, Vietnam, Germany, Canada, Romania and more), the talks day also brought me a (hopefully long-lasting) inspiration and motivation. In particular, there are three most important things I came back with:

  • Try to create innovative, distruptive and industry-changing apps. Second best is to make them awesome and distinctive.
  • Don't get stuck in the past. Although it's obviously cool to support old devices/OS versions, it's most often not worth the effort. Aim to use latest tech for the latest devices, as they are the most popular (of the 250 million total iOS devices, 50% have been sold in the past year – that's one huge market)
  • Go international & localize. There are lots of non-US iOS owners.

Two less important but still interesting things I learned:

  • Always try to learn. Past WWDC videos and iOS video tutorials on iTunesU are way worth the effort to download and study.
  • There's no such thing as a small niche. It all depends on the app and your marketing plan.

3+2 ideas. That's it. But they were totally worth the trip. Talk to you later.

November 1, 2011

Compacted Google Reader

After Google Reader’s UI change of today I, like millions of users, was furious. I won’t go into detail about the reasons (you can read them on my Romanian blog), but summing them up is easy: the new UI was probably designed on a couple of 30″ computer screens and is a visual insult to anyone trying to use it on a normal, smaller, screen. Regardless of the colors and fonts used, the new Google Reader UI is unusable due to its excessive use of whitespace which fills up the screen with unneeded padding and margins, while the actual content is crammed into a tiny space forcing you to scroll like a mad man.
Solutions started to appear pretty soon after, in the form of user-side scripts using browser extensions such as Greasemonkey or UserCSS.
Being a Safari user myself I followed a Twitter friend’s suggestion and created my own little UserCSS stylesheet which fixes the excessive whitespace issue, while keeping the new style.
If you are curious to try it, check out this page on Github. You can easily insert the style into a Greasemonkey script, if you wish.
From this..

..To this

October 28, 2011

Are you coming to the TechTalks?

One week ago, Apple announced its iOS5 Tech Talk World Tour, with a great talk agenda prepared. The announcement email took a bit longer to reach me, so I first learned about it from Twitter. Needless to say, I instantly applied. My target of choice? London, obviously – I knew there’s a great iOS community in UK which I’d love to meet, I had never been there and also wanted to combine tourism with business (can we call it business if it’s a talk for developers teaching them new tech tricks to use in their apps?). Eventually, after 5 long days of waiting, I got notified of having been accepted to the London tech talk (I had access to two iOS accounts and had also applied to Rome to double my chances – I was only accepted to London, though).

So… London, here I come!

Due to the late notice(trying to book a flight with less than 14 days before the flight) I missed the last economy seats on the direct British Airlines flight, my only option was Lufthansa with a pretty awful schedule, risky plane changes in Germany and more expensive with over 50%. Also, it turns out that I might not be able to visit London at all, as I’ll be arriving late in the evening of the 6th and will leave the on the 8th at noon. Well, that’s how life is sometimes, you can’t have it all. I wasn’t planning to be too much of a tourist, but I had hoped to take a peek at some standard London attractions; but, after all, that’s not the reason why I’m going there.

Anyways, to conclude this useless post, I was wondering if any of the iOS developers reading this blog are coming there – if so, why don’t you leave a comment with your blog url/twitter so maybe we can get together during one of the breaks and, you know, socialize? If you’re not coming to the London Tech Talk on November 7th but to another one, where is that? Still curious to find out! :)

October 25, 2011

More AppStore scams – Flash Video Exposer

While casually browsing the AppStore I noticed an intriguing app called “Flash Video Exposer” which, at the time, was #1 Top Paid app in the Romanian iPad App Store. It was also in the top 30 paid apps in the iPhone App Store. Problem is, all user reviews unanimously cry that this app is a scam, and a pretty expensive one actually (8 euro).

Curious to learn how it was possible that an app whose users call a fake can still be in the App Store, I started looking for more information.
It turns out that this is a resubmitted version(under a minor name change) of an app that had been previously removed from the store (we can’t tell if at Apple’s initiative or simply at the developer’s). It used to be called “Flash Video Expose” and, as such, managed to receive many complaints, dedicated blog posts about the scam, and even YouTube reviews exposing it. What matters is that now it’s back, with a minor name twist and a different “developer” listed, selling the same fake features for an even higher price. And, according to the download charts and product reviews, it works.

Don’t bother looking for it in the US App Store; probably having learned its lesson, the developer didn’t submit it there; since the US App Store is under thorough scrutiny from the IT press, popular blogs and, obviously, Apple staff, when you want your scammy app to fly below the radar you should avoid it.

How well does such a scam do? According to AppAnnie, pretty well – it is the most popular/grossing Utility app in tens of App Stores. True, revenue from minor App Stores is significantly smaller than it would have been from the US store but, with a bit of luck, if this scam remains undiscovered for a couple of months, it should bring its author a few hundred thousands in revenue, if not millions of dollars.

If you ask me what Apple could have done to prevent this, my answer is simple: they need to add a visible & easy to access contact form inside iTunes App Store for app complaints. As it is now, people don’t have a clear method to notify Apple of App Store issues such as scams, copyright infringements or ill-behaved apps. Sure, adding this would be a logistic nightmare and likely affect the reputation of the App Store ecosystem, but it’s necessary and way overdue.

Until then, blog posts such as this one and Tweets are the only means we have of letting Apple know that they should do more to protect their users.

October 24, 2011

iPad 1 multi-gestures on iOS5

Since I upgraded my iPad 1st generation to iOS5 I started to look around for the previous Settings menu item – “Multitasking gestures”. For previous OS versions it used to be there, enabled after having put my iPad in development mode, but now it was gone, vanished forever.
I’ve tried to put the iPad in development mode again (by using it to test iOS apps from within XCode) but to no avail. It looked like a bleak future, one where I’d be angry with Apple for downgrading old devices by removing previous features, just so that they could make iPad 2 a bit more appealing.
Luckily a random chat today with an online friend gave me the answer; apparently it has been known to many others since appearing on Lifehacker and Gizmodo : there is a tool in town that does just this – instead of jailbreaking the iPad, it simply activates the Multitasking gestures once more. You can download it from here. And, if you get tired trying to put your iPad in DFU mode following redsn0w’s bad tips, you can try the method in this youtube video, which worked perfectly for me.

Result? Multitasking gestures work again for me, and I’m a happier iPad user.
The end.