Tuesday, April 23, 2013

NSFileManager ubiquityIdentityToken example ios


ubiquityIdentityToken

Returns an opaque token that represents the current iCloud (“ubiquity”) identity.
- (id < NSObject, NSCopying, NSCoding >)ubiquityIdentityToken
Return Value
An opaque object that identifies the current iCloud user. If iCloud is unavailable for any reason or there is no logged-in user, this method returns nil.
Discussion of [NSFileManager ubiquityIdentityToken]
Use this method to determine if iCloud is currently available. Because this method returns relatively quickly, you can call it at launch time and you can call it from your app’s main thread.
You can use the token returned by this method, together with the NSUbiquityIdentityDidChangeNotification notification, to detect when the user logs in or out of iCloud and to detect changes to the active iCloud account. When the user logs in with a different iCloud account, the returned identity token changes and the system posts the notification. If you stored or archived the previous token, you compare that token to the newly obtained one using the isEqual: method to determine if the users are the same or different.[NSFileManager ubiquityIdentityToken]
Calling this method does not establish access to your app’s ubiquity containers. To establish access to a ubiquity container, call theURLForUbiquityContainerIdentifier: method. In OS X, you can instead use an NSDocument object, which establishes access automatically.
Example of [NSFileManager ubiquityIdentityToken]
- (BOOL) hasValidCloudToken {
    NSFileManager *fm = [NSFileManager defaultManager];
    if( ![fm respondsToSelector:@selector(ubiquityIdentityToken)] ) return NO;
    id cloudToken = [fm ubiquityIdentityToken];
    if( cloudToken ) {
        NSData *tokenAsData = [NSKeyedArchiver archivedDataWithRootObject:cloudToken];
        [[NSUserDefaults standardUserDefaults] setObject:tokenAsData forKey: @"com.apple.MyAppName.UbiquityIdentityToken"];
        return YES;
    }
    else {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey: @"com.apple.MyAppName.UbiquityIdentityToken"];
        return NO;
    }
}