CNContactVCardSerialization.data(with:) always returns nil result - swift

I am trying to retrieve all contacts and save them in VCard form (swift 4, XCode 9.0). But CNContactVCardSerialization.data(with:) always returns nil. Here is my code:
var contacts = [CNContact]()
let request = CNContactFetchRequest(keysToFetch:[CNContact.descriptorForAllComparatorKeys()])
do {
try contactsStore.enumerateContacts(with: request, usingBlock:
{ (contact:CNContact, result:UnsafeMutablePointer<ObjCBool>) in
self.contacts.append(contact)
})
}
catch {
}
// at this point all contacts are in the "contacts" array.
var data = Data()
do {
try data = CNContactVCardSerialization.data(with: contacts)
}
catch {
print("some error in contacts:" + String(describing: error));
}
print(">>>data:" + String(data.count))
Output:
2017-11-02 XXX [5224:449081]
Exception writing contacts to vCard (data): A property was not
requested when contact was fetched.
2017-11-02 XXX [5224:449362] XPC
connection interrupted
some error in contacts:nilError
>>>data:0
I red the question below but it does not help.
How to use method dataWithContacts in CNContactVCardSerialization?
I added "Privacy - Contacts Usage Description" into info.plist

Maybe you need to provide some specific keys to fetch?
UPD: Yep, if you want to fetch requests and serialize them, you have to set keys to fetch:
keysToFetch:#[[CNContactVCardSerialization descriptorForRequiredKeys]]

Change
let request = CNContactFetchRequest(keysToFetch:[CNContact.descriptorForAllComparatorKeys()])
To
let request = CNContactFetchRequest(keysToFetch:[CNContactVCardSerialization.descriptorForRequiredKeys()])

Related

Ambiguous reference to member 'save(_:completionHandler:)' with CloudKit save attempt

I'm trying to save back to CloudKit after updating a reference list and getting the error on the first line of this code block.
Error: Ambiguous reference to member 'save(_:completionHandler:)'
CKContainer.default().publicCloudDatabase.save(establishment) { [unowned self] record, error in
DispatchQueue.main.async {
if let error = error {
print("error handling to come")
} else {
print("success")
}
}
}
This sits within a function where the user going to follow a given location (Establishment). We're taking the existing establishment, and its record of followers, checking to see if the selected user is in it, and appending them to the list if not (or creating it if the list of followers is null).
Edit, in case helpful
//Both of these are passed in from the prior view controller
var establishment: Establishment?
var loggedInUserID: String?
#objc func addTapped() {
// in here, we want to take the logged in user's ID and append it to the list of people that want to follow this establishment
// which is a CK Record Reference
let userID = CKRecord.ID(recordName: loggedInUserID!)
var establishmentTemp: Establishment? = establishment
var followers: [CKRecord.Reference]? = establishmentTemp?.followers
let reference = CKRecord.Reference(recordID: userID, action: CKRecord_Reference_Action.none)
if followers != nil {
if !followers!.contains(reference) {
establishmentTemp?.followers?.append(reference)
}
} else {
followers = [reference]
establishmentTemp?.followers = followers
establishment = establishmentTemp
}
[this is where the CKContainer.default.....save block pasted at the top of the question comes in]
I've looked through the various posts on 'ambiguous reference' but haven't been able to figure out the source of my issue. tried to explicitly set the types for establisthmentTemp and followers in case that was the issue (based on the solutions to other related posts) but no luck.
Afraid I'm out of ideas as a relatively inexperienced newbie!
Help appreciated.
Documenting the solution that I figured out:
Combination of two issues:
I was trying to save an updated version of a CK Record instead of updating
I was not passing a CK Record to the save() call - but a custom object
(I believe point two was the cause of the 'ambiguous reference to member'
error)
I solved it by replacing the save attempt (first block of code in the question) with:
//first get the record ID for the current establishment that is to be updated
let establishmentRecordID = establishment?.id
//then fetch the item from CK
CKContainer.default().publicCloudDatabase.fetch(withRecordID: establishmentRecordID!) { updatedRecord, error in
if let error = error {
print("error handling to come")
} else {
//then update the 'people' array with the revised one
updatedRecord!.setObject(followers as __CKRecordObjCValue?, forKey: "people")
//then save it
CKContainer.default().publicCloudDatabase.save(updatedRecord!) { savedRecord, error in
}
}
}

Swift dealing with classes, UIButtons and tableView

