Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
/ ObjectiveDDP Public archive

Making it easy for your Objective-C apps to communicate with your Meteor.js apps since 2013.

License

Notifications You must be signed in to change notification settings

boundsj/ObjectiveDDP

Repository files navigation

ObjectiveDDP

Join the chat at https://gitter.im/boundsj/ObjectiveDDP

Build Status

Connect your iOS/OSX applications written in Objective-C or Swift to server applications that communicate with the DDP protocol created by Meteor and, if required by your server, authenticate with it.

Note:
supports meteor 0.8.2 and above.
If you need to migrate please see the srp-upgrade-support branch
Now with support for OAuth with Facebook and other services [experiemental]

This is unsupported so don't come crying if it eats your cat! or destroys any data.

(void)logonWithOAuthAccessToken: (NSString *)accessToken serviceName: (NSString *) serviceName responseCallback: (MeteorClientMethodCallback)responseCallback;

Please require the following project to your meteor server for this to work.

https://github.com/jasper-lu/facebook-ddp

Hopefully meteor will support this natively in the future meteor/meteor#3515

What's Inside

ObjectiveDDP should run well with iOS projects using ARC and iOS 7.1 or above. Check out the example application and the project wiki for more information. Here is a sneak peak:

Integrate it with your project using CocoaPods:
pod 'ObjectiveDDP', '~> 0.2.0'

For more information about this, check out Linking and Building in the wiki.

Load the library and connect to a meteor server:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.meteorClient = [[MeteorClient alloc] init];
    [self.meteorClient addSubscription:@"awesome_server_mongo_collection"];
    ObjectiveDDP *ddp = [[ObjectiveDDP alloc] initWithURLString:@"wss://awesomeapp.meteor.com/websocket" delegate:self.meteorClient];
    self.meteorClient.ddp = ddp;
    [self.meteorClient.ddp connectWebSocket];
}
Signup with username:
[self.meteor signupWithUsername:self.username.text password:self.password.text fullname:self.fullname responseCallback:^(NSDictionary *response, NSError *error) {
    if (error) {
        [self handleFailedAuth:error];
        return;
    }
    [self handleSuccessfulAuth];
}];

or with email

[self.meteor signupWithEmail:self.email.text password:self.password.text fullname:self.fullname.text responseCallback:^(NSDictionary *response, NSError *error) {
    if (error) {
        [self handleFailedAuth:error];
        return;
    }
    [self handleSuccessfulAuth];
}];

or with both

[self.meteor signupWithUsernameAndEmail:self.username.text email:self.email.text password:self.password.text fullname:self.fullname.text responseCallback:^(NSDictionary *response, NSError *error) {
    if (error) {
        [self handleFailedAuth:error];
        return;
    }
    [self handleSuccessfulAuth];
}];
Logon using authentication:
[self.meteor logonWithUsername:self.username.text password:self.password.text responseCallback:^(NSDictionary *response, NSError *error) {
    if (error) {
        [self handleFailedAuth:error];
        return;
    }
    [self handleSuccessfulAuth];
}];

or with email

[self.meteor logonWithEmail:self.email.text password:self.password.text responseCallback:^(NSDictionary *response, NSError *error) {
    if (error) {
        [self handleFailedAuth:error];
        return;
    }
    [self handleSuccessfulAuth];
}];

or if you accept both

[self.meteor logonWithUsernameOrEmail:self.usernameOrEmail.text password:self.password.text responseCallback:^(NSDictionary *response, NSError *error) {
    if (error) {
        [self handleFailedAuth:error];
        return;
    }
    [self handleSuccessfulAuth];
}];
Call a remote function on the server:
[self.meteor callMethodName:@"sayHelloTo" parameters:@[self.username.text] responseCallback:^(NSDictionary *response, NSError *error) {
    NSString *message = response[@"result"];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Meteor Todos"
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:@"Great"
                                          otherButtonTitles:nil];
    [alert show];
}];
Listen for updates that meteor sends regarding the collection previously subscribed to:
- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveAddedUpdate:)
                                                 name:@"awesome_server_mongo_collection_added"
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveRemovedUpdate:)
                                                 name:@"awesome_server_mongo_collection_removed"
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveChangeUpdate:)
                                                 name:@"awesome_server_mongo_collection_changed"
                                               object:nil];
}
Send CRUD updates to the meteor server to change the collection:
NSString *message = @"I am the walrus";
NSString *anId = [[NSUUID UUID] UUIDString];
NSArray *parameters = @[@{@"_id": anId,
                          @"msg": message,
                          @"owner": self.userId,
                          @"info": self.importantInformation}];

// add a document
[self.meteor callMethodName:@"/awesome_server_mongo_collection/insert"
                 parameters:parameters
           responseCallback:nil];

// then remove it
[self.meteor callMethodName:@"/awesome_server_mongo_collection/remove"
                 parameters:@[@{@"_id": anId}]
           responseCallback:nil];
Listen for notifications:

MeteorClientConnectionReadyNotification - When the server responds as accepting the DDP protocol version to communicate on, you won't be able to call any methods to meteor until this happens

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reportConnection) name:MeteorClientDidConnectNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reportConnectionReady) name:MeteorClientConnectionReadyNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reportDisconnection) name:MeteorClientDidDisconnectNotification object:nil];

License

MIT