I am trying to get a JSON (NSString *) of the address book , in async task , however I want it to be asynchronously and with a completion block .
I have managed to retrieve that easy without the completion block , however when I am adding the completion block I have the following compiler error :
Incompatible block pointer types passing NSString *(^)(bool, CFErrorRef)' to parameter of type 'ABAddressBookRequestAccessCompletionHandler' (aka 'void (^)(bool, CFErrorRef)')
here is my code
+ (NSString *) getAddressBook:(id (^)())block {
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if (status == kABAuthorizationStatusDenied)
{
// if you got here, user had previously denied/revoked permission for your
// app to access the contacts, and all you can do is handle this gracefully,
// perhaps telling the user that they have to go to settings to grant access
// to contacts
[[[UIAlertView alloc] initWithTitle:nil message:#"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
return nil;
}
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (error)
{
NSLog(#"ABAddressBookCreateWithOptions error: %#", CFBridgingRelease(error));
if (addressBook) CFRelease(addressBook);
return nil;
}
if (status == kABAuthorizationStatusNotDetermined)
{
// present the user the UI that requests permission to contacts ...
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) -> THE ERROR OCCURS HERE !
{...
I think the problem is within my completion blocks , but I can't find some good example/tutorial for my specific issue I would be really glad if some 1 could help me please.
Your problem is that you are declaring getAddressBook like this(if we strip block)
-(id)block
And you are trying to pass it as a function of type:
-(void)block:(BOOL)aBool error:(CFErrorRef*)error
Blocks are quite hard to get used with syntax(which is improved in swift) however I'm always refering to this site when not sure:
block syntax reference
UPDATE:
You have to actually wait for your async method finish. You may also refer to official documentation
+ (void) getAddressBook:((^)(NSString* result))block {
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if (status == kABAuthorizationStatusDenied)
{
// if you got here, user had previously denied/revoked permission for your
// app to access the contacts, and all you can do is handle this gracefully,
// perhaps telling the user that they have to go to settings to grant access
// to contacts
[[[UIAlertView alloc] initWithTitle:nil message:#"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
block(nil);
}
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (error)
{
NSLog(#"ABAddressBookCreateWithOptions error: %#", CFBridgingRelease(error));
if (addressBook) CFRelease(addressBook);
block(nil);
}
if (status == kABAuthorizationStatusNotDetermined)
{
// present the user the UI that requests permission to contacts ...
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) -> THE ERROR OCCURS HERE !
{
NSString* result = whatever
block(result);
});
}
}
And use it like this:
[YourClass getAddressBook:^(NSString* result)
{
//now you really have result
}];
Related
I am working on Address book in My application,I just want to get an alertview for accessing Address.How Can i show System Generated Alert for accessing addressBook?
If you want to show the user a custom message when asking for permission to use their contacts or address book, instead of the default one shown above, you need to add this field to your Info.plist file:
NSContactsUsageDescription
Here is apple developer link for the same:
https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW14
// Check the authorization status of your application for Address Book
-(void)checkAddressBookAccess
{
switch (ABAddressBookGetAuthorizationStatus())
{
// Update our UI if the user has granted access to their Contacts
case kABAuthorizationStatusAuthorized:
[self showAddressBook];
break;
// Prompt the user for access to Contacts if there is no definitive answer
case kABAuthorizationStatusNotDetermined :
[self requestAddressBookAccess];
break;
// Display a message if the user has denied or restricted access to Contacts
case kABAuthorizationStatusDenied:
case kABAuthorizationStatusRestricted:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning"
message:#"You have not granted access to your contacts or you have revoked it earlier."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
break;
default:
break;
}
}
// Prompt the user for access to their Address Book data
-(void)requestAddressBookAccess
{
TF_Message_View_Controller * __weak weakSelf = self;
ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error)
{
if (granted)
{
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf showAddressBook];
});
}
});
}
If you wanted to show the alert asking for permission, you can use the following code
This code works with Contacts framework, which is available only for iOS 9.0+
- (void)checkContactStoreAccess {
self.contactsStrore = [[CNContactStore alloc] init];
switch ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]) {
case CNAuthorizationStatusAuthorized: {
[self fetchContactsFromContactStore];
break;
}
case CNAuthorizationStatusNotDetermined:{
[self requestContactsAccess];
break;
}
case CNAuthorizationStatusDenied:
case CNAuthorizationStatusRestricted:{
//show info that access denied for contact and redirect user to settings with
[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
}
}
- (void)requestContactsAccess {
typeof(self) __weak weakSelf = self;
[self.contactsStrore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
[weakSelf fetchContactsFromContactStore];
}];
}
I know this may be a simple question but I can't find what I'm looking for on the internet. I'm using the LocalAuthentication framework from iOS 8 in my project and my code is here:
if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:#"Let's just quickly check that you are the device owner."
reply:^(BOOL success, NSError *error) {
dispatch_async (dispatch_get_main_queue(), ^{
if (error) {
// Error occurred
} else if (success) {
// Device owner, success!
} else {
// Not device owner
}
});
}];
}
But I want to know when the user tapped 'Enter password' which is LAErrorUserFallback. However I just want to know how to compare the error variable I have there with the LAErrorUserFallback to see the outcome error.
I have tried this:
if (error) {
if (error == LAErrorUserFallback) {
// User tapped 'Enter password'
}
}
but obviously these are not the same type.
Any help?
According to the docs, that's the error code.
So try something like error.code == LAErrorUserFallback.
I'm currently trying to add Facebook SDK to the iOS version of my app. The login, logout, share, and request features are all currently working, but I'm having difficulty getting the MessageDialog feature to work. The Facebook app and the Facebook Messenger app are currently installed on my devices. However, every time I call 'FBDialog canPresentMessageDialogWithParams:params' it returns false.
My guess is that this feature doesn't work while the app is still in development mode, but that is just a guess. Does anyone know if Message Dialog works with apps that are under development? Or do you have any ideas as to what I am doing wrong?
I've also included my code in case I've made any bone-headed mistakes. Any help is greatly appreciated!
// Check if the Facebook app is installed and we can present the share dialog
FBLinkShareParams *params = [[FBLinkShareParams alloc] init];
params.link = [NSURL URLWithString:#"https://www.facebook.com/"];
params.name = #"Flash Bear";
params.caption = nil;
params.picture = nil;
params.linkDescription = #"I'm playing Flash Bear. Why don't you come join me?";
// If the Facebook Messenger app is installed and we can present the share dialog
if ([FBDialogs canPresentMessageDialogWithParams:params]) {
// Present message dialog
[FBDialogs presentMessageDialogWithParams:params
clientState:nil
handler: ^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Error messaging link: %#", error.description);
} else {
// Success
NSLog(#"result %#", results);
}
}];
} else {
// Present the feed dialog
// Put together the dialog parameters
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Sorry!"
message:#"There was an error connecting to FB Messenger. Please make sure that it is installed."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
I also had same problem with my application.
I resolved it by checking active session state. If state for active session is other than open then we need to open session again.
For doing the same use following code.
if([FBSession activeSession].state != FBSessionStateOpen){
BOOL result = [FBSession openActiveSessionWithAllowLoginUI:NO];
if(result)
{
//Do your stuff here
}
}
Best luck.
I am using below Code, My application don't ask permission on iOS 6 while on iOS 7 and above version it ask for Contact permission access. On iOS 6 it doesn't show app in privacy setting as well. I have read some other thread but not found any solutions.
App crashed in iOS 6 when user changes Contacts access permissions
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"6.0")) {
__block CDNDeviceContact *controller = self;
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef,
^(bool granted, CFErrorRef error) {
if (granted)
[controller loadContacts];
else [controller doAlertForContact];
});
} else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
[self loadContacts];
} else {
[controller doAlertForContact];
}
if (addressBookRef) CFRelease(addressBookRef);
}
If the user has previously been presented with the request to get permission, it will not show again. According to the documentation,
The user is only asked for permission the first time you request access. Later calls use the permission granted by the user.
If testing in the simulator, I recommend that you go to iOS Simulator -> Reset Content and Settings so that you are able to simulate the event.
You don't need SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO and you also don't need to create an ABAddressBookRef.
For me, this is works like a charm:
if (ABAddressBookGetAuthorizationStatus) {
switch (ABAddressBookGetAuthorizationStatus()) {
case kABAuthorizationStatusNotDetermined:{
ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error) {
self.addContactButton.enabled = granted;
if (granted) {
// granted
} else {
// User denied access
}});
} break;
case kABAuthorizationStatusDenied: break;
case kABAuthorizationStatusAuthorized: break;
default: break;
}
}
I am trying to print out all of my phone's contacts to the console, using NSLog(). Currently this code is just printing (null).
.h
#property (nonatomic, strong) NSMutableArray *contactsObjects;
.m
#synthesize contactsObjects;
//lazy instantiation.
- (NSMutableArray *)contactsObjects
{
if(!contactsObjects)
{
contactsObjects = [[NSMutableArray alloc]init];
}
return contactsObjects;
}
- (void)viewWillAppear:(BOOL)animated
{
CFErrorRef error = nil;
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, &error);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
// First time access has been granted, add all the user's contacts to array.
CFMutableArrayRef contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
} else {
// User denied access.
// Display an alert telling user that they must allow access to proceed to the "invites" page.
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add all the user's contacts to array.
CFMutableArrayRef contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
}
else {
// The user has previously denied access
// Send an alert telling user that they must allow access to proceed to the "invites" page.
}
NSLog(#"%#", contactsObjects);
}
I get two warnings here:
I have no idea what I am supposed to do in order to properly print the names and numbers of my contacts to the console.
How do I print my contacts names and numbers?
You have a scope problem with your code. The contactsObjects variables in viewWillAppear: are not related to the ivar you have called contactsObjects. You're declaring new variables that are using the same name. The NSLog() at the end, on the other hand, is the ivar. But setting those other variables didn't put anything into the ivar, so you see (null), which is how NSLog() represents "no object".
Fix this by not making new variable declarations, but using the ivar.
if (granted) {
contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
You will also need to cast these:
contactsObjects = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBookRef);
(Also, the function doesn't return a mutable array, so you may have trouble down the road with that.)
The second problem is that ABAddressBookRequestAccessWithCompletion() doesn't stop and wait for its completion Block to run. While access is being requested, the rest of your method carries on, so you reach the NSLog() before contactsObjects is actually set in that case.
You say it prints out null and that you get an error. But this would explain your error.
contactObjects is defined within the if block and the else if block. So by the time you are outside of your conditional it's no longer defined.
Try this
- (void)viewWillAppear:(BOOL)animated
{
CFErrorRef error = nil;
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, &error);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
// First time access has been granted, add all the user's contacts to array.
contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
} else {
// User denied access.
// Display an alert telling user that they must allow access to proceed to the "invites" page.
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add all the user's contacts to array.
contactsObjects = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
}
else {
// The user has previously denied access
// Send an alert telling user that they must allow access to proceed to the "invites" page.
}
NSLog(#"%#", contactsObjects);
}