how can I remove duplicate Contact with OK alert button? - ios

how can I remove duplicate Contact from my table view when tap OK alert button?
Here's my findDuplicateContacts()
#objc fileprivate func findDuplicateContacts() {
let keys = [CNContactIdentifierKey as CNKeyDescriptor, CNContactFormatter.descriptorForRequiredKeys(for: .fullName)]
let request = CNContactFetchRequest(keysToFetch: keys)
var contactsByName = [String: [CNContact]]()
do {
try self.contactStore.enumerateContacts(with: request) { contact, stop in
guard let name = CNContactFormatter.string(from: contact, style: .fullName) else { return }
contactsByName[name] = (contactsByName[name] ?? []) + [contact] // or in Swift 4, `contactsByName[name, default: []].append(contact)`
}
} catch let err {
print("error:", err)
}
let duplicates = contactsByName.filter { $1.count > 1 }
let alert = UIAlertController(title: "Alert", message: "Number of duplicates: \(duplicates.count)", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction!) in
//HERE I WANT TO REMOVE DUPLICATES
print("you have pressed the ok button")
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
print(duplicates)
self.tableView.reloadData()
}
Thanks in advance for reply

You should use CNSaveRequest class func delete(_ contact: CNMutableContact) method, executing with func execute(_ saveRequest: CNSaveRequest) method of CNContactStore class
This example removes all other contacts and keeps only one (position 0) but you can add a method to determine which contact is more complete and keep that one
Full Code
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction!) in
print("you have pressed the ok button")
var arrayOfContactsRequests : [CNSaveRequest] = []
for dict in duplicates {
for (index,contact) in dict.value.enumerated() {
if(index != 0) {
let saveRequest = CNSaveRequest()
saveRequest.delete(contact.mutableCopy() as! CNMutableContact)
arrayOfContactsRequests.append(saveRequest)
}
}
}
debugPrint(duplicates)
for request in arrayOfContactsRequests {
do{
try self.contactStore.execute(request)
}
catch let err {
print("error:", err)
}
}
}))
This answer was possible with help of this answer How to convert CNContact to CNMutableContact?

Related

How to put a success or error alert inside an options alert?

I have a problem trying to put an alert message after completing the action. The application crashes.
#IBAction func deleteAccountAction(_ sender: Any) {
let userID = prefs.value(forKey: "userId") as! String
print("user id: \(userID)")
let alert = UIAlertController(title: "Delete account", message: "Are you sure you want to delete your account?, This action cannot be reversed.", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
// ...
}
let okayAction = UIAlertAction(title: "OK", style: .default) { (action) in
RestAPIManager.sharedInstance.deleteAccount(userID: userID){
(json, error) in
if(json != JSON.null){
print(json)
if(json["success"] == true){
//here i want succes alert
}else{
self.errorAlert()
}
}else{
}
}
}
alert.addAction(okayAction)
alert.addAction(cancelAction)
self.present(alert, animated: true)
}
func errorAlert(){
var dialogMessage = UIAlertController(title: "Error", message: "Error", preferredStyle: .alert)
self.present(dialogMessage, animated: true, completion: nil)
}
I tried to put an alert message after the action but I can't.
is solved needs to be async.
DispatchQueue.main.async {
self.errorAlert()
}

Show UIAlertController every time view controller launches

Currently, the UIAlertController appears when the user taps on the HeaderButton. I am trying to make the UIAlertController automatically appear every time the view controller initially launches. Any suggestions?
// MARK: - RestaurantListTableViewHeaderDelegate
extension RestaurantListViewController: RestaurantListTableViewHeaderDelegate {
func didTapHeaderButton(_ headerView: RestaurantListTableViewHeader) {
let locationPicker = UIAlertController(title: "Select location", message: nil, preferredStyle: .actionSheet)
for location in RestaurantListViewController.locations {
locationPicker.addAction(UIAlertAction(title: location, style: .default) { [weak self] action in
guard let `self` = self else { return }
self.currentLocation = action.title
self.tableView.reloadData()
})
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
locationPicker.addAction(cancelAction)
present(locationPicker, animated: true)
}
}
I kept the extension for when the Header Button gets tapped and I added the following to viewDidLoad:
// Code for showing alert
let locationPicker = UIAlertController(title: "Select location", message: nil, preferredStyle: .actionSheet)
for location in RestaurantListViewController.locations {
locationPicker.addAction(UIAlertAction(title: location, style: .default) { [weak self] action in
guard let `self` = self else { return }
self.currentLocation = action.title
self.tableView.reloadData()
})
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
locationPicker.addAction(cancelAction)
present(locationPicker, animated: true)
It's not an elegant solution but it will work:
var alertAlreadyShown = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if !alertAlreadyShown {
alertAlreadyShown = true
/* Code for showing alert */
}
}

Want to use UIAlertController inside an Alamofire reponseJSON block

The UIAlertControllers are not being executed or are not being displayed. If i solely write a print statement then there's output in the console but now the print statements are also not being executed(if i write them along with the UIAlertControllers like in the code i wrote below).
Alamofire.request(some_url, method: .post, parameters: data, encoding: URLEncoding.default, headers: nil).responseJSON{
response in
let json = JSON(response.result.value)
print(json)
self.eventid = json[0]["EventRegID"].stringValue
if !json[0]["AuthKeyError"].exists(){
if !json[0]["ExceptionOccured"].exists(){
if !json[0]["RegistrationFailed"].exists() {
if !json[0]["EventInHold"].exists() {
if json[0]["RegistrationSuccess"].exists() {
let alertController = UIAlertController(title: "", message: json[0]["RegistrationSuccess"].stringValue, preferredStyle: .alert)
let no1Action = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
print("The user has registered successfully")
}
alertController.addAction(no1Action)
}
else{
}
}
else {
let alertController = UIAlertController(title: "", message: "Event is on hold.", preferredStyle: .alert)
let no2Action = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
print("The event is on hold.")
}
let yes2Action = UIAlertAction(title: "GO", style: .default) { (action) -> Void in
self.performSegue(withIdentifier: "bullshit", sender: self)
}
alertController.addAction(no2Action)
alertController.addAction(yes2Action)
}
}
else {
print("Registration failed due to connection issues. Please login.")
let alertController = UIAlertController(title: "", message: "Registration failed", preferredStyle: .alert)
let no3Action = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
print("The registration failed")
}
alertController.addAction(no3Action)
}
}
else {
print("There's some problem with the database")
let alertController = UIAlertController(title: "", message: "Some problem with the server", preferredStyle: .alert)
let no4Action = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
print("The user has registered successfully")
}
alertController.addAction(no4Action)
}
}
else {
print("AuthKeyError")
let alertController = UIAlertController(title: "", message: "Auth key error", preferredStyle: .alert)
let no5Action = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
print("AAUTH KEY ERROR")
}
alertController.addAction(no5Action)
}
}
}
else {
print("not ok")
}
}
you need to present the alertcontroller after addAction
presentViewController(alertController, animated: true, completion: nil)
maybe you have to present it in a new operation
OperationQueue.main.addOperation {
presentViewController(alertController, animated: true, completion: nil)
}

“Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)”?

This is my swift code
i will get above error on this line : guard (newUser["status"] as! Int != 0)
#IBAction func signInButton(_ sender: UIButton) {
if validator(){
DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async(execute: {
let datas:[String:String] = ["email":self.emailField.text!,"name": self.nameField.text!,"password" : self.passwordField.text!]
DispatchQueue.main.async {
SwiftSpinner.show("Signin' in...")
}
let newUser:NSDictionary = self.marketcloud!.createUser(datas)
print(newUser)
DispatchQueue.main.async {
SwiftSpinner.hide()
guard (newUser["status"] as! Int != 0) else {
let alertController = UIAlertController(title: "Error", message: "Email already in use. Try with a different one!", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Close",
style: UIAlertActionStyle.destructive,
handler: nil))
self.present(alertController, animated: true, completion: nil)
return
}
let alertController = UIAlertController(title: "Ok!", message: "User created successfully!", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in
UserData.setLastRegisteredUser(self.emailField.text!, password: self.passwordField.text!);
print("Setted UserData \n \(UserData.getLastRegistedUserEmail(),UserData.getLastRegisteredUserPassword())")
//returns to the login view
let next = self.storyboard!.instantiateViewController(withIdentifier: "viewController") as! ViewController
next.downloadProducts = false
next.load = true
self.navigationController?.pushViewController(next, animated: true)
}));
self.present(alertController, animated: true, completion: nil)
}
})
}
}
Try to understand how guard works.
The condition must be an optional binding with let or a boolean expression
guard let status = newUser["status"] as? Int, status != 0 else { ...
And no parentheses in Swift.

Optimizing a CloudKit sign-in on app launch with error handling. How can I better handle the optional chaining? Swift 3.0/Xcode 8.1

Is there a cleaner, swiftier solution to handle the optional chaining happening in my code below? I'm setting up the user for CloudKit access in my custom function runCKsetup():
func runCKsetup() {
container.requestApplicationPermission(.userDiscoverability) { (status, error) in
guard error == nil else {
if let error = error as? NSError {
if let errorDictionary: AnyObject = error.userInfo as? Dictionary<String, AnyObject> as AnyObject? {
let localizedDescription = errorDictionary["NSLocalizedDescription"]! as! String!
if localizedDescription! == "This operation has been rate limited" {
// Create an alert view
let alert = UIAlertController(title: "Network Error", message: localizedDescription!, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Test for Connection", style: UIAlertActionStyle.default) { (action) in
self.runCKsetup()
})
self.present(alert, animated: true, completion: nil)
} else {
// Create an alert view
let alert = UIAlertController(title: "Sign in to iCloud", message: localizedDescription!, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default) { (action) in
})
self.present(alert, animated: true, completion: nil)
}
}
}
return
}
if status == CKApplicationPermissionStatus.granted {
self.container.fetchUserRecordID { (recordID, error) in
guard error == nil else {
self.presentMessageAlert((error?.localizedDescription)!, title: "Error", buttonTitle: "Ok")
return }
guard let recordID = recordID else { return }
self.container.discoverUserIdentity(withUserRecordID: recordID, completionHandler: { (info, fetchError) in
//do something with the users names: e.g. print("\(info?.nameComponents?.givenName) \(info?.nameComponents?.familyName)")
})
}
}
}
}

Resources