I am working on an iBeacon supported iOS app which requires to send out a HTTP request when the app comes to background from terminates state when it enters a region where a monitored iBeacon is situated in. All works fine excepts it fails to read some credential information saved in the Keychain. I'm not sure whether this has anything to do with Keychain or iBeacon, or both. But this is how my code looks like,
When I save my credentials(I use apps Keychain Wrapper code),
KeychainItemWrapper* keyChain = [[KeychainItemWrapper alloc] initWithIdentifier:keyChainIdentifier accessGroup:nil];
[keyChain setObject:@"MY_APP_CREDENTIALS" forKey:(__bridge id)kSecAttrService];
[keyChain setObject:myUserName forKey:(__bridge id)kSecAttrAccount];
[keyChain setObject:myPassword forKey:(__bridge id)kSecValueData];
[keyChain setObject:(__bridge id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly forKey:(__bridge id)kSecAttrAccessible];
Now, the iBeacon part,
- (void)locationManager:(CLLocationManager *)manager
didDetermineState:(CLRegionState)state
forRegion:(CLBeaconRegion *)region
{
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
{
// don't send any notifications
return;
}
if (state == CLRegionStateInside)
{
NSLog(@"Inside beacon region");
KeychainItemWrapper* keyChain = [[KeychainItemWrapper alloc] initWithIdentifier:keyChainIdentifier accessGroup:nil];
NSString *username =[keyChain objectForKey:(__bridge id)kSecAttrAccount];
NSString *password =[keyChain objectForKey:(__bridge id)kSecValueData];
[self.locationManager startRangingBeaconsInRegion:region];
if (username.length > 0 && password.length > 0 ) {
[self sendRequest:userName passCode:password];
}else {
// This is where it ends up when the app is TERMINATED and entering to the region !!!
[self displayError];
NSLog(@"I CAN SEE THIS !!!");
}
}
}
I kill the application, lock the screen and then enter into the iBeacon region while holding the device, then I can see everything is happening according to the logic but only can see the error message. Any help would be really appreciated.