Permalink
Browse files

C12 first draft

  • Loading branch information...
Erica Sadun Erica Sadun
Erica Sadun authored and Erica Sadun committed Jul 27, 2009
1 parent 55d673f commit 566fb0206c63cc01ebc4fb2e48e245ff06895639
Showing with 14,641 additions and 0 deletions.
  1. BIN C12-GameKit/01-Simple GameKit/Default.png
  2. +41 −0 C12-GameKit/01-Simple GameKit/GameKitHelper.h
  3. +157 −0 C12-GameKit/01-Simple GameKit/GameKitHelper.m
  4. +245 −0 C12-GameKit/01-Simple GameKit/HelloWorld.xcodeproj/project.pbxproj
  5. +8 −0 C12-GameKit/01-Simple GameKit/HelloWorld_Prefix.pch
  6. +26 −0 C12-GameKit/01-Simple GameKit/Info.plist
  7. +293 −0 C12-GameKit/01-Simple GameKit/TestBedViewController.xib
  8. BIN C12-GameKit/01-Simple GameKit/cover320x416.png
  9. BIN C12-GameKit/01-Simple GameKit/icon.png
  10. +77 −0 C12-GameKit/01-Simple GameKit/main.m
  11. BIN C12-GameKit/02-Monitoring GameKit/Default.png
  12. +40 −0 C12-GameKit/02-Monitoring GameKit/GameKitHelper.h
  13. +156 −0 C12-GameKit/02-Monitoring GameKit/GameKitHelper.m
  14. +245 −0 C12-GameKit/02-Monitoring GameKit/HelloWorld.xcodeproj/project.pbxproj
  15. +8 −0 C12-GameKit/02-Monitoring GameKit/HelloWorld_Prefix.pch
  16. +26 −0 C12-GameKit/02-Monitoring GameKit/Info.plist
  17. +231 −0 C12-GameKit/02-Monitoring GameKit/TestBedViewController.xib
  18. BIN C12-GameKit/02-Monitoring GameKit/cover320x416.png
  19. BIN C12-GameKit/02-Monitoring GameKit/icon.png
  20. +61 −0 C12-GameKit/02-Monitoring GameKit/main.m
  21. BIN C12-GameKit/03-Sending Complex Objects/Default.png
  22. +20 −0 C12-GameKit/03-Sending Complex Objects/DrawView.h
  23. +211 −0 C12-GameKit/03-Sending Complex Objects/DrawView.m
  24. +40 −0 C12-GameKit/03-Sending Complex Objects/GameKitHelper.h
  25. +156 −0 C12-GameKit/03-Sending Complex Objects/GameKitHelper.m
  26. +251 −0 C12-GameKit/03-Sending Complex Objects/HelloWorld.xcodeproj/project.pbxproj
  27. +8 −0 C12-GameKit/03-Sending Complex Objects/HelloWorld_Prefix.pch
  28. +26 −0 C12-GameKit/03-Sending Complex Objects/Info.plist
  29. +164 −0 C12-GameKit/03-Sending Complex Objects/TestBedViewController.xib
  30. BIN C12-GameKit/03-Sending Complex Objects/cover320x416.png
  31. BIN C12-GameKit/03-Sending Complex Objects/icon.png
  32. +125 −0 C12-GameKit/03-Sending Complex Objects/main.m
  33. +27 −0 C12-GameKit/04-GameKit Voice/AudioChatHelper.h
  34. +170 −0 C12-GameKit/04-GameKit Voice/AudioChatHelper.m
  35. BIN C12-GameKit/04-GameKit Voice/Default.png
  36. +249 −0 C12-GameKit/04-GameKit Voice/HelloWorld.xcodeproj/project.pbxproj
  37. +8 −0 C12-GameKit/04-GameKit Voice/HelloWorld_Prefix.pch
  38. +26 −0 C12-GameKit/04-GameKit Voice/Info.plist
  39. +157 −0 C12-GameKit/04-GameKit Voice/TestBedViewController.xib
  40. BIN C12-GameKit/04-GameKit Voice/cover320x416.png
  41. BIN C12-GameKit/04-GameKit Voice/icon.png
  42. +53 −0 C12-GameKit/04-GameKit Voice/main.m
  43. +103 −0 C12-GameKit/05-Bonjour AppleTCP/Apple Sample Code/NetReachability.h
  44. +194 −0 C12-GameKit/05-Bonjour AppleTCP/Apple Sample Code/NetReachability.m
  45. +77 −0 C12-GameKit/05-Bonjour AppleTCP/Apple Sample Code/NetUtilities.h
  46. +89 −0 C12-GameKit/05-Bonjour AppleTCP/Apple Sample Code/NetUtilities.m
  47. +85 −0 C12-GameKit/05-Bonjour AppleTCP/Apple Sample Code/Networking_Internal.h
  48. +121 −0 C12-GameKit/05-Bonjour AppleTCP/Apple Sample Code/TCPConnection.h
  49. +506 −0 C12-GameKit/05-Bonjour AppleTCP/Apple Sample Code/TCPConnection.m
  50. +115 −0 C12-GameKit/05-Bonjour AppleTCP/Apple Sample Code/TCPServer.h
  51. +249 −0 C12-GameKit/05-Bonjour AppleTCP/Apple Sample Code/TCPServer.m
  52. +99 −0 C12-GameKit/05-Bonjour AppleTCP/Apple Sample Code/TCPService.h
  53. +245 −0 C12-GameKit/05-Bonjour AppleTCP/Apple Sample Code/TCPService.m
  54. BIN C12-GameKit/05-Bonjour AppleTCP/Default.png
  55. +297 −0 C12-GameKit/05-Bonjour AppleTCP/HelloWorld.xcodeproj/project.pbxproj
  56. +8 −0 C12-GameKit/05-Bonjour AppleTCP/HelloWorld_Prefix.pch
  57. +28 −0 C12-GameKit/05-Bonjour AppleTCP/Info.plist
  58. +15 −0 C12-GameKit/05-Bonjour AppleTCP/ModalAlert.h
  59. +180 −0 C12-GameKit/05-Bonjour AppleTCP/ModalAlert.m
  60. +181 −0 C12-GameKit/05-Bonjour AppleTCP/TestBedViewController.xib
  61. BIN C12-GameKit/05-Bonjour AppleTCP/cover320x416.png
  62. BIN C12-GameKit/05-Bonjour AppleTCP/icon.png
  63. +139 −0 C12-GameKit/05-Bonjour AppleTCP/main.m
  64. +103 −0 C12-GameKit/06-PictureCatcher AppleTCP/Apple Sample Code/NetReachability.h
  65. +194 −0 C12-GameKit/06-PictureCatcher AppleTCP/Apple Sample Code/NetReachability.m
  66. +77 −0 C12-GameKit/06-PictureCatcher AppleTCP/Apple Sample Code/NetUtilities.h
  67. +89 −0 C12-GameKit/06-PictureCatcher AppleTCP/Apple Sample Code/NetUtilities.m
  68. +85 −0 C12-GameKit/06-PictureCatcher AppleTCP/Apple Sample Code/Networking_Internal.h
  69. +121 −0 C12-GameKit/06-PictureCatcher AppleTCP/Apple Sample Code/TCPConnection.h
  70. +506 −0 C12-GameKit/06-PictureCatcher AppleTCP/Apple Sample Code/TCPConnection.m
  71. +115 −0 C12-GameKit/06-PictureCatcher AppleTCP/Apple Sample Code/TCPServer.h
  72. +249 −0 C12-GameKit/06-PictureCatcher AppleTCP/Apple Sample Code/TCPServer.m
  73. +99 −0 C12-GameKit/06-PictureCatcher AppleTCP/Apple Sample Code/TCPService.h
  74. +245 −0 C12-GameKit/06-PictureCatcher AppleTCP/Apple Sample Code/TCPService.m
  75. +32 −0 C12-GameKit/06-PictureCatcher AppleTCP/Catcher.h
  76. +179 −0 C12-GameKit/06-PictureCatcher AppleTCP/Catcher.m
  77. BIN C12-GameKit/06-PictureCatcher AppleTCP/Catcher.psd
  78. BIN C12-GameKit/06-PictureCatcher AppleTCP/English.lproj/InfoPlist.strings
  79. +870 −0 C12-GameKit/06-PictureCatcher AppleTCP/English.lproj/MainMenu.xib
  80. +28 −0 C12-GameKit/06-PictureCatcher AppleTCP/Info.plist
  81. BIN C12-GameKit/06-PictureCatcher AppleTCP/PictureCatcher.xcodeproj/TemplateIcon.icns
  82. +320 −0 C12-GameKit/06-PictureCatcher AppleTCP/PictureCatcher.xcodeproj/project.pbxproj
  83. +7 −0 C12-GameKit/06-PictureCatcher AppleTCP/PictureCatcher_Prefix.pch
  84. BIN C12-GameKit/06-PictureCatcher AppleTCP/catcher.icns
  85. +14 −0 C12-GameKit/06-PictureCatcher AppleTCP/main.m
  86. +16 −0 C12-GameKit/06-PictureCatcher AppleTCP/version.plist
  87. BIN C12-GameKit/07-Pasteboard Share/Default.png
  88. +41 −0 C12-GameKit/07-Pasteboard Share/GameKitHelper.h
  89. +157 −0 C12-GameKit/07-Pasteboard Share/GameKitHelper.m
  90. +251 −0 C12-GameKit/07-Pasteboard Share/HelloWorld.xcodeproj/project.pbxproj
  91. +8 −0 C12-GameKit/07-Pasteboard Share/HelloWorld_Prefix.pch
  92. +26 −0 C12-GameKit/07-Pasteboard Share/Info.plist
  93. +15 −0 C12-GameKit/07-Pasteboard Share/ModalAlert.h
  94. +180 −0 C12-GameKit/07-Pasteboard Share/ModalAlert.m
  95. +180 −0 C12-GameKit/07-Pasteboard Share/TestBedViewController.xib
  96. BIN C12-GameKit/07-Pasteboard Share/cover320x416.png
  97. BIN C12-GameKit/07-Pasteboard Share/icon.png
  98. +135 −0 C12-GameKit/07-Pasteboard Share/main.m
  99. +103 −0 C12-GameKit/08-Bonjour Helper/Apple Sample Code/NetReachability.h
  100. +194 −0 C12-GameKit/08-Bonjour Helper/Apple Sample Code/NetReachability.m
  101. +77 −0 C12-GameKit/08-Bonjour Helper/Apple Sample Code/NetUtilities.h
  102. +89 −0 C12-GameKit/08-Bonjour Helper/Apple Sample Code/NetUtilities.m
  103. +85 −0 C12-GameKit/08-Bonjour Helper/Apple Sample Code/Networking_Internal.h
  104. +121 −0 C12-GameKit/08-Bonjour Helper/Apple Sample Code/TCPConnection.h
  105. +506 −0 C12-GameKit/08-Bonjour Helper/Apple Sample Code/TCPConnection.m
  106. +115 −0 C12-GameKit/08-Bonjour Helper/Apple Sample Code/TCPServer.h
  107. +249 −0 C12-GameKit/08-Bonjour Helper/Apple Sample Code/TCPServer.m
  108. +99 −0 C12-GameKit/08-Bonjour Helper/Apple Sample Code/TCPService.h
  109. +245 −0 C12-GameKit/08-Bonjour Helper/Apple Sample Code/TCPService.m
  110. +358 −0 C12-GameKit/08-Bonjour Helper/BonjourHelper (original).m
  111. +339 −0 C12-GameKit/08-Bonjour Helper/BonjourHelper-earlier.m
  112. +366 −0 C12-GameKit/08-Bonjour Helper/BonjourHelper-early.m
  113. +53 −0 C12-GameKit/08-Bonjour Helper/BonjourHelper.h
  114. +358 −0 C12-GameKit/08-Bonjour Helper/BonjourHelper.m
  115. BIN C12-GameKit/08-Bonjour Helper/Default.png
  116. +303 −0 C12-GameKit/08-Bonjour Helper/HelloWorld.xcodeproj/project.pbxproj
  117. +8 −0 C12-GameKit/08-Bonjour Helper/HelloWorld_Prefix.pch
  118. +28 −0 C12-GameKit/08-Bonjour Helper/Info.plist
  119. +15 −0 C12-GameKit/08-Bonjour Helper/ModalAlert.h
  120. +180 −0 C12-GameKit/08-Bonjour Helper/ModalAlert.m
  121. +293 −0 C12-GameKit/08-Bonjour Helper/TestBedViewController.xib
  122. BIN C12-GameKit/08-Bonjour Helper/cover320x416.png
  123. BIN C12-GameKit/08-Bonjour Helper/icon.png
  124. +79 −0 C12-GameKit/08-Bonjour Helper/main.m
