//import contacts by using contact picker
CNContactPickerViewController *contactPicker = [CNContactPickerViewController new];
contactPicker.delegate = self;
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:#"phoneNumbers.#count >= 1"];
contactPicker.predicateForEnablingContact = filterPredicate;
[self presentViewController:contactPicker animated:YES completion:nil];
- (void) contactPicker:(CNContactPickerViewController *)picker
didSelectContacts:(NSArray<CNContact *> *)contacts {
//code for selecting multiple contacts
}
-(void)contactPickerDidCancel:(CNContactPickerViewController *)picker {
NSLog(#"Cancell");
}
I am using the above code to import the contacts, i am able to import the contacts. But i need to remove the searchbar from the CNContactPickerViewController. I have already tried the below solution provided in the stack overflow but its not working. Any help will be really appreciated.
How to hide/Remove the search bar on Contact Picker
As the CNContactPickerViewController is a UIRemoteView you have no access to it. As such this is not possible.
Related
I am using CTAssetsPickerController https://github.com/chiunam/CTAssetsPickerController to import images from the photos app. I want to disable importing from the 'Hidden' album some users may have. If you have What's App and a hidden album you'll notice that you can't import from said album. I want to be able to do the same but been searching for hours and haven't gotten anywhere yet. Appreciate your help
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType {
// Create the image picker
// request authorization status
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status){
dispatch_async(dispatch_get_main_queue(), ^{
// init picker
self.imagePickerController = [[CTAssetsPickerController alloc] init];
// set delegate
self.imagePickerController.delegate = self;
// create options for fetching photo only
PHFetchOptions *fetchOptions = [PHFetchOptions new];
fetchOptions.predicate = [NSPredicate predicateWithFormat:#"mediaType == %d", PHAssetMediaTypeImage];
// assign options
self.imagePickerController.assetsFetchOptions = fetchOptions;
// Optionally present picker as a form sheet on iPad
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
self.imagePickerController.modalPresentationStyle = UIModalPresentationFormSheet;
// present picker
[self presentViewController:self.imagePickerController animated:YES completion:nil];
});
}];
Looking at the repo for CTAssetsPickerController, it seems you can set the album type by setting a property called assetCollectionSubtype to an array of integer values, corresponding to Apple's PHAssetCollectionSubtype enum. https://developer.apple.com/library/ios/documentation/Photos/Reference/PHAssetCollection_Class/#//apple_ref/c/tdef/PHAssetCollectionSubtype
So after a little more research I found that the framework has a delegate method that enables you to disable assets.
To solve my problem I implemented this method:
- (BOOL)assetsPickerController:(CTAssetsPickerController *)picker shouldEnableAsset:(PHAsset *)asset {
return !asset.isHidden;
}
The hidden album still shows up but importing from it is disabled.
I'm trying to add phone selection to an iOS app using the Contacts API in iOS9. It should call up the CNContactsPickerViewController and let the use choose only a contact which has at least one phone number associated.
If there are more than one phone number the user should be prompted to choose which one.
If there is only one, simply choosing the contact from the initial list should select the contact.
The behavior is almost correct. I am not allowed to choose a contact with no phone number. If I select a contact with multiple phones, I get prompted to choose one and the delegate is called. If I select a contact with one phone number, it returns but no delegate is called.
I suspect I need to add a NSPredicate for predicateForSelectionOfProperty but I cannot find any format querying against the CNContactProperty which works. If so, what's the appropriate predicate for predicateForSelectionOfProperty or if not, what am I missing?
- (void)initiateIdSelectionViaContacts
{
CNContactPickerViewController *picker = [[CNContactPickerViewController alloc] init];
NSArray *propertyKeys = #[CNContactPhoneNumbersKey, CNContactGivenNameKey, CNContactFamilyNameKey, CNContactOrganizationNameKey];
NSPredicate *enablePredicate = [NSPredicate predicateWithFormat:#"(phoneNumbers.#count > 0)"];
NSPredicate *contactSelectionPredicate = [NSPredicate predicateWithFormat:#"phoneNumbers.#count == 1"];
picker.displayedPropertyKeys = propertyKeys;
picker.predicateForEnablingContact = enablePredicate;
picker.predicateForSelectionOfContact = contactSelectionPredicate;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
}
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
{
}
I need to access the contacts of my iPhone into my app.
I am using Xcode 7.2 and IOS-9.
Give me best framework or library which is usefully for me.
I try to work on the ABAddressbook framework but it has been deprecated.
Thank You.
Apple has introduced new frameworks Contacts and ContactsUI for accessing contacts for iOS9 and above.
The previous ABAddressbook framework has been deprecated. You might find a lot of info in SO on this.
You can make use of ContactsUI framework for iOS9+ devices
- (void) presentContacts
{
CNContactPickerViewController *contactPicker = [[CNContactPickerViewController alloc] init];
contactPicker.delegate = self;
[_myViewController presentViewController:contactPicker animated:YES completion:nil];
}
//Listen for delegates
- (void) contactPickerDidCancel: (CNContactPickerViewController *) picker
{
NSLog(#"didCancel");
}
- (void) contactPicker: (CNContactPickerViewController *) picker
didSelectContact: (CNContact *) contact
{
NSLog(#"didSelectContact"):
}
- (void) contactPicker: (CNContactPickerViewController *) picker
didSelectContactProperty: (CNContactProperty *) contactProperty
{
NSLog(#"didSelectProperty");
}
CNContactPickerViewController
https://developer.apple.com/library/ios/documentation/ContactsUI/Reference/CNContactPickerViewController_Class/
Address book ui framework
https://developer.apple.com/library/ios/documentation/AddressBookUI/Reference/AddressBookUI_Framework/index.html
MFMessageComposeViewController https://developer.apple.com/library/ios/documentation/MessageUI/Reference/MFMessageComposeViewController_class/
tutorials http://www.appcoda.com/ios-programming-import-contact-address-book/
http://gabriel-tips.blogspot.in/2012/02/how-to-import-contacts-from-address.html
I know that Apple deprecated the old Search Display Controller. I have a table with data, and I'm simply making a search bar that will allow the user to search for data (this data only contains alphabetical names) from the table view with a search bar. I was trying to use the following code at one point to detect if the user is typing something:
if(tableView == self.searchDisplayController.searchResultsTableView)
But I cannot because Apple has deprecated the old way. I understand that I have to use the UISearchController now, and I've looked at Apple's documentation and the sample code they provide, but I have not been able to understand it. I've looked everywhere, but have found no solid examples/tutorials on how to do this with objective-c. Can anyone please explain how we use the tableview in conjunction with the UISearchController in order to allow the user to search the data?
I have a simple demo project here
You need a TableviewController to show data,and a TableviewController to show searchResult
For example,(Code from demo project in the link)
Declare a SearchResultViewController
#interface SearchResultViewController : UITableViewController
Then in main tableview
#interface SearchTableViewController()<UISearchBarDelegate,UISearchResultsUpdating>
#property (strong,nonatomic)NSMutableArray * dataArray;
#property (strong,nonatomic)UISearchController * searchcontroller;
#property (strong,nonatomic)SearchResultViewController * resultViewController;
#end
In viewDidLoad,setup everything
self.resultViewController = [[SearchResultViewController alloc] init];
self.searchcontroller = [[UISearchController alloc] initWithSearchResultsController:self.resultViewController];
self.searchcontroller.searchBar.delegate = self;
self.resultViewController.tableView.delegate = self;
[self.searchcontroller.searchBar sizeToFit];
self.searchcontroller.searchResultsUpdater = self;
self.searchcontroller.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
self.tableView.tableHeaderView = self.searchcontroller.searchBar;
Then in delegate method,do real search and update searchResultViewController
#pragma mark - search bar delegate
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
}
#pragma mark - UISearchResultUpdating
//Do real search,this is up to you
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString * searchtext = searchController.searchBar.text;
NSArray * searchResults = [self.dataArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
BOOL result = NO;
if ([(NSString *)evaluatedObject hasPrefix:searchtext]) {
result = YES;
}
return result;
}]];
SearchResultViewController *tableController = (SearchResultViewController *)self.searchcontroller.searchResultsController;
tableController.dataArray = searchResults;
[tableController.tableView reloadData];
}
I'm currently working on a project where I have to filter through a NSArray (displayed in an UITableView). Problem is, there is neither a XIB file nor a storyboard.
My question is (I already got the predicate and search method et.c set up), how do I get the SearchBar to show up in my program only using code - and ofc I need to be able to work with it.
I'd be really glad about some help with this issue, because every tutorial I found either uses storyboards or xibs :(
Thank you very much up front!
Btw those are the two methods I got so far concerning the search bar, the TableView is also set up already.
-(void) filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains[c] %#", searchText];
self.searchResults = [self.array filteredArrayUsingPredicate:predicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles]objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
You can do something like this:
In your .h add this:
#property (nonatomic,strong) UISearchBar *searchBar;
Add this in your viewDidLoad method
self.searchBar = [[UISearchBar alloc]init];
self.searchBar.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
self.searchBar.delegate = self;
//Customize the searchBar
[self.view addSubview:self.searchBar];
And whenever you want to search, press the search button within the keyboard. A delegate will be called when you do this. Like so,
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
//Do some search
}
You can access the entered the text using the searchBar.text property. I am assuming that you have implemented the relevant protocols.
If there is no XIB file and no Storyboard, it is initiated somewhere in the code (probably in the viewDidload) looking something like this.
UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
searchBar.delegate = self;
//etc..
Now first add a property in the interface
#property (nonatomic, strong)UISearchBar *searchBar;
And change this line in the viewDidLoad:
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
Now you can us self.searchBar anywhere in your code.
Edit:
And don't forget to add this to the interface if it wasn't already:
#interface ViewController () <UISearchBarDelegate, UISearchDisplayDelegate>