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.

Adding an external library on Android Studio

No Comments

When I started writing Android apps, I(just like everyone else) used Eclipse. Then Google released Android Studio, a whole new IDE designed for Android development. Things are obviously different from Eclipse. One of my first questions was “How do I import a library into my project?”. It turns out to be very easy!

To import a library click on File > Project structure.

More

Documents folder and UIFileSharing

No Comments

On the iOS, we can only save files on the documents folder. The bundle for example, is not writable. Getting the path of the documents folder is easy. Just use the following code:

NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [[array objectAtIndex:0] copy];

The variable documents will be the path to the documents folder. Now if you want to create an URL from it, use the following code:

NSURL *url = [NSURL URLWithString:documents];

Say you saved a couple files and want your users to be able to transfer it to their computers. On iOS 3.2, Apple introduced UIFileSharing. It allows users to transfer files from their iOS devices to any computer running iTunes. To enable that, just add the Application supports iTunes file sharing line to your info.plist and check the box.

Your users will be able to transfer any file from the documents folder to a computer. To do that, open iTunes then click on your device name. Go to the apps tab and scroll down.

Just drag and drop any file to/from the documents table view.