Thursday, May 31, 2012

Using jQuery and ajax with Codeigniter

To integrate jQuery with codeigniter, might sound a bit scary at first, but mind you, it’s not something out of this world. We will implement the same example from the previous jQuery post. We will use the same html page used in previous post of ajax jquery, and will only change how our PHP script will look, making it specific for codeigniter. Thus, making this post a bit vague for those that do not know how this works yet. Again, I recommend all that have not read my previous post, to do so.
The final resulting html page was:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><script language="JavaScript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Display Page</title></head>
<body><button type="button" name="getdata" id="getdata">Get Data.</button>
<div id="result_table">
</div>
<script type="text/javascript" language="javascript">$('#getdata').click(function(){
    $.ajax({            url: "getdata.php",            type:'POST',            dataType: 'json',            success: function(output_string){                    $("#result_table").append(output_string);                } // End of success function of ajax form            }); // End of ajax call

});</script></body></html>

Only one change needs to be done to this page, and it lies within the ajax call. Since we are using codeigniter, then we all know that the standard way of calling a function and a method is through the url and looks something like, the base_url / controller / function / additional parameters. In this particular example, we will create a new controller named profiles. Within the controller, we are going to have a function named: get_all_users(), so in this html page, we are going to change the ajax url parameter to this:

url: "<?php echo base_url().'profiles/get_all_users';?>"


Or if you have removed the index.php then:

http://www.yourdomain.com/products/get_all_users


Now, instead of having that ‘getdata.php’ file, we will create the products controller file for this task we are going to run. It will basically do the same process, except in a ‘codeigniter’ kind of way. Let’s create it:
Note: for the sake of this tutorial, I will place my logic within the controller, but the best practices are to create your logic within a model.
File name: products.php


<?phpclass Products extends CI_Controller{
    function __construct(){        parent::__construct();    }
    public function get_all_users(){
        $query = $this->db->get('products');        if($query->num_rows > 0){            $header = false;            $output_string = "";            $output_string .=  "<table border='1'>\n";            foreach ($query->result() as $row){                $output_string .= "<tr>\n";                $output_string .= "<th>{$row['value']}</th>\n";                $output_string .= "</tr>\n";            }            $output_string .= "</table>\n";        }        else{            $output_string = "There are no results";        }
        echo json_encode($output_string);    } }?>



This logic is a bit simpler than the original post, but it’s just to get the idea on how to create an ajax, jquery connection using codeigniter. The html page should look something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="JavaScript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Display Page</title>
</head>
 
<body>
<button type="button" name="getdata" id="getdata">Get Data.</button>
 
<div id="result_table">
 
</div>
 
<script type="text/javascript" language="javascript">
$('#getdata').click(function(){
 
    $.ajax({
            url: "<?php echo base_url().'profiles/get_all_users';?>",
            type:'POST',
            dataType: 'json',
            success: function(output_string){
                    $("#result_table").append(output_string);
                } // End of success function of ajax form
            }); // End of ajax call
 
});
</script>
</body>
</html>

Wednesday, May 30, 2012

Accelerometer. It's simple

The simplest way to use accelerometer in an iPhone application is UIAccelerometer class:

    UIAccelerometer* accelerometer = [UIAccelerometer sharedAccelerometer];
This code above shows how to get the accelerometer instance in the code.
The following line sets up the update interval:
    [accelerometer setUpdateInterval1.0 / 10.0f];
    [accelerometer setDelegate:self];
each 0.1 second the accelerometer will update the program that implements delegate method:
 - (void)accelerometer:(UIAccelerometer *)acel didAccelerate:(UIAcceleration *)aceler 
{
    NSLog(@"acceleration.x = %+.6f", aceler.x);
    NSLog(@"acceleration.y = %+.6f", aceler.y);
    NSLog(@"acceleration.z = %+.6f", aceler.z);
}
Do not forget to add UIAccelerometerDelegate, for example, to a view controller class:

@interface ViewController : UIViewController <UIAccelerometerDelegate>


So here is the step-by-step scenario for beginners:
1. Create new Single-View project in Xcode.
2. In ViewController.h file add <UIAccelerometerDelegate> after UIViewController.
3. In ViewController.m file, modify viewDidLoad method:
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIAccelerometer* accelerometer = [UIAccelerometer sharedAccelerometer];
    [accelerometer setUpdateInterval1.0 / 10.0f];
    [accelerometer setDelegate:self];
}
4. In ViewController.m file, add new method:

- (void)accelerometer:(UIAccelerometer *)acel didAccelerate:(UIAcceleration *)aceler 
{
    NSLog(@"acceleration.x = %+.6f", aceler.x);
    NSLog(@"acceleration.y = %+.6f", aceler.y);
    NSLog(@"acceleration.z = %+.6f", aceler.z);
}
This program works on iPhone. It does not make much sense to test it simulator.

Find all words in a sentence

This is one of the first samples in Mac OS X Developer Library. I found it in Cocoa Fundamental Guide



#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        NSArray *param = [[NSProcessInfo processInfoarguments];
        NSCountedSet *cset = [[NSCountedSet allocinitWithArray:param];
        NSArray *sorted_args = [[cset allObjects]
                                sortedArrayUsingSelector:@selector(compare:)];
        NSEnumerator *enm = [sorted_args objectEnumerator];
        id word;
        while (word = [enm nextObject]) {
            printf("%s\n", [word UTF8String]);
        }
        
        [cset release];
        
    }
    return 0;
}

