Iphone Developer

json php mySQL iphone webService

Youll be glad to know i got it working. I think this is what a webservice is, maybe ive been calling it something else.

Ive got the mySQL db sittin on a server in the cloud. There is a php read.php file and a write.php file.

The read php file takes the arguments for user and then reads the mySQL db and returns a json string which iphone reads and presents.

The write file takes the arguments input by the user and posts them to mySQL db.

So is this it? Now im only missing something that the server can use to update a 3rd updatedread.php file that will read the db every so often, get updated data and create an xml file that iphone can go and fetch whenever the user wants to get updated data…

Here is the write part:

$con = mysql_connect("localhost","usr","pass");
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("iglobe", $con);
$sql="INSERT INTO users (name, udid) VALUES('$_POST[name]','$_POST[udid]')";
$sql="INSERT INTO  tags (originudid, latitude, longitude, destintyudid) VALUES
if (!mysql_query($sql,$con))
die('Error: ' . mysql_error());
echo "1 record added to tags";

And here is the read part:


$json = new Services_JSON();

$link = mysql_pconnect("localhost", "usr", "pass") or die("Could not connect");
mysql_select_db("iglobe") or die("Could not select database");

$query = "SELECT * FROM users";
$result = mysql_query($query);

$arr = array();
$rs = mysql_query("SELECT * FROM users");

while($obj = mysql_fetch_object($rs)) {
	$arr[] = $obj;

Echo $json->encode($arr);
//Echo '{:'.$json->encode($arr).'}';


And from your iphone you post like this:

+ (BOOL)updateTags:(NSString *)status{

    NSData *postData = [status dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@""]];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLResponse *response;
    NSError *error;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    return (error == nil);
Iphone Developer

Paparazzi Stanford CS193

I’m starting CS193p this new semester again because the Assignment this time around is Paparazzi, a Flickr client which uses CoreData and it’ll be a good experience!

I’ve started with the static view controllers and noticed I was a little rough around the edges in alloc/initting view controllers (mainly nav and tabbar). However I got thru it.

Then I moved on to Part 2 where you use the plist file to populate the CoreData model which is where I am currently and am stuck!

This time around, unlike the Twitter App, we have to use the plist file to populate the db. I’ve got the object model set up, which by the way, I figured out I can write the class files for person and photo in Paparazzi as for person in Presence and then create a CoreData object model by “importing” those class files! Except now I don’t know if i still need those class files.

So to populate the db we must read the plist file which is an array of dictionaries, right?

Figured it out with a little help (from my friends @the google auditors group).  Its definitely a seasoned programmers trick even though the guy who helped me claims to be a newbie 🙂 :

for (NSDictionary *dict in twitterIds) {}

I couldn’t figure out how to cycle thru the dictionary that was inside the array.  So this worked beautifully.

TWO important concepts:

1) Singletons: Are like the UIApplicationDelegate class.  They are special classes which have certain methods you want to import or access from other class files (which provide certain functionality) and its not bound to your UIApplicationDelegate.  So you dont violate that rule of thumb.  Basically you declare a sharedInstance of that singleton class “FlickrFetcher” in your code, then you can being to call all methods from that class.  I guess later on ill understand why we dont just adopt the class as a protocol, which would seem to me to provide the same functionality.

2) CoreData: Aside from your singleton class declaring the moc, mom and psc…you need to declare a new moc in the class where you will be using the code you ‘access via singleton’.  Then you allocate your objects just like any Class Object Files as the Person objects in Presence from last semester and start using them.  Dont forget to save them to the psc, dont just leave them in the moc or youll lose them 🙂

Translate »