"Not Authenticated" (9/1002) error while accessing cloud kit public database - ios

I have integrated cloudkit framework in one of my iOS app and is working fine in iOS 8 and 9 but not in iOS 10. It fails to fetch the records from public database without active iCloud account. Able to fetch the records in iOS 8 and 9 if user restricted iCloud drive for the app also but it fails in iOS 10, getting error like
CKError 0x170249090: "Not Authenticated" (9/1002); "No backing
account, so not returning an auth token."
It is not working for both development and production cloudkit environments in iOS devices.
I have tested the app with AdHoc build pointing to production.
Below is my code
CKQuery *query = [[CKQuery alloc] initWithRecordType:#"RecordSettings" predicate:[NSPredicate predicateWithFormat:#"TRUEPREDICATE"]];
CKQueryOperation *queryOperation = [[CKQueryOperation alloc] initWithQuery:query];
queryOperation.queuePriority = NSOperationQueuePriorityVeryHigh;
queryOperation.recordFetchedBlock = ^(CKRecord *record) {
if(record != nil) {
}
};
queryOperation.queryCompletionBlock = ^(CKQueryCursor * __nullable cursor, NSError * __nullable operationError) {
NSLog(#"iCloud error: %#", operationError.description);
};
[[[CKContainer defaultContainer] publicCloudDatabase] addOperation:queryOperation];

You need to log in using your iCloud account on simulator or device. It worked for me.
The answer is in the section Enter iCloud Credentials Before Running Your App:
In development, when you run your app through Xcode on a simulator or a device, you need to enter iCloud credentials to read records in the public database. In production, the default permissions allow non-authenticated users to read records in the public database but do not allow them to write records.

I am having the same issue. Everything worked great until iOS10. The only thing so far that I have found to fix this is for the user to have iCloud Drive enabled on their device to fetch any record in a public container. I'm in the process of filing a bug report with Apple!

This issue exists from iOS 10.0 to 10.1.1 but it has been resolved in iOS 10.2.

Related

iOS - App Files not deleted when app is deleted

We ran into strange behavior.
Install the app from test flight
App creates a local sqlite database in the NSDocumentDirectory
User deletes the app
User reinstalls the app again from test flight (newer version)
App sees old database in the app
This has happened several times. And my understanding was that apps are supposed to delete everything with it once they are deleted. Yet intermittently this happens.
Anyone seen this issue before?
I assume this is caused by iCloud backup and restore functionality. If you don't set the exclude back-up key explicitly, the iOS will automatically back-up your files in the document directory. In your case, the previous database was backed-up in the iCloud and when you re-installed the app, the iCloud restored the database file. That's the reason why you are seeing the old data. If you don't need to back up your app's database file use the following code:
Swift 4:
do
{
var resourceValues = URLResourceValues()
resourceValues.isExcludedFromBackup = true
try databaseURL.setResourceValues(resourceValues)
}
catch
{
print(error.localizedDescription)
}
Old Version Code
do
{
// Database URL
try databaseURL.setResourceValue(true, forKey:NSURLIsExcludedFromBackupKey)
}
catch let error as NSError
{
print("Error excluding \(URL.lastPathComponent) from backup \(error)");
}
Reference: How do I prevent files from being backed up to iCloud and iTunes?

Can't access CloudKit for strange error: Internal Error (1/2005)

My project worked fine with XCode 7 on iOS 9. But after building with XCode 8 on iOS 10, CloudKit gave this error:
[LogFacilityCK] Couldn't get connection for container <private>, failing operation. NSError: Internal Error (1/2005).
When I call this function below:
[[CKContainer defaultContainer] accountStatusWithCompletionHandler:^(CKAccountStatus accountStatus, NSError *error) {}];
Has someone met this problem before? Thank you for your help!
ApplicationPermissionStatus is CKApplicationPermissionStatusCouldNotComplete.
Later, I tested on iOS10.0.1 and iOS10.0.0, everything worked fine except iOS10.0.2. It's so weird. Is it a bug?
Reboot your iOS device. After upgrading one of my test iPhones to iOS 10.1 CKContainer returned a NSError with CKApplicationPermissionStatusCouldNotComplete in the completionBlock of the function requestApplicationPermission:completionHandler: and statusForApplicationPermission:completionHandler: too. The iCloud user was authenticated on that device but the Settings app do not provide any information about the iCloud user (storage e.g.). After rebooting the device all information are provided and the function calls return no errors.

HealthKit requestAuthorization dialog not appearing

I have been consistently unable to get the HealthKit requestAuthorization dialog to appear. This has been the case for two different phones with different OS versions and different installation methods (see below). The requestAuthorization code is standard boilerplate code, such as:
HKCharacteristicType *genderType = [HKObjectType characteristicTypeForIdentifier:HKCharacteristicTypeIdentifierBiologicalSex];
[self.healthStore requestAuthorizationToShareTypes:nil readTypes:[NSSet setWithObjects: genderType, nil] completion:^(BOOL success, NSError *error) {
NSLog(#"requestAuthorization completion block");
}];
I've tried a fresh app with just that code, tried different parameters for the requestAuthorization method call, and have tried resetting our systems in a number of different ways: app uninstall, resetting settings, erasing phone content and setting with existing partial backup; erasing phone content and settings with a full backup, erasing content and settings and starting phone as a new phone. Have tried multiple different readTypes and shareTypes. Each time requestAuthorization is called on a fresh app install, the following error code appears:
"Error occurred = Error Domain=com.apple.healthkit Code=4 "Missing
com.apple.developer.healthkit entitlement." UserInfo=0x7fa748534b00
{NSLocalizedDescription=Missing com.apple.developer.healthkit
entitlement.}"
However, the HealthKit entitlements are set in both Capabilities on xcode as well as Capabilities in the App ID on Apple Developer: Certificates, Identifiers, and Profiles.
- Questions: How do I fix this issue so the requestAuthorization dialog appears? What is a possible cause of phones getting into a state where they get this error consistently?
- Phones used: iPhone 6, iPhone 6 Plus
- OS Versions used: iOS 9.0 / 9.0.2 / 9.1 / 9.2
- Methods of loading app onto phone: iOS App Store, Watch App App Store, TestFlight, xcode
- Situations where this appears to not be broken: With our users' (non-development) phones installing via the App Store.
A few things could be causing this. First, your provisioning profiles could be out of date with what you are actually seeing on the web portal. Try refreshing these manually from XCode.
Does your app have an app extension or watchOS app that uses the HealthKit framework? Each of extension/watch app will have their own profiles on the web portal, and if the HealthKit framework has been added to any of them without having the entitlement added as well then this could also cause the error.

KeychainWrapperItem stopped working in iOS 7.1

I recently installed iOS 7.1 simulator and the new Xcode 5.1. My App worked in iOS 7 fine. I'm using the KeychainItemWrapper class from Apple.
After the update it crashed with the following message:
*** Assertion failure in -[KeychainItemWrapper writeToKeychain]
Specifically at line 299:
NSAssert( result == noErr, #"Couldn't update the Keychain Item." );
Error is hear -25300 (errSecItemNotFound)
I have specified the Keychain Access Group in my entitlements file. This error occurred only in the iOS 7.1 Simulator and not on a real iPhone or the 7.0 Simulator.
Does anyone know what changed with Keychain in 7.1 ?
Well KeychainItemWrapper is a old implementation full of bugs. I would recommend using another wrapper, or write your own.
That being said, I used to have many error but not this one. Basically what happens is while saving it checks that your item is already in the keychain, in order to add it or just update it. Here that check returns true even though the item is quite different, so it cannot update because SecItemUpdate believes it does not exist.
What you should do is reset your keychain, you have two options for this :
On the simulator Menu Simulator->Reset content and settings
Run this snippet somewhere in your code :
Based on Vegard answer here Reset An iPhone App's Keychain
-(void)resetKeychain {
[self deleteAllKeysForSecClass:kSecClassGenericPassword];
[self deleteAllKeysForSecClass:kSecClassInternetPassword];
[self deleteAllKeysForSecClass:kSecClassCertificate];
[self deleteAllKeysForSecClass:kSecClassKey];
[self deleteAllKeysForSecClass:kSecClassIdentity];
}
-(void)deleteAllKeysForSecClass:(CFTypeRef)secClass {
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
[dict setObject:(__bridge id)secClass forKey:(__bridge id)kSecClass];
OSStatus result = SecItemDelete((__bridge CFDictionaryRef) dict);
NSAssert(result == noErr || result == errSecItemNotFound, #"Error deleting keychain data (%ld)", result);
}
Keychain continues to work under iOS 7.1. Your issue is related to Simulator itself. Simulator does not allow to store certain keys as far as I know, and this is consistent between iOS 7.0 and iOS 7.1.
If you run your app on a real device, the crash will disappear.

App built with xcode 5 can't read password stored for app build with xcode 4.6.3

My password store doesn't seem to be compatible across different XCode builds.
I'm managing a password with SFHFKeychainUtils. If I build my app with XCode 4.6.3, then do a subsequent build with XCode 5.0, I am getting a (null) back from this call:
NSString *p = [SFHFKeychainUtils getPasswordForUsername:username andServiceName:DEF_SERVICENAME error:&error];
and I stored it using this call:
[SFHFKeychainUtils storeUsername:username andPassword:password forServiceName:DEF_SERVICENAME updateExisting:YES error:&error];
Has anyone experienced similar issues and have you found a solution for dealing with this? It will be bad if my updated app can't read the stored data from my current app.
This line is getting the exact same dictionary in both builds:
OSStatus status = SecItemCopyMatching((CFDictionaryRef) attributeQuery, (CFTypeRef *) &attributeResult);
I get the following error:
errSecItemNotFound = -25300, /* The specified item could not be found in the keychain. */

Resources