Search code examples
iosbluetoothgamekit

Maintain Gamekit Bluetooth connection through whole app


So I'm building a data collection app for my work, and we'd like to have two ipads running the app simultaneously while both maintaining the same info. So I ran some tests with gamekit and I think it will work great, but I could only get it working on one view controller. My problem is users will surf between three different view controllers while using the app and I don't know how to maintain that session and send data and listen for data regardless of which page you're on. Is this something that the appdelegate could maintain? Or perhaps a singleton class? Could someone point me in the right direction? Thank you!


Solution

  • I would use a Singleton class to send / receive commands from game kit. Also that singleton might send NSNotifications, so you can assign any of your view controllers to receive them (make them observers). I have implemented this in several games and it works fine.

    Example:

    #define kWGConnectionManagerDidOpenConnection   @"kWGConnectionManagerDidOpenConnection"
    #define kWGConnectionManagerDidCloseConnection @"kWGConnectionManagerDidCloseConnection"
    
    typedef enum {
        WGConnectionTypeUndefined,
        WGConnectionTypeServer,
        WGConnectionTypeClient
    } WGConnectionType;
    
    @interface WGAPIManager : NSObject {
    }
    @property (nonatomic, readonly) WGConnectionType connectionType;
    
    + (WGAPIManager*) sharedInstance;
    
    - (void) sendCommand:(NSString*) command;
    

    So any class can use this singleton to send commands using game kit, and any class can be an observer of notifications