[Quickies]Getting UITextView to dismiss keyboard when the user hits the return key

No Comments

It might seem something simple, but I still remember when I was beginning my life as an iOS developer and certain things were not exactly out of the box. UITextView’s support for multiple lines puts it into a complicate situation when it comes to handle the return key. Still, that’s something you can easily implement using a delegate method.

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if([text isEqualToString:@”\n”]){

[textView resignFirstResponder];

return NO;
}
return YES;

}

Using native Objective-C code from UIWebView

No Comments

There are many of frameworks out there like PhoneGap that allow you to write iOS apps using HTML and Javascript. Even if you know Objective-C and/or SWIFT, sometimes a UIWebView might be useful. But what if you need to use functionality that’s not available on webkit? Creating a bridge between Javascript and Objective-C is easier than you think!

Open Xcode and create a new project.

Screen Shot 2015-05-23 at 5.16.03 AM

More

[Quickies]Making an iPhone vibrate

No Comments

Making an iPhone vibrate is very easy and takes only one function. The first thing you have to do is to import the AudioToolbox framework into your project and header file(s). That framework provides two functions you can use:

AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

When the device does not support vibration(e.g. an iPod touch), the device will beep instead of vibrating. If it supports vibration, it will vibrate.

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

This function will vibrate a device that supports vibration. If the device doesn’t support it, it will do nothing.

[Open Source]FVGravityView

No Comments

Hey folks, I’m proud to announce my new open source project for iPhone. It’s called FVGravityView. It’s basically an UIView that has the ability to fall. Yes, fall. It uses the accelerometer to detect the device position and the view will fall depending on it. It’s basically an UIView that recognizes gravity.

A few thing:
-The default velocity is 20, but you may change this value by changing the velocity property.
-FVGravityView supports dragging. To enable it, set the property isDragable to YES.
-Say you want to invert the gravity. Just set the isInverse to YES.
-It’s basically an UIView, so you may add subviews to it. It also has every drawing capabilities that an ordinary UIView has.

To download the sample project, click here.

PS: I didn’t try it on an iPad, but it should work with no problems at all.

RIP Sensitive Code

No Comments

Hey folks, unfortunately, my blog Sensitive Code is dead. I had no time to update it and ended up not renewing the domain registration. It had a great page rank. For example, if you type UIFileSharing on Google, a post from Sensitive Code it is the first result(not sure for how long). I moved every post from there to this blog, so everything is still available. I hope to write about programming on this blog in the future.

Easy multithreading, a courtesy of NSOperation

No Comments

Threading is known for being very hard in any language. A foolish mistake can make your application crash. Programmers spend a lot of time to make sure everything works fine. On Mac OS X 10.5, Apple finally introduced a very easy way to deal with threading. NSOperation is The Apple Way® to deal with threading. All you have to do is to subclass the NSOperation, create and initialize a NSOperationQueue, create and initialize the NSOperation and add it to the NSOperationQueue. For this tutorial, I will teach you how to write a simple app that will load a RSS feed and display it on a NSTableView.

To get started, go to Xcode and create a new project. I will call mine RSS Reader, but feel free to call it whatever you want. Let’s start by editing our AppDelegate.h. Create 3 IBOutlets:

More

[Quickies]Opening URLs in external applications

No Comments

Opening URLs on external applications is easy, no matter if you’re on an iPhone/iPod touch/iPad or on a Mac. On the Mac, we’d do something like this:

NSURL *URL = [NSURL URLWithString:@”http://www.sensitivecode.com”];
[[NSWorkspace sharedWorkspace] openURL:URL];

Simple, uh? Now let’s see how to do this on the iOS:

NSURL *URL = [NSURL URLWithString:@”http://www.sensitivecode.com”];
[[UIApplication sharedApplication] openURL:URL];

Easy peasy :)

ShareKit: sharing made easy

No Comments

Today being social means being cool. I mean, most apps have some sort of a sharing feature. There are tons of social networks, but the ones that hold most attention are Twitter and Facebook. Imagine yourself coding a share feature for each social network. Seems a bit painful, doesn’t it? So here’s the solution for your sharing problems! ShareKit is a framework which contains classes that allows you to share stuff on most popular social networks. ShareKit supports networks like ReadItLater, Facebook and Twitter.

Just copy a couple files to your project, import a header and implement a share feature with a few lines of code, and it’s done! Some networks will require you to get an API key, but ShareKit has a header file for you to configure it. Just paste your API keys and it’s configured.

You can download it and find more information on its official website.

FVImageSequence for Mac OS X/iOS

6 Comments

When I got to know Rainer Brockerhoff, one of my first questions was about networking. One of his advices was to write some open source code. A few days ago I was working on my customer’s app. He needed something similar to Apple’s 360° demo. I did not want to do that on a UIWebView as the performance is not the best. After looking for a view that does that, I found there was no one available. So I decided to write my own. Now I’m releasing it as an open-source project. The first of many.

FVImageSequence is basically a subclass of NSImageView/UIImageView that allows you to create the 360° effect by using a sequence of images. To use this view, you will have to add a image sequence to your project. All images must have the same prefix(i.e. myimage0.png, myimage1.png, myimage2.png). Set the prefix of the images you want to use by setting the prefix property.

You must also set the number of images. Just set the numberOfImages property in order to do that.

Also, don’t forget to set the property extension with the extension of the files(i.e png, jpg, gif).

Optionally you might set the increment property. This property basically tells the view how many images it should increment. If instead of incrementing 1 image, you want to increment 5, just set it to 5.

If you’re using the iOS version, don’t forget to enable the user interaction on Interface Builder.

Download the source code and the sample project from this link.

Fuck the clueless

No Comments

Rainer Brockerhoff used to tell that being clueless makes you get no answers for your questions. In the beggining, I didn’t understand why. Today I’ve finally figured out what a true clueless person is. And I’m talking about a person who has this word tattooed in her forehead.

I was trying to get some good reputation answering questions on Stack Overflow and a guy asked a question about getting information displayed in a custom way on a UITableViewCell. I told them to subclass it and add four UILabels to it. Problem solved, right? Wrong! The guy asked how they should do that. Well folks, I’m not sure if I’m talking about a guy who doesn’t know how to do that. What I know is that they wanted me to do the entire work for them, so they would just copy and paste the code and it would work like a magic. Yes, magic! No work at all. “Let me just get someone to do the work for me”.

Yeah guys, this is THE way to not get your questions answered. When asking something, try to learn from your question. Trying to get other people to do the work for you will not work. I recomend this great article by Matt Gemmell.

Rainer, you were right.

Older Entries