This might sound like a very stupid question but I am fairly new to swift and cannot think how to go about this. As you can see in this Screenshot I have a search recipes textfield in RecipesViewController where the user enters a food item (which I use in the api call). After the user hits the button I make a call to an api and get data from that api and store that data in instance variable (searchRecipe array) in my RecipesViewController class. Now I am trying to show the data that I received from the api in a table view so I have another class called SearchRecipeTViewController. n this class I want to populate the table with the data I received from the api however when I try to access the searchRecipe array (which stores the elements received from the api) I get a blank value which I understand is due to the instance variable being initialized as "". But now how do I go about this so that I can get data from the api and display it on the table view when the user hits the button. Any suggestions would be appreciated.
Code to call and get data from api when button is clicked
#IBAction func SearchButton(sender: UIButton) {
if let recipe = RecipeSearchBar.text {
searchRecipe = recipe
}
//search recipe API call
endpoint = "http://api.yummly.com/v1/api/recipes? _app_id=apiID&_app_key=apiKey&q=\(searchRecipe)"
Alamofire.request(.GET, endpoint).responseJSON { response in
if response.result.isSuccess {
let data = response.result.value as! NSDictionary
if let matches = data["matches"] as? [[String: AnyObject]] {
for match in matches {
if let name = match["recipeName"] as? String {
self.recipeName.append(name);
}
}
}
}
else if response.result.isFailure {
print("Bad request")
}
}
}
Try using SwiftyJSON to manipulate the JSON the API returns. SwiftyJSON makes API calls that use JSON much easier. Here is the code I used that uses both Alamofire and SwiftyJSON.
//Use alamofire to connect to the web service and use GET on it
Alamofire.request(url).responseJSON { response in
if let error = response.result.error {
print("Error: \(error)")
return
}
//Value is the response the webservice gives, given as a Data Obkect
if let value = response.result.value {
//The information given from the webservice in JSON format
let users = JSON(value)
print("The user information is: \(users)")
//Get each username in the JSON output and print it
for username in users.arrayValue{
print(username["username"].stringValue)
}
}
}
Forgot to add a link to SwiftJSON: https://github.com/SwiftyJSON/SwiftyJSON

How to get e-mail subject from message:// URL in OSX Swift

I have a desktop app that receives e-mail URLs ("message://" scheme) from the drag&drop pasteboard and I want to get the Subject from the relevant message. The only clue I have, so far, is that the QuickLook library might give me an information object where I can retrieve this info from.
Since the QuickLook API seems to be rather in flux at the moment and most examples show how to use it in iOS, I simply cannot find a way to set up my "Preview" object using a URL and get the information from there.
I would like to avoid setting up my project as a QuickLook plugin, or setting up the whole preview pane / view scaffolding; at the moment I just want to get out what QuickLook loads before it starts displaying, but I can't comprehend what paradigm Apple wants me to implement here.
XCode 7.3.1.
It turns out I misinterpreted the contents of draggingInfo.draggingPasteboard().types as a hierarchical list containing only one type of info (URL in this case).
Had to subscribe to dragged event type kUTTypeMessage as String and retrieve the e-mail subject from the pasteboard with stringForType("public.url-name")
EDIT: Note that the current Mail.app will sometimes create a stack of mails when you drag an e-mail thread. Although the method above still works to get the subject of the stack, there is no URL in the dragging info then and since there's no list of Message-IDs available either, I had to resort to scraping the user's mbox directory:
// See if we can resolve e-mail message meta data
if let mboxPath = pboard.stringForType("com.apple.mail.PasteboardTypeMessageTransfer") {
if let automatorPlist = pboard.propertyListForType("com.apple.mail.PasteboardTypeAutomator") {
// Get the latest e-mail in the thread
if let maxID = (automatorPlist.allObjects.flatMap({ $0["id"]! }) as AnyObject).valueForKeyPath("#max.self") as? Int {
// Read its meta data in the background
let emailItem = draggingEmailItem
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
// Find the e-mail file
if let path = Util.findEmlById(searchPath: mboxPath, id: maxID) {
// Read its contents
emailItem.properties = Util.metaDataFromEml(path)
dispatch_async(dispatch_get_main_queue(), {
// Update UI
});
}
}
}
}
}
Util funcs:
/* Searches the given path for <id>.eml[x] and returns its URL if found
*/
static func findEmlById(searchPath searchPath: String, id: Int)-> NSURL? {
let enumerator = NSFileManager.defaultManager().enumeratorAtPath(searchPath)
while let element = enumerator?.nextObject() as? NSString {
switch (element.lastPathComponent, element.pathExtension) {
case (let lpc, "emlx") where lpc.hasPrefix("\(id)"):
return NSURL(fileURLWithPath: searchPath).URLByAppendingPathComponent(element as String)!
case (let lpc, "eml") where lpc.hasPrefix("\(id)"):
return NSURL(fileURLWithPath: searchPath).URLByAppendingPathComponent(element as String)!
default: ()
}
}
return nil
}
/* Reads an eml[x] file and parses it, looking for e-mail meta data
*/
static func metaDataFromEml(path: NSURL)-> Dictionary<String, AnyObject> {
// TODO Support more fields
var properties: Dictionary<String, AnyObject> = [:]
do {
let emlxContent = try String(contentsOfURL: path, encoding: NSUTF8StringEncoding)
// Parse message ID from "...\nMessage-ID: <...>"
let messageIdStrMatches = emlxContent.regexMatches("[\\n\\r].*Message-ID:\\s*<([^\n\r]*)>")
if !messageIdStrMatches.isEmpty {
properties["messageId"] = messageIdStrMatches[0] as String
}
}
catch {
print("ERROR: Failed to open emlx file")
}
return properties
}
Note: If your app is sandboxed you will need the com.apple.security.temporary-exception.files.home-relative-path.read-only entitlement set to an array with one string in it: /Library/

