PHP Notes

Cron Jobs in Cpanel

The command to run:

/usr/local/bin/php -f/home/[username]/public_html/[fileName].php

Next you’ll want to select an option from all the select boxes. Remember to select an option in each box. If you want something to run every day at 1AM, select Minute: 0; Hour: 1; Day: Every; Month: Every; Weekday: Every;

Happy Coding :)

Sending Email with Attachments

This tutorial gives you an idea on how to send emails in objective c with attachments.

First we are aware that iPhone has this capablities already. But the question is how could we be able to use this feature in our own applications?

Here is the answer: read.

Anyway, there are two ways to implement emails sending in Objective C.

You could either use the UIApplication or the MFMailComposeViewController in MessageUI Framework.

Here is the procedure on sending an email using UIApplication.

Attach the code from this tip to a button, you can give your users a way to promote your app to their friends via the iPhone’s built in Mail app.

- (void)sendEmailTo:(NSString*)to withCC:(NSString*)cc withBCC:(NSString*)bcc withSubject:(NSString*)subject withBody:(NSString*)body {
	NSString * url = [NSString stringWithFormat:@"mailto:?to=%@&cc=%@&bcc=%@&subject=%@&body=%@",
					  [to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [cc stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [bcc stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
					  [body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
 
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

NOTE: Running this function in the iPhone Simulator will have no effect because the simulator does not include the iPhone’s Mail app. To see the effects of this function, it must be run on a device.

Usage

Once you have that message defined in one of your classes, you can open the Mail app with a pre-composed email with just one message call. The following example opens an email and specifies a to, cc and bcc address.

[self sendEmailTo:@"me@me.com"
           withCC:@"cc@cc.com"
          withBCC:@"bcc@bcc.com"
      withSubject:@"Subject"
         withBody:@"<p>Hello, <a href='http://zaldzbugz.wordpress.com'>Hello</a>, I am here!</p><p><a href='http://google.com'><img src='image source here...'/></a></p>"];

Next is using the MessageUI Framework.

//First we need to reference the Message UI framework.Then import
<MessageUI/MessageUI.h> and <MessageUI/MFMailComposeViewController.h>
//Here is the framework link incase you forgot. Macintosh
HD>Developer>Platforms>iPhoneOS.platform>developer>SDKs>iPhoneOS3.0.sdk>System>Library>Frameworks

//Add this protocol to any interface you were using
<MFMailComposeViewControllerDelegate>

Then add this code in you implementation file.

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
 [self dismissModalViewControllerAnimated:YES];
}

-(IBAction)mailIt {

 MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
 picker.mailComposeDelegate = self;

 [picker setSubject:@"This is a sample Subject"];

 //image...
 UIImage *roboPic = [UIImage imageNamed:@"podd.jpg"];
 NSData *imageData = UIImageJPEGRepresentation(roboPic, 1);
 [picker addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"podd.jpg"];

 //audio

 NSString *emailBody = @"This is a sample Body content.!";
 [picker setMessageBody:emailBody isHTML:YES];

 [self presentModalViewController:picker animated:YES];
 [picker release];
}

Happy Coding…:)

UITableViewCell – Customization

This is how you call your UITablewVieCell class inside your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. (Considering that you were using a NIB – viewbased).

static NSString *CellIdentifier = @"Cell"

CustomCell *cell = (CustomCell  *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) 
NSArray *cellObject =[[NSBundle mainBundle]  loadNibNamed:@"CustomCell" owner:nil  options:nil];

for(id currentCell in cellObject) {
if([currentCell isKindOfClass:[UITableViewCell class]]) {

cell = (CustomCell*)currentCell;
cell.memberVariable.text = @"text";
break;

}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;

Enjoy. :)

UIViewControllers – Modal Views

This post helps you with pushing or presenting modal views from a uiviewcontroller.

There were two ways to do this.

First is Without using a NIB. (just a UIView class)

See Code below.

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
test1View *m_test1View = [[test1View alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
m_test1View.m_viewController = self;   //when you are assigning a delegate variable to a viewController.
self.view = m_test1View;
[m_test1View release];
}

Second is using a NIB. See code below.

ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:viewController animated:YES];

Enjoy. :)

UITextField – Dissmiss Keyboard upon return

First of all, make sure that your UITextField object has delegate reference to the Files Owner and included the Delegate class <UITextFieldDelegate>.

Then you could use this code below.

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}

Enjoy.


Using NSScanner

Before we start with sample code using NSScanner, let us have a quick over of NSScanner from Apple.

The NSScanner class is an abstract superclass of a class cluster that declares the programmatic interface for an object that scans values from an NSString object.

An NSScanner object interprets and converts the characters of an NSString object into number and string values. You assign the scanner’s string on creating it, and the scanner progresses through the characters of that string from beginning to end as you request items.

Because of the nature of class clusters, scanner objects aren’t actual instances of the NSScanner class but one of its private subclasses. Although a scanner object’s class is private, its interface is public, as declared by this abstract superclass, NSScanner. The primitive methods of NSScanner are string and all of the methods listed under “Configuring a Scanner” in the “Methods by Task” section. The objects you create using this class are referred to as scanner objects (and when no confusion will result, merely as scanners).

You can set an NSScanner object to ignore a set of characters as it scans the string using the setCharactersToBeSkipped: method. The default set of characters to skip is the whitespace and newline character set.

We could now go through our sample code.

NSString *sampleString = @”<span>ZALDY</span>”;           //our sample string
NSString *separatorString = @”</span>”;                                  //we set our separator string
NSScanner *aScanner = nil;
NSString *container = nil;
aScanner = [NSScanner scannerWithString:sampleString];   //we initiate our scanner with string

[aScanner setScanLocation:0];                                                       //we set location to 0
[aScanner scanUpToString:@"<span>" intoString:nil];           //we scan string up to a delimiter -> <span>
[aScanner scanString:@"<span>" intoString:nil];                      //we scan string with delimiter -> <span>
[aScanner scanUpToString:separatorString intoString:&container];     //we store to container any result between the <span> and </span> delimiters.

Now if we output the value of “container”, we can get the value = “ZALDY”.

Happy Coding.

Fetch Decimal Digit in NSString

In order to fetch decimal digits from charater set of your NSString. You need to Trim the String then invert the set of decimal digit character set.

See example below which returns value 20.

NSString *mString = @”My number is 20″;
NSString *digitsString = [mString stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSLog(@”Decimal Value: %i”, [digitsString intValue]);

Happy Coding…

Removing HTML tags

- (NSString *)flattenHTML:(NSString *)html {

NSScanner *theScanner;
NSString *text = nil;

theScanner = [NSScanner scannerWithString:html];

while ([theScanner isAtEnd] == NO) {

// find start of tag
[theScanner scanUpToString:@"<" intoString:NULL] ; // find end of tag [theScanner scanUpToString:@">" intoString:&text] ;

// replace the found tag with a space
//(you can filter multi-spaces out later if you wish)
html = [html stringByReplacingOccurrencesOfString:
[ NSString stringWithFormat:@"%@>", text]
withString:@” “];

} // while //

return html;

}

Useful Javascripts For UiWebView Integration

Other useful JavaScript methods:

  • self.find(<string>); – navigates to the next occurrence of a string
  • window.pageYOffset; – basically tells you the scroll position of the webview
  • window.scrollTo(x, y); – allows you to effectively set the scroll position
  • window.getSelection(); – fetch the highlighted strings in the webview.

Happy coding.

How to get the Highlighted text in UiWebView

Posted a more detailed tutorial to my other blog. See here: http://zaldzbugz.posterous.com/how-to-mark-or-get-the-highlighted-string-ins

Follow

Get every new post delivered to your Inbox.