Author Archive

NSString to NSDate

NSString *dateString = @"01-01-2014";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

NSDate convert to NSString:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", strDate);
[dateFormatter release];

Rename files in Documents directory

To get NSDocuments directory use :

NSString *originalfileName = @"MyFile.txt";
NSString *newfileName = @"MyFileNameChanged.txt";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathOriginal = [documentsDirectory stringByAppendingPathComponent:originalfileName];
NSString *filePathNew = [documentsDirectory stringByAppendingPathComponent:newfileName];

You can use moveItemAtPath to rename.

NSError * err = NULL;
NSFileManager * fileManager = [[NSFileManager alloc] init];
BOOL result = [fileManager moveItemAtPath:filePathOriginal toPath:filePathNew error:&err];
NSLog(@"Error: %@", err);
[fileManager release];

If you do not know the names of the files in documents directory you can use :

NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];

This array contains all filenames you need. So you can rename them all.

Comparing UIButton backgroundImage

We can use the following code to compare a UIButton’s background image

if ([[myButton backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed@"myImage.png"]]) {
// Button has a background image named 'myImage.png'
// Button has not a background image named 'myImage.png'

Add an activity indicator as shown here:

UIActivityIndicatorView *activityIndicator =
[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
UIBarButtonItem * barButton =
[[UIBarButtonItem alloc] initWithCustomView:activityIndicator];

// Set to Left or Right
[[self navigationItem] setRightBarButtonItem:barButton];

[barButton release];
[activityIndicator startAnimating];

Remove the spinner and stop animating:

[[self navigationItem] setLRightBarButtonItem:nil];
[activityIndicator stopAnimating];

UIAlertView – a simple text input dialog box

Create the alertView with style UIAlertViewStylePlainTextInput:

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Hello!" message:@"Please enter your name:" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeNumberPad;
alertTextField.placeholder = @"Enter your name";
[alert show];

This produces an alertView:

When pressing any buttons, the regular delegate methods will be called and you can extract the textInput there like so:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"Entered: %@",[[alertView textFieldAtIndex:0] text]);

Detect Retina Display

The following lines of code can be used to detect whether the device is retina display

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00) {
         // Retina Display
    else {
         // Not Retina Display

CSS3 Text Shadow

text-shadow: 5px 5px 5px #FF0000;

<h1>Text-shadow effect!</h1>

<p><b>Note:</b> Internet Explorer 9 and earlier versions, does not support the text-shadow property.</p>


PHP switch Statement

switch (n)
case label1:
code to be executed if n=label1;
case label2:
code to be executed if n=label2;
case label3:
code to be executed if n=label3;
code to be executed if n is different from all labels;

Stream Radio

Use the following lines of code to stream radio

NSString *stream = @"";
NSURL *url = [NSURL URLWithString:stream];
NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
[Webview loadRequest:urlrequest];

Incoming search terms:

  • xcode eCommerce

Drawing Image using Canvas in Android

The following code is for drawing a bitmap using canvas.

package com.pac;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

public class SampleActivity extends Activity
      public void onCreate(Bundle savedInstanceState)
            Keypaint view = new Keypaint(this);

      public class Keypaint extends View
            Paint p;
            protected void onDraw(Canvas canvas)
                  p=new Paint();
Bitmap b1=BitmapFactory.decodeResource(getResources(), R.drawable.im1);
canvas.drawBitmap(b1, 0, 0, p);

            public Keypaint(Context context)
Page 1 of 912345...Last »
We Can Build Your App for You. Lets Get Started !