Binary file not shown.
@@ -0,0 +1,41 @@
+/*
+ Erica Sadun, http://ericasadun.com
+ iPhone Developer's Cookbook, 3.0 Edition
+ BSD License, Use at your own risk
+ */
+
+#import <UIKit/UIKit.h>
+#import <GameKit/GameKit.h>
+
+@protocol GameKitHelperDataDelegate <NSObject>
+@optional
+-(void) connectionEstablished;
+-(void) connectionLost;
+-(void) sentData: (NSString *) errorMessage;
+-(void) receivedData: (NSData *) data;
+@end
+
+
+@interface GameKitHelper : NSObject <GKPeerPickerControllerDelegate, GKSessionDelegate>
+{
+ NSString *sessionID;
+ id <GameKitHelperDataDelegate> dataDelegate;
+ UIViewController *viewController;
+
+ GKSession *session;
+ BOOL isConnected;
+}
+
+@property (retain) id dataDelegate;
+@property (retain) UIViewController *viewController;
+@property (retain) NSString *sessionID;
+@property (retain) GKSession *session;
+@property (assign) BOOL isConnected;
+
++ (void) connect;
++ (void) disconnect;
++ (void) sendData: (NSData *) data;
++ (void) assignViewController: (UIViewController *) aViewController;
+
++ (GameKitHelper *) sharedInstance;
+@end
@@ -0,0 +1,157 @@
+
+/*
+ Erica Sadun, http://ericasadun.com
+ iPhone Developer's Cookbook, 3.0 Edition
+ BSD License, Use at your own risk
+ */
+
+#import "GameKitHelper.h"
+
+@implementation GameKitHelper
+@synthesize dataDelegate;
+@synthesize viewController;
+@synthesize sessionID;
+@synthesize session;
+@synthesize isConnected;
+
+#define DO_DATA_CALLBACK(X, Y) if (self.dataDelegate && [self.dataDelegate respondsToSelector:@selector(X)]) [self.dataDelegate performSelector:@selector(X) withObject:Y];
+#define showAlert(format, ...) myShowAlert(__LINE__, (char *)__FUNCTION__, format, ##__VA_ARGS__)
+#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
+
+// Simple Alert Utility
+void myShowAlert(int line, char *functname, id formatstring,...)
+{
+ va_list arglist;
+ if (!formatstring) return;
+ va_start(arglist, formatstring);
+ id outstring = [[[NSString alloc] initWithFormat:formatstring arguments:arglist] autorelease];
+ va_end(arglist);
+
+ UIAlertView *av = [[[UIAlertView alloc] initWithTitle:outstring message:nil delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil] autorelease];
+ [av show];
+}
+
+#pragma mark Shared Instance
+static GameKitHelper *sharedInstance = nil;
+
++ (GameKitHelper *) sharedInstance
+{
+ if(!sharedInstance) sharedInstance = [[self alloc] init];
+ return sharedInstance;
+}
+
+#pragma mark Data Sharing
+- (void) sendDataToPeers: (NSData *) data
+{
+ NSError *error;
+ BOOL didSend = [self.session sendDataToAllPeers:data withDataMode:GKSendDataReliable error:&error];
+ if (!didSend)
+ NSLog(@"Error sending data to peers: %@", [error localizedDescription]);
+ DO_DATA_CALLBACK(sentData:, (didSend ? nil : [error localizedDescription]));
+}
+
+- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
+{
+ DO_DATA_CALLBACK(receivedData:, data);
+}
+
+#pragma mark Connections
+- (void) startConnection
+{
+ if (!self.isConnected)
+ {
+ GKPeerPickerController *picker = [[GKPeerPickerController alloc] init];
+ picker.delegate = self;
+ picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
+ [picker show];
+ if (self.viewController)
+ self.viewController.navigationItem.rightBarButtonItem = nil;
+ }
+}
+
+// Dismiss the peer picker on cancel
+- (void) peerPickerControllerDidCancel: (GKPeerPickerController *)picker
+{
+ [picker release];
+ if (self.viewController)
+ self.viewController.navigationItem.rightBarButtonItem = BARBUTTON(@"Connect", @selector(startConnection));
+}
+
+- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession: (GKSession *) session{
+ [picker dismiss];
+ [picker release];
+ [self.session setDataReceiveHandler:self withContext:nil];
+ isConnected = YES;
+ DO_DATA_CALLBACK(connectionEstablished, nil);
+}
+
+- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type
+{
+ // The session ID is basically the name of the service, and is used to create the bonjour connection.
+ if (!self.session) {
+ self.session = [[GKSession alloc] initWithSessionID:(self.sessionID ? self.sessionID : @"Sample Session") displayName:nil sessionMode:GKSessionModePeer];
+ self.session.delegate = self;
+ }
+ return self.session;
+}
+
+#pragma mark Session Handling
+- (void) disconnect
+{
+ [self.session disconnectFromAllPeers];
+ self.session = nil;
+}
+
+- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
+{
+ /* STATES: GKPeerStateAvailable, = 0, GKPeerStateUnavailable, = 1, GKPeerStateConnected, = 2,
+ GKPeerStateDisconnected, = 3, GKPeerStateConnecting = 4 */
+
+ NSArray *states = [NSArray arrayWithObjects:@"Available", @"Unavailable", @"Connected", @"Disconnected", @"Connecting", nil];
+ NSLog(@"Peer state is now %@", [states objectAtIndex:state]);
+
+ if (state == GKPeerStateConnected)
+ {
+ if (self.viewController)
+ self.viewController.navigationItem.rightBarButtonItem = BARBUTTON(@"Disconnect", @selector(disconnect));
+ }
+
+ if (state == GKPeerStateDisconnected)
+ {
+ self.isConnected = NO;
+ showAlert(@"Lost connection with peer. You are no longer connected to another device.");
+ [self disconnect];
+ if (self.viewController)
+ self.viewController.navigationItem.rightBarButtonItem = BARBUTTON(@"Connect", @selector(startConnection));
+ DO_DATA_CALLBACK(connectionLost, nil);
+ }
+}
+
+- (void) assignViewController: (UIViewController *) aViewController
+{
+ self.viewController = aViewController;
+ self.viewController.navigationItem.rightBarButtonItem = BARBUTTON(@"Connect", @selector(startConnection));
+}
+
+#pragma mark Class utility methods
++ (void) connect
+{
+ [[self sharedInstance] startConnection];
+}
+
++ (void) disconnect
+{
+ [[self sharedInstance] disconnect];
+}
+
++ (void) sendData: (NSData *) data
+{
+ [[self sharedInstance] sendDataToPeers:data];
+}
+
++ (void) assignViewController: (UIViewController *) aViewController
+{
+ [[self sharedInstance] assignViewController:aViewController];
+}
+
+@end
Oops, something went wrong.

0 comments on commit 566fb02

Please sign in to comment.