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];
if(!result)
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'
}else{
// 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

<html>
<head>
<style>
h1
{
text-shadow: 5px 5px 5px #FF0000;
}
</style>
</head>
<body>

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

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

</body>
</html>

PHP switch Statement

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

Stream Radio

Use the following lines of code to stream radio

NSString *stream = @"http://www.bbc.co.uk/radio/listen/live/r1.pls";
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.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

public class SampleActivity extends Activity
{
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            Keypaint view = new Keypaint(this);
            setContentView(view);
      }    

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

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