Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Monday, June 2, 2014

Swift Tutorial Videos

So, you're going to need some video tutorials for Swift on iOS, huh?

We are hard at work creating Swift Tutorial Videos for the masses of developers who are feeling like refugees from Objective-C. Start with the book on Swift of course (https://itunes.apple.com/us/book/the-swift-programming-language), but come back for another round of learning with our forthcoming videos and tutorials!

Go signup to get >0 Swift Video Tutorials in your email inbox each week!

http://www.swifttutorialvideos.com


Read More

Monday, April 14, 2014

iOS NSDate Minus 1 Hour

So, you want to subtract an hour from an NSDate, huh?

If you're coming from another language like Java or Ruby, you're probably pretty familiar with the idea of creating a date by adding or subtracting some time from a Date object. The equivalent function in iOS is as follows:

[SOME_NSDATE_OBJECT dateByAddingTimeInterval:NUMBER_OF_SECONDS]

So, for example, if you wanted to get a date representing one hour from now (60 seconds/minute * 60 minutes/hour), you would do as follows:

NSDate *anHourFromNow = [[NSDate alloc] dateByAddingTimeInterval:60*60];

If you wanted to do an hour ago, you'd add a negative value like so:
NSDate *anHourBeforeNow = [[NSDate alloc] dateByAddingTimeInterval:-60*60];

Thats it! So easy once you know the method calls.

If you like this, or found it helpful, or copied our code to use in your project :). A quick tweet would be awfully nice of you, but we'd settle for a +1 on Google Plus!


Read More

Friday, December 20, 2013

iOS Get App Window

So, you want to get the iOS app window, huh?

Terminology is one of the great barriers to moving from language-to-language and platform-to-platform, so its no surprise that some people are going to want to search for different terms, so we cover a few cases in this post, just to make sure we're getting the answer you're actually after:

Case #1, you're really wanting to get the current UIViewController displayed in your application:

A. When you're using a NavigationController

  // Way #1: Get the topmost ViewController (current) in this navigation hierarchy.
  UIViewController *currentViewController = [[self.navigationController viewControllers] lastObject];

  // Way #2: Ask your UINavigationController which ViewController is the visible one.
  UIViewController *currentViewController = self.navigationController.visibleViewController;

B. When you're not (presumably nested modal views or other)

- (UIViewController *) getTopViewController
{
  UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

  UIViewController *viewController = keyWindow.rootViewController;
  while(viewController.presentedViewController){
    viewController = viewController.presentedViewController;
  }
  
  return viewController;
}

Case #2: You actually want the key window:

  UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

... which is a window that "manages and coordinates the views an app displays on the device screen", and of which your app probably only has one.

Case #3: What you really want is the UIView of the current UIViewController so you can manipulate it somehow, in which case you want to do:

  // I am in a UIViewController, so I can get my view like this
  UIView *view = [self view];

Well, hopefully that covers it, but if not, please leave a comment on a case we might have missed. Thanks, and please give us a +1 if you have the inclination!


Read More

Thursday, December 19, 2013

iOS Get First Day of Year

So, you want an NSDate representing the start of this year, huh?

Well, what's the fun in just getting the start of the current year. Here's a bit of code that will give you the start of the current year as well as the start of any year before or after it:

Here's how you can get the start of this year:


  // Lets get the start of this year, but we could do next year (1) or last year (-1)
  int yearOffset = 0;
  
  // Date representing right now
  NSDate *date = [[NSDate alloc] init];
  
  NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  NSDateComponents * calComponents = [cal components: NSYearCalendarUnit fromDate:date];

  [calComponents setYear:([calComponents year] + yearOffset)];
  

  NSDate *startOfYearWithOffset = [cal dateFromComponents:calComponents];
 

In the above code, its the yearOffset that gives you the ability to get the start of next year (yearOffset = 1) or last year (yearOffset = -1). Hope that helps you.

If you love us with undying passion for what we just taught you, then please give us a +1 and tell your next door neighbors about us... you've been needing a good excuse to introduce yourself anyway.


Read More

iOS Last Day of Week

So, you'd like to get an NSDate representing the last day of the current week, huh?

Well, firstly, lets define what we mean by last day of the week. Of course, we mean Saturday, why wouldn't we, right? Well, there tends to be a lot of discussion around this, and we don't want to meddle with ongoing debates on the merits of treating Sunday as the last day of the week. For our purposes, Sunday is the first day of the week, and Saturday is the last day of the week.

Here's a quick example of how to get the (beginning of) the last day of the week:

  NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

  NSDate *date = [[NSDate alloc] init];
  
  NSDateComponents *calComponents = [cal components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:date];

  // this will give you current day of week
  int dayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:date] weekday];
  
  // Offset to Saturday
  int dayOfWeekOffset = 6;
  
  // this will give you Saturday (the last day of the week)
  [calComponents setDay:([calComponents day] - ((dayofweek) - 1) + dayOfWeekOffset)];

  NSDate *lastDayOfWeek = [cal dateFromComponents:calComponents];

That seems like a lot of code, but all you are doing is breaking down dates into their components and adding and/or subtracting components. Its easy to get it mixed up in your head the first go-round. Best of luck!

If you're viewing this page as a castaway from a deserted island somewhere in the Pacific and you need help, reach out to us, otherwise, if you found this post useful, please give us a +1, thanks!


Read More

iOS NSDate Get Start of Day

So, you want an NSDate representing the start of today, huh?

Sometimes, we like to give you a bit more capability than you ask for, just so you can reuse our code in more circumstances. Here's a bit of code that will give you the start of the current day as well as the start of any day before or after it:

Here's how you can get the start of today:

  int dayOffset = 0;
  
  NSDate *rightNow = [[NSDate alloc] init];
  
  NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  NSDateComponents * calComponents = [cal components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:rightNow];
  
  // Current day of week, with hours, minutes and seconds zeroed-out
  int today = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:rightNow] weekday];
  
  [calComponents setDay:([calComponents day] + dayOffset)];

  NSDate *beginningOfDay = [cal dateFromComponents:calComponents];

In the above code, its the dayOffset integer that gives you the ability to get the start of tomorrow (dayOffset = 1) or yesterday (dayOffset = -1). That should give you plenty of power.

If you found this so helpful that you just want to throw money at us, why not throw it instead at your local charity? And if you don't have any money to throw around, we'll sure take a +1 instead. Thanks!



Read More

iOS Get Selected Rows in a UITableView

So, you want to know what rows are currently selected in a UITableView, huh?

If you're using a tableview to allows single-selection but you do not segue to another controller with the selected record, you may find the need to know what rows are currently selected. If that is your aim, then have no fear, I have some code for you here:


  NSIndexPath *selectedRowPath = [self.tableView indexPathForSelectedRow];


Now, if your tableview's datasource is an array that is a property of your current ViewController, then all is well, and you can get the Object that is actually representative of the selection in the table like so:


  int rowIndex = selectedRowPath.row;  
  NSObject *selectedObject = [self.datasourceArray objectAtIndex:rowIndex];


Hopefully that sets you back on the road to creating value. Speaking of, if you have a mind to give us a +1, please do so, as it creates a lot of value for us. Thank you!




Read More

About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates: http://www.blindnsdate.com

Popular Posts

Designed By Seo Blogger Templates