here is the Terminal Window:


I payed attention on this sample, because it shows a useful trick: how to sort and enumerate data, for example an NSDictionary object.

Sunday, May 27, 2012

Cocos2d Menu Tutorial

Introduction to In-App Purchases


One of the great things about being an iOS devleoper is that there are a variety of models we can use to make money off of our apps, including paid, free with ads, and in-app purchases.
In-app purchases are a particularly compelling option, for several reasons:
  • You can earn more money than just the price of your app. Some users are willing to spend a lot more on extra content!
  • You can release your app for free (which is a no-brainer download for most people), and if they enjoy it they can purchase more.
  • Once you have it implemented, you can keep adding additional content in the future to the same app (rather than having to make a new app to earn more money!)
In the latest app I’m working on (Wild Fables, check out a sneak peek!), I’ve decided to make the app free with one story included, and more available as in-app purchases.
In this tutorial, you’ll learn how to use in-app purchases to unlock local content embedded in your app. I’ll show you how you can deal with the tricky asycnchronous nature of in-app purchases. Take this advice with a grain of salt, as my app is still in development – but I’ll update it with any lessons learned as I go! :]
This tutorial assumes familiarity with basic iOS programming concepts. If you’re still new to iOS development, check out some of the other tutorials on this site.

Friday, May 25, 2012

Getting Started with Cocos2d


Cocos2D for iPhone is a great open source framework that makes it easy to make some very impressive 2d games with much less effort than having to know OpenGL directly or rely on a third party system that might have performance issues or be unnecessarily bloated.   From the official cocos2d for iphone website:
Cocos2D for iPhone is a framework for building 2D games, demos, and other graphical/interactive applications. It is based on the cocos2d design: it uses the same concepts, but instead of using python it uses Objective-C.”

How to play music in your app


n this tutorial we will have a look at how to use MediaPlayer framework to play music from your app. To begin with we need to#import <MediaPlayer/MediaPlayer.h>. There are three core classes that we will work with.
MPMusicPlayerControllerThere are two types of MPMusicPlayerController
  • Application music player ([MPMusicPlayerController applicationMusicPlayer])that plays music just in your app and does not affect the in-built iPod
  • iPod music player ([MPMusicPlayerController iPodMusicPlayer]) that employs the in-built iPod to play music – the music can be controlled in the iPod app and remains playing even after you quit your app
MPMusicPlayerController methods are quite self-explanatory:
  • - setQueueWithQuery: and - setQueueWithItemCollection are used to set playback queue with query (see below)
  • - play, – pause, – stop, – beginSeekingForward, – beginSeekingBackward, – endSeeking, – skipToNextItem, – skipToPreviousItem, – skipToBeginning are all used to control the player
  • currentPlaybackTime, nowPlayingItem, playbackState, repeatMode, shuffleMode, volume are all properties of the player that you can use to change the behavior or display different attributes

iPhone App Development Tutorial


Part 1.  Content
- Introduction to iPhone Programming
- Getting Setup
- Programming Fundamentals
- Download Links:

Wednesday, May 23, 2012

Change Project Nam -Xcode


For Xcode 4 or later:
  • Open a project
  • Select Project Navigator
  • Highlight project name
  • Single click on project name
    enter image description here
For Xcode 3:
  • Open a project > Menu > Project > Rename ...

Error: No suitable application records were found


To be more precise. Here's how you move your app to the "Waiting for Upload" status...
Starting from the "Manage Your Apps" window in iTunes Connect, click the big icon for your app.
That will bring you to the "App Information" and "Versions" window.
In the Versions section click the View Details button.
In this next window there should be a button on the top right that says something like "Upload Binary." Click that button.
There's where you answer about encryption. Answer those questions about encryption and your app should be moved to the Waiting for Upload status.
Go back to the App Information Versions window. It should say "Waiting for Upload" in the Versions section.
Nowadays you will be informed specifically if you have a Bundle ID mismatch when you try to do the upload from your Xcode organizer. It'll even tell you which Bundle ID you have in iTunes Connect and which Bundle ID your app has.

Popular Posts