Can't retrieve CKRecords to resolve conflicts on CKErrorCodeServerRecordChanged in CKModifyRecordsOperation

I ran into some problem when trying to handle errors when doing batch operations on records in CloudKit.
I am successfully extracting the dictionary containing the partial errors, which I can iterate over. However, I am not able to get the records needed to resolve the conflict for CKErrorCodeServerRecordChanged. According to the docs I should be able to get 3 records out of the dictionary:
CKRecordChangedErrorServerRecordKey
CKRecordChangedErrorAncestorRecordKey
CKRecordChangedErrorClientRecordKey
Thank you for any hints on what I am doing wrong here.
func pushRecordChangesForZoneID(recordZoneID: CKRecordZoneID) {
// ...
modifyRecordsOperation.modifyRecordsCompletionBlock = { (savedRecords, deletedRecordIDs, error) -> Void in
if (error != nil) {
if error.code == CKErrorCode.PartialFailure.rawValue {
if let errorDict = error.userInfo?[CKPartialErrorsByItemIDKey] as? [CKRecordID : NSError] {
for (recordID, partialError) in errorDict {
if partialError.code == CKErrorCode.ServerRecordChanged.rawValue {
if let userInfo = partialError.userInfo {
let serverRecord = userInfo[CKRecordChangedErrorServerRecordKey] as? CKRecord
// serverRecord will always be nil
}
}
}
}
}
}
}
}
Additional information: When I print the description of the userInfo dict of the partial error (partialError.userInfo) it doesn’t look like it contains the other CKRecords:
[NSDebugDescription: CKInternalErrorDomain: 2037, NSLocalizedDescription: Error saving record <CKRecordID: 0x7fb41bf7e640; DA39FE08-AB0B-4F07-A42E-F5732B114706:(userData:__defaultOwner__)> to server: Protection data didn't match, NSUnderlyingError: <CKError 0x7fd89a92d370: "Unknown Error" (2037)>]
The description of the source error's dictionary (errorDict) looks like this (and I can successfully get the dictionary containing the recordIDs and partial errors out via CKPartialErrorsByItemIDKey):
[<CKRecordID: 0x7fb5bb88afa0; C1575083-F992-448A-8D77-D62C4A42D696:(userData:__defaultOwner__)>: <CKError 0x7fb5b961c6a0: "Batch Request Failed" (22/2024); server message = "Atomic failure"; uuid = 1E4C0FD5-EC10-4071-B277-102A9F1B0E5E; container ID = "iCloud.net.neverthesamecolor.atsumeru">, <CKRecordID: 0x7fb5bb848ad0; DA39FE08-AB0B-4F07-A42E-F5732B114706:(userData:__defaultOwner__)>: <CKError 0x7fb5b9653060: "Server Record Changed" (14/2037); "Error saving record <CKRecordID: 0x7fb41bd9ca50; DA39FE08-AB0B-4F07-A42E-F5732B114706:(userData:__defaultOwner__)> to server: Protection data didn't match">]
The docs say it's a custom zone feature only.
Edit 21st Jan 2016: It's working for me right now even in the default zone and public database. This is a big change from before.

How do you store a dictionary on Parse using swift?

I am very new to swift and I don't know Obj C at all so many of the resources are hard to understand. Basically I'm trying to populate the dictionary with PFUsers from my query and then set PFUser["friends"] to this dictionary. Simply put I want a friends list in my PFUser class, where each friend is a PFUser and a string.
Thanks!
var user = PFUser()
var friendsPFUser:[PFUser] = []
var friendListDict: [PFUser:String] = Dictionary()
var query = PFUser.query()
query!.findObjectsInBackgroundWithBlock {
(users: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(users!.count) users.")
// Do something with the found objects
if let users = users as? [PFUser] {
friendsPFUser = users
for user in friendsPFUser{
friendListDict[user] = "confirmed"
}
user["friends"] = friendListDict //this line breaks things
user.saveInBackground()
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
To be clear, this code compiles but when I add
user["friends"] = friendListDict
my app crashes.
For those who might have this issues with. "NSInternalInconsistencyException" with reason "PFObject contains container item that isn't cached."
Adding Objects to a user (such as arrays or dictionaries) for security reasons on Parse, the user for such field that will be modified must be the current user.
Try signing up and using addObject inside the block and don't forget do save it!
It helped for a similar problem I had.