Monday, August 10, 2009

Notes on iPhone:MapKit, CFNetwork, and Push Notifications Part 1

This is my first tutorial so for those of you here looking for tips and hints bear with me as I get the hang of this.

Alright the first thing I am going to cover is how we can use Push Notifications to keep track of downloads of our app. I am also going to be using MapKit as a means of allowing users to see where others who have used our app are located. It isn't going to look pretty but hopefully the code with help you.

Am hoping to make this a library so that I can just kinda plug it in to any other application I make in the future.

So first step, Lets open up xcode and start a new tab bar applications.
why tab bar?
1. we want one of the screens to be a map
2. we want to display stats of our app.

We are going to handle the view before any code.

Okay first thing we are going to want to do is add a few IBOutlets for UILabels in the first view controller. You could also use a table or any other means you want to display the statistics. Just make sure to incorporate a quick way to change stats.

Note make sure to connect them properly.

Here is what mine looks like: MainWindow.xib

so what do these terms mean:
(removed Installed but was too lazy to change pic)
Open how many people actually opened the app
Removed how many people deleted the app
Reinstalled how many people reinstalled and open the app again.

This is what my FirstViewController.h looks like

#import "UIKit/UIKit.h"



@interface FirstViewController : UIViewController {

IBOutlet UILabel *opened;

IBOutlet UILabel *removed;

IBOutlet UILabel *reinstalled;

}


@property (nonatomic,retain) IBOutlet UILabel *opened;

@property (nonatomic,retain) IBOutlet UILabel *removed;

@property (nonatomic,retain) IBOutlet UILabel *reinstalled;


@end



FirstViewController.m make sure to synthesize and dealloc

#import "FirstViewController.h"



@implementation FirstViewController

@synthesize opened;

@synthesize removed;

@synthesize reinstalled;


- (void)dealloc {

[opened dealloc];

[removed dealloc];

[reinstalled dealloc];

[super dealloc];

}


@end



Now for the map.

First add the MapKit Framework to the project.

Create a subclass of UIViewController name it whatever, but for the sake of understanding name it something like map.h/m

As always you can create any subviews programmatically or through IB, am using the simple way IB. Don't forget to connect everything in IB. Also change the SecondView in the tab bar to the map.xib or simply rename to map.xib and alter. Last make sure you change the class of UIViewController in TabBarController.

Map.xib:


#import "UIKit/UIKit.h"

#import "MapKit/MapKit.h"


@interface Map : UIViewController {

IBOutlet MKMapView *map;

}


@property (nonatomic,retain) IBOutlet MKMapView *map;


@end



#import "Map.h"


@implementation Map

@synthesize map;


- (void)viewDidLoad{

//enable location on the map

[mapview setShowsUserLocation:YES];

[super viewDidLoad];

}


- (void)dealloc {

[map dealloc];

[super dealloc];

}



@end


now the last step on the initial step up is to register the device with your server and apple's push notification service as well. We are going to be registering as a Badge notification. The reason we are doing this is because if you do not actually send notifications you can't keep track of number of times your app gets removed, and for that matter reinstalled.

first step is to register with APN. To do this we simply call


[[UIApplicatio sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge];



but we also need to implement the callback delegates

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken;

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err;




put all this in the AppDelegate.m

#import "AppTrackerAppDelegate.h"



@implementation AppTrackerAppDelegate


@synthesize window;

@synthesize tabBarController;



- (void)applicationDidFinishLaunching:(UIApplication *)application {

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge];

[window addSubview:tabBarController.view];

}


- (void)sendProviderDeviceToken:(const void*)devToken{

}


//Delegate methods that tell you if you succeeded.

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {

const void *devTokenBytes = [devToken bytes];

self.registered = YES;

[self sendProviderDeviceToken:devTokenBytes]; // custom method

}


- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {

NSLog(@"Error in registration. Error: %@", err);

}


@end


so now your wondering what goes in the sendProviderDeviceTokern. Well it depends on how you plan to implement your server, if you implement a low level server then something like BSD Sockets or CFNetwork is what you want to use. If you are using a php script then something NSUrl is what you want to use. In this case I want to learn CFNetwork so that what am using. CFNetwork has a few different connection type am using CFSocket API's.


#include "CFNetwork/CFNetwork.h"

#include "sys/socket.h"

#include "netinet/in.h"

#include "arpa/inet.h"



#define PORT 2048

#define IPADDR "127.0.0.1"


//this is the callback method that gets called when

//client opens up the connection to the server

//if you were using more than one callback type you would need

//to check before continuing but since we only

//used the connect callback there was no need.


void ConnectCallBack(CFSocketRef socket, CFSocketCallBackType type,

CFDataRef address, const void *data, void *info){

//gets the native socket its a BSD socket so

//all normal BSD socket methods work

//like send you need to close the socket

CFSocketNativeHandle sock = CFSocketGetNative(socket);

send(sock, info, sizeof(info)+1, 0);

close(sock);

}



//this will open up a socket to our server

//we use the connectcallback to know when the socket actually connects

//once connected you need to have ConnectCallBack write the

//devToken to the server.

- (void)sendProviderDeviceToken:(const void*)devToken{

//socket reference and context, which get past around in the callback method

CFSocketRef sock;

CFSocketContext context = { 0, &devToken, NULL, NULL, NULL };

//open socket with default allocator its a TCP socket in ipv_4

sock = CFSocketCreate(NULL, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketConnectCallBack, (CFSocketCallBack)ConnectCallBack, &context);

//if can't create socket fail to send devtoken

if(sock == NULL){

NSLog(@"error");

}

else {

//put the address of your server and port

//port define above as well as ip address

struct sockaddr_in addr;

memset(&addr, 0, sizeof(addr));

addr.sin_len = sizeof(addr);

addr.sin_family = AF_INET;

addr.sin_port = htons(PORT);

addr.sin_addr.s_addr = inet_addr(IPADDR);

//create reference and connect to server

CFDataRef connectAddr = CFDataCreate(NULL, (unsigned char *)&addr, sizeof(addr));

CFSocketConnectToAddress(sock, connectAddr, -1);

//run the socket through run loop,

//if connects callback method is called

CFRunLoopSourceRef sourceRef = CFSocketCreateRunLoopSource(kCFAllocatorDefault, sock, 0);

CFRunLoopAddSource(CFRunLoopGetCurrent(), sourceRef, kCFRunLoopCommonModes);

CFRelease(sourceRef);

CFRunLoopRun();

}

}


ways to improve codebase make sure to include reachability checking. I hope this was helpful or interesting in some way. That handles the basics of getting the app running with the desired views. Look forward to server for push notification implementation and getting the actual data you want display. Ending with getting your location when you open the app and sending it to the server. Also more about JSON for both push notifications, and map points.

1 comment:

  1. Wouldn't you need a way to stop your run loop or does that happen on it's own when you close the sock?

    ReplyDelete