Showing posts with label applications in iphone. Show all posts
Showing posts with label applications in iphone. Show all posts

How to create an iPhone based Web Service

The iPhone provides no disk access. The backup mechanism hides your files into mdbackup archives. Apple disabled attachments for email. So what do you do when you want to share your data with the world. Creating your own web server is one way to provide a connection between your iPhone data and your users.
Building a barebones web server is a lot easier than you might think. To create a server, just listen to a port for incoming requests. You can use a set port number or listen to a random port, as in the code that follows.

This code shows some very basic socket set-up -- and yes, it's very old-style, there are nice CFNetwork solutions that are much cleaner with callbacks -- and a listener loop.
With a port and a listener loop, you can just grab requests from a browser client, process them and send back HTML-compliant data, as you 'll see in part 2. All the web request processing is handled in a separate method, handleWebRequest which pulls the actual data from the socket, interprets it and then responds to it.

Sockets are a great way to work with the iPhone for all sorts of applications -- serving web data like this is just one application. You can also do remote application control, general computer-to-computer communication, and more. Once you set up the socket, you can use your imagination to define what information to pass.

- (void) startServer
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int socketfd;
socklen_t length;
static struct sockaddr_in cli_addr;
static struct sockaddr_in serv_addr;
// Set up socket
if((listenfd = socket(AF_INET, SOCK_STREAM,0)) <0)
{
isServing = NO;
return;
}
// Serve to a random port
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = 0;

// Bind
if(bind(listenfd, (struct sockaddr *)&serv_addr,
sizeof(serv_addr)) <0)
{
isServing = NO;
return;
}
// Find out what port number was chosen.
int namelen = sizeof(serv_addr);
if (getsockname(listenfd, (struct sockaddr *)&serv_addr,
(void *) &namelen) < 0)
{
close(listenfd);
isServing = NO;
return;
}

chosenPort = ntohs(serv_addr.sin_port);
// Listen
if(listen(listenfd, 64) < 0)
{
isServing = NO;
return;
}
// Respond to requests until the service shuts down
while (1 > 0) {
length = sizeof(cli_addr);
if((socketfd = accept(listenfd, (struct sockaddr *)&cli_addr,
&length)) < 0)
{
isServing = NO;
return;
}
[self handleWebRequest:socketfd];
}
[pool release];
}

Creating an iPhone Project

The iPhone SDK provides several project templates to get you up and running developing your application. You can choose from these types of application:
  • Navigation-Based Application. An application that presents data hierarchically, using multiple screens. The Contacts application is an example of a navigation-based application.
  • OpenGL ES Application. An application that uses an OpenGL ES–based view to present images or animation.
  • Tab Bar Application. An application that presents a radio interface that lets the user choose from several screens. The Clock application is an example of a tab bar application.
  • Utility Application. An application that implements a main view and lets the user access a flipside view to perform simple customizations. The Stocks application is an example of a utility application.
  • View-Based Application. An application that uses a single view to implement its user interface.
  • Window-Based Application. This template serves as a starting point for any application, containing an application delegate and a window. Use this template when you want to implement your own view hierarchy.
If you need to develop a static library for use in an iPhone application, you can add a static library target to your project by choosing Project > New Target and selecting the Static Library target template in the iPhone OS/Cocoa Touch list.
Static libraries used in iPhone applications do not need to be code signed. Therefore, you should remove the Code Signing Identity build setting definition from the static library targets you create. To do so:
  1. Open the static-library target’s Info window and display the Build pane.
  2. In the Code Signing group, select the Any iPhone OS Device conditional definition for the Code Signing Identity build setting.
  3. Change the conditional definition’s value from iPhone Developer to Don’t Code Sign.

To develop an iPhone application, you work on an Xcode project. And you do most of your work on projects through the project window, which displays and organizes your source files and other resources needed to build your application. This window allows you to access and edit all the pieces of your project.