UIActivityViewController联系人分享?
我正在尝试考虑使用UIActivityViewController
(或使用SMS)从一个设备到另一个设备共享联系的方法。
到目前为止,我一直在寻找,我不知道如何使用我的应用程序在设备之间共享联系人。
有任何想法吗?
经过几个月的搜索,我终于找到了实现这一目标的方法。
您可以使用此代码在UIActivityViewController中共享单个CNContact:
let contact: CNContact = /* your contact here */ let fileManager = NSFileManager.defaultManager() let cacheDirectory = try! fileManager.URLForDirectory(NSSearchPathDirectory.CachesDirectory, inDomain: NSSearchPathDomainMask.UserDomainMask, appropriateForURL: nil, create: true) let fileLocation = cacheDirectory.URLByAppendingPathComponent("\(CNContactFormatter().stringFromContact(contact)!).vcf") let contactData = try! CNContactVCardSerialization.dataWithContacts([contact]) contactData.writeToFile(fileLocation.path!, atomically: true) let activityVC = UIActivityViewController(activityItems: [fileLocation], applicationActivities: nil) presentViewController(activityVC, animated: true, completion: nil)
此代码将联系人转换为其NSData表示forms,将其保存到应用程序的缓存文件夹,并将该文件作为活动项共享。
如果要共享多个联系人,可以通过这种方式传递多个文件。
编辑:Swift 3.0版本
let contact: CNContact = /* your contact here */ let fileManager = FileManager.default let cacheDirectory = try! fileManager.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true) let fileLocation = cacheDirectory.appendingPathComponent("\(CNContactFormatter().string(from: contact)!).vcf") let contactData = try! CNContactVCardSerialization.data(with: [contact]) do { try contactData.write(to: fileLocation.asURL(), options: .atomicWrite) } catch { ... } let activityVC = UIActivityViewController(activityItems: [fileLocation], applicationActivities: nil) present(activityVC, animated: true, completion: nil)
Swift 3.0:
let cacheDirectory = try! FileManager.default.url(for: FileManager.SearchPathDirectory.cachesDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: true) let fileLocation = cacheDirectory.appendingPathComponent("\(CNContactFormatter().string(from: contact)!).vcf") let contactData = try! CNContactVCardSerialization.data(with: [contact]) do { _ = try contactData.write(to: fileLocation, options: .atomic) } catch let error { print(error) }
这也可以使用旧的ABAddressBook框架以类似的方式轻松实现(制作vCard表示,将其保存到文件,共享URL):
func AB_RecordVcardFileURL(record: ABRecord) -> NSURL? { let docsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) guard let pathStr = docsPath.first as NSString? else {return nil} let name = ABRecordCopyCompositeName(record).takeRetainedValue() ?? "vcard" let filePath = pathStr.stringByAppendingPathComponent("\(name!).vcf") let fileUrl = NSURL(fileURLWithPath: filePath) let card = ABPersonCreateVCardRepresentationWithPeople([record]).takeRetainedValue() as NSData card.writeToURL(fileUrl, atomically: true) return fileUrl }
您只需将URL传递给活动控制器:
guard let fileUrl = AB_RecordVcardFileURL(record) else {return} let activity = UIActivityViewController(activityItems: [fileUrl], applicationActivities: nil) presentViewController(activity, animated: true, completion: nil)