0

I am currently developing an iPhone application that requires that I use Facebook accounts. I also need to use core data to store user-relevant data on the iPhone. The problem is that I know that core data is iPhone specific. Meaning if I use a certain iPhone, then that specific iPhone will hold the certain attributes I intend on giving to each user. However, I want to be able to make it so that if the user decides to log into another phone, he or she is able to sign in with Facebook and see the relevant data to that user and not the owner of the iPhone. Is this possible? or should I use MYSQL separately so that it takes Facebook user-relevant information from another online server.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
user2977578
  • 413
  • 5
  • 19

1 Answers1

0

This is something you ideally want to use iCloud for. Start here with the iCloud Key Value Store.

What this will let you do is save data to a user store rather than the device directly. This will give you two things:

  1. If the user goes to another device you don't need to log them in again.
  2. If the user logs out on one device or another user logs in via a different apple ID then you can detect that event and automatically log the user out.

You can also use the Sqlite iCloud store directly instead of the key-value store but I wouldn't recommend it unless you are an iOS7+ app only as it doesn't have the best reputation for stability. The code to implement the key value store for a user might look something like this:

NSData *iCloudToken = (NSData *)[[NSFileManager defaultManager] ubiquityIdentityToken];
__weak typeof(self) weakSelf = self;

if (iCloudToken) {
    NSLog(@"iCloud is available, setting up ubiquity container");
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        __strong typeof(self) strongSelf = weakSelf;
        strongSelf.icloudContainerURLString = [NSString stringWithFormat:@"%@", [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil]];
        NSLog(@"Ubiquity container setup complete");
    });
} else {
    NSLog(@"iCloud is unavailable");
}

If iCloud isn't available you can just use the local keychain to do the same thing using something like this (this code uses the SSKeychain wrapper for the CoreFoundation keychain service):

- (NSString *)userID
{
    if (self.iCloudAvailable) {
        return [[NSUbiquitousKeyValueStore defaultStore] stringForKey:kUserIDKey];
    } else {
        NSArray *accounts = [SSKeychain accountsForService:kDeliveriesServiceName];
        return [[accounts firstObject] valueForKey:kSSKeychainAccountKey];
    }
}

- (NSString *)userPassword
{
    if (self.isCloudAvailable) {
        return [[NSUbiquitousKeyValueStore defaultStore] stringForKey:kUserPasswordKey];
    } else {
        return [SSKeychain passwordForService:kDeliveriesServiceName account:[self userID]];
    }
}

- (void)setUserID:(NSString *)userID andPassword:(NSString *)password;
{
    NSParameterAssert(userID);
    NSParameterAssert(password);
    if (self.isCloudAvailable) {
        [[NSUbiquitousKeyValueStore defaultStore] setString:userID forKey:kUserIDKey];
        [[NSUbiquitousKeyValueStore defaultStore] setString:password forKey:kUserPasswordKey];
        [[NSUbiquitousKeyValueStore defaultStore] synchronize];
    } else {
        [SSKeychain setPassword:password forService:kDeliveriesServiceName account:userID];
    }
}

Hope this helps.

smyrgl
  • 864
  • 6
  • 12