2014-05-03 10 views
0

我目前正在開發一款iPhone應用程序,要求我使用Facebook帳戶。我還需要使用核心數據在iPhone上存儲用戶相關數據。問題是我知道核心數據是iPhone特有的。這意味着如果我使用某個iPhone,那麼特定的iPhone將擁有我打算給予每個用戶的某些屬性。但是,我希望能夠做到這一點,以便如果用戶決定登錄另一部手機,他或她可以使用Facebook登錄並向該用戶而不是iPhone的所有者查看相關數據。這可能嗎?或者我應該單獨使用MYSQL,以便從另一臺在線服務器獲取Facebook用戶相關信息。Objective-C使用Facebook帳戶和核心數據

回答

0

這是你最想使用iCloud的東西。 Start here with the iCloud Key Value Store

這可以讓你做的是將數據保存到用戶存儲而不是直接設備。這會給你兩件事:

  1. 如果用戶轉到另一個設備,你不需要再次登錄它們。
  2. 如果用戶在一臺設備上註銷或另一個用戶通過不同的Apple ID登錄,則可以檢測到該事件並自動將用戶註銷。

您也可以直接使用Sqlite iCloud存儲來代替鍵值存儲,但我不會推薦它,除非您只是iOS7 +應用程序,因爲它沒有穩定性的最佳聲譽。該代碼來實現鍵值存儲對於用戶可能是這個樣子:

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"); 
} 

如果iCloud中不可用,你可以只使用本地鑰匙扣使用這樣的事情(此代碼使用做同樣的事情該SSKeychain wrapper爲的CoreFoundation鑰匙扣服務):

- (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]; 
    } 
} 

希望這有助於。