0

I have developed numerous iOS apps over the years so know Objective C reasonably well.

I'd like to build my first web service to offload some of the most processor intensive functions.

I'm leaning towards using my Mac as the server, which comes with Apache. I have configured this and it appears to be working as it should (I can type the Mac's IP address and receive a confirmation).

Now I'm trying to decide on how to build the server-side web service, which is totally new to me. I'd like to leverage my Objective C knowledge if possible. I think I'm looking for an Objective C-compatible web service engine and some examples how to connect it to browsers and mobile interfaces. I was leaning towards using Amazon's SimpleDB as the database.

BTW: I see Apple have Lion Server, but I cannot work out if this is an option.

Any thoughts/recommendations are appreciated.?

Jeremy
  • 883
  • 1
  • 20
  • 41
  • Why AWS SimpleDB instead of a db running on your server? It means an extra trip over the wire for your data, e.g. iPhone <--> server <--> AWS. If using Mac as server, can you use MySQL? I'm assuming that since your consider your Mac as a viable option to serve the web service that scalability isn't a big concern. – FluffulousChimp Jun 09 '12 at 11:11
  • Hi Alan: I was thinking on SimpleDB to help address (in the medium term) the issue of scalability. If I needed to move off the Mac, at least the database was reusable so to speak. – Jeremy Jun 09 '12 at 12:17

1 Answers1

0

There are examples of simple web servers out there written in ObjC such as this and this.

That said, there are probably "better" ways of doing this if you don't mind using other technologies. This is a matter of preference; but I've use Python, MySQL, and the excellent web.py framework for these sorts of backends.

For example, here's an example web service (some redundancies omitted...) using the combination of technologies described. I just run this on my server, and it takes care of url redirection and serves JSON from the db.

import web
import json
import MySQLdb

urls = (
    "/equip/gruppo", "gruppo",  # GET = get all gruppos, # POST = save gruppo
    "/equip/frame", "frame"
)

class StatusCode:
    (Success,SuccessNoRows,FailConnect,FailQuery,FailMissingParam,FailOther) = range(6);

#   top-level class that handles db interaction
class APIObject:
    def __init__(self):
        self.object_dict = {}       # top-level dictionary to be turned into JSON
        self.rows = []
        self.cursor = ""
        self.conn = ""

    def dbConnect(self):
        try:
            self.conn = MySQLdb.connect( host = 'localhost', user = 'my_api_user', passwd = 'api_user_pw', db = 'my_db')
            self.cursor = self.conn.cursor(MySQLdb.cursors.DictCursor)
        except:
            self.object_dict['api_status'] = StatusCode.FailConnect
            return False
        else:
            return True

    def queryExecute(self,query):
        try:
            self.cursor.execute(query)
            self.rows = self.cursor.fetchall()
        except:
            self.object_dict['api_status'] = StatusCode.FailQuery
            return False
        else:
            return True

class gruppo(APIObject):
    def GET(self):
        web.header('Content-Type', 'application/json')
        if self.dbConnect() == False:
            return json.dumps(self.object_dict,sort_keys=True, indent=4)
        else:
            if self.queryExecute("SELECT * FROM gruppos") == False:
                return json.dumps(self.object_dict,sort_keys=True, indent=4)
            else:
                self.object_dict['api_status'] =  StatusCode.SuccessNoRows if self.rows.count == 0 else StatusCode.Success

                data_list = []
                for row in self.rows:
                    # create a dictionary with the required elements
                    d = {}
                    d['id'] = row['id']
                    d['maker'] = row['maker_name']
                    d['type'] = row['type_name']
                    # append to the object list
                    data_list.append(d)
                self.object_dict['data'] = data_list
                # return to the client
                return json.dumps(self.object_dict,sort_keys=True, indent=4)
FluffulousChimp
  • 9,157
  • 3
  • 35
  • 42
  • Hi Alan: Appreciate the ideas. Trouble is, this looks like I'll have to learn one/several new languages (Python etc). I was hoping there would be a Cocoa web service framework that would take care of the detail behind the scenes. – Jeremy Jun 09 '12 at 13:17