1 //返回按钮事件 2 @IBAction func backButtonClick() 3 { 4 self.navigationController?.popViewControllerAnimated(true) 5 } 6 7 //新增联系人 8 @IBAction func addPeople () 9 { 10 //取得电话薄句柄 11 var error:Unmanaged<CFError>? 12 var addressBook: ABAddressBookRef? = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue() 13 14 var newContact:ABRecordRef! = ABPersonCreate().takeRetainedValue() 15 var success:Bool = false 16 17 //昵称 18 success = ABRecordSetValue(newContact, kABPersonNicknameProperty, "Gaven", &error) 19 println("设置昵称是否成功: \(success)") 20 21 //设置姓 22 success = ABRecordSetValue(newContact, kABPersonFirstNameProperty, "zhang", &error) 23 println("设置姓是否成功: \(success)") 24 25 //设置名 26 success = ABRecordSetValue(newContact, kABPersonLastNameProperty, "wu", &error) 27 println("设置名是否成功:\(success)") 28 29 //添加电话 30 let tmpMutableMultiPhones:ABMutableMultiValue = ABMultiValueCreateMutable( 31 ABPropertyType(kABStringPropertyType)).takeRetainedValue() 32 33 success = ABMultiValueAddValueAndLabel(tmpMutableMultiPhones, "15901234000", "companyPhone", nil); 34 println("设置电话标题是否成功:\(success)") 35 36 success = ABRecordSetValue(newContact, kABPersonPhoneProperty, tmpMutableMultiPhones, nil) 37 println("设置电话是否成功:\(success)") 38 39 //添加邮箱 40 let addr:ABMutableMultiValue = ABMultiValueCreateMutable( 41 ABPropertyType(kABStringPropertyType)).takeRetainedValue() 42 ABMultiValueAddValueAndLabel(addr, "[email protected]", "companyEmail", nil) 43 println("设置邮箱标题是否成功:\(success)") 44 45 ABRecordSetValue(newContact, kABPersonEmailProperty, addr, nil) 46 println("设置邮箱是否成功:\(success)") 47 48 //保存记录 49 success = ABAddressBookAddRecord(addressBook, newContact, &error) 50 println("保存记录是否成功:? \(success)") 51 52 //保存数据库 53 success = ABAddressBookSave(addressBook, &error) 54 println("修改数据库是否成功: \(success)") 55 } 56 57 //删除联系人 58 @IBAction func deletePeople() 59 { 60 //定义错误标记 61 var error:Unmanaged<CFError>? 62 63 //取得电话薄句柄 64 var addressBook: ABAddressBookRef? = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue() 65 66 //读取通讯录 67 let sysAddressBookStatus = ABAddressBookGetAuthorizationStatus() 68 69 if sysAddressBookStatus == .Denied || sysAddressBookStatus == .NotDetermined { 70 // Need to ask for authorization 71 var authorizedSingal:dispatch_semaphore_t = dispatch_semaphore_create(0) 72 var askAuthorization:ABAddressBookRequestAccessCompletionHandler = { success, error in 73 if success { 74 ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray 75 dispatch_semaphore_signal(authorizedSingal) 76 } 77 } 78 ABAddressBookRequestAccessWithCompletion(addressBook, askAuthorization) 79 dispatch_semaphore_wait(authorizedSingal, DISPATCH_TIME_FOREVER) 80 } 81 82 var sysContacts:NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray 83 84 85 //遍历通讯录 86 for contact in sysContacts { 87 var currentContact:Dictionary = [String:String]() 88 89 // 姓 90 // var firstName = ABRecordCopyValue(contact, kABPersonFirstNameProperty)?.takeRetainedValue() as String? ?? "" 91 92 93 //2015年5月2号修改 94 var firstName = ABRecordCopyValue(contact, kABPersonFirstNameProperty)?.takeRetainedValue() as! String? ?? "" 95 96 97 println("姓:\(firstName)") 98 99 // 名 100 // var lastName = ABRecordCopyValue(contact, kABPersonLastNameProperty)?.takeRetainedValue() as String? ?? "" 101 102 //2015年5月2号修改 103 var lastName = ABRecordCopyValue(contact, kABPersonLastNameProperty)?.takeRetainedValue() as! String? ?? "" 104 105 106 println("名:\(lastName)") 107 108 var success:Bool = false 109 110 if firstName == "zhang" && lastName == "wu" 111 { 112 success = ABAddressBookRemoveRecord(addressBook, contact, nil); 113 println("删除是否成功: \(success)") 114 } 115 116 //保存数据库 117 success = ABAddressBookSave(addressBook, &error) 118 println("修改数据库是否成功: \(success)") 119 } 120 } 121 122 //代码获取地址薄 123 @IBAction func codeToAddressButtonClick() 124 { 125 //定义错误标记 126 var error:Unmanaged<CFError>? 127 128 //取得电话薄句柄 129 var addressBook: ABAddressBookRef? = ABAddressBookCreateWithOptions(nil, &error).takeRetainedValue() 130 131 //发出授权信息,界面会弹出提示窗口 132 let sysAddressBookStatus = ABAddressBookGetAuthorizationStatus() 133 134 //获取电话薄 135 if sysAddressBookStatus == .Denied || sysAddressBookStatus == .NotDetermined { 136 // Need to ask for authorization 137 var authorizedSingal:dispatch_semaphore_t = dispatch_semaphore_create(0) 138 139 var askAuthorization:ABAddressBookRequestAccessCompletionHandler = { success, error in 140 if success { 141 //获取的联系人信息数组 142 var sysContacts:NSArray = ABAddressBookCopyArrayOfAllPeople(addressBook).takeRetainedValue() as NSArray 143 144 145 //循环读取联系人 146 for contact in sysContacts { 147 var currentContact:Dictionary = [String:String]() 148 149 150 /* 151 部分单值属性 152 */ 153 // 姓 154 // var firstName = ABRecordCopyValue(contact, kABPersonFirstNameProperty)?.takeRetainedValue() as String? ?? "" 155 156 //2015年5月2号修改 157 var firstName = ABRecordCopyValue(contact, kABPersonFirstNameProperty)?.takeRetainedValue() as! String? ?? "" 158 println("姓:\(firstName)") 159 160 161 // 名 162 // var lastName = ABRecordCopyValue(contact, kABPersonLastNameProperty)?.takeRetainedValue() as String? ?? "" 163 164 //2015年5月2号修改 165 var lastName = ABRecordCopyValue(contact, kABPersonLastNameProperty)?.takeRetainedValue() as! String? ?? "" 166 println("名:\(lastName)") 167 168 169 // 昵称 170 // var nikename = ABRecordCopyValue(contact, kABPersonNicknameProperty)?.takeRetainedValue() as String? ?? "" 171 172 //2015年5月2号修改 173 var nikename = ABRecordCopyValue(contact, kABPersonNicknameProperty)?.takeRetainedValue() as! String? ?? "" 174 175 println("昵称:\(nikename)") 176 177 178 // 公司(组织) 179 // var organization = ABRecordCopyValue(contact, kABPersonOrganizationProperty)?.takeRetainedValue() as String? ?? "" 180 181 //2015年5月2号修改 182 var organization = ABRecordCopyValue(contact, kABPersonOrganizationProperty)?.takeRetainedValue() as! String? ?? "" 183 println("公司(组织):\(organization)") 184 185 186 // 职位 187 // var jobTitle = ABRecordCopyValue(contact, kABPersonJobTitleProperty)?.takeRetainedValue() as String? ?? "" 188 189 //2015年5月2号修改 190 var jobTitle = ABRecordCopyValue(contact, kABPersonJobTitleProperty)?.takeRetainedValue() as! String? ?? "" 191 192 println("职位:\(jobTitle)") 193 194 195 // 部门 196 // var department = ABRecordCopyValue(contact, kABPersonDepartmentProperty)?.takeRetainedValue() as String? ?? "" 197 198 //2015年5月2号修改 199 var department = ABRecordCopyValue(contact, kABPersonDepartmentProperty)?.takeRetainedValue() as! String? ?? "" 200 201 println("部门:\(department)") 202 203 204 205 //备注 206 // var note = ABRecordCopyValue(contact, kABPersonNoteProperty)?.takeRetainedValue() as String? ?? "" 207 208 //2015年5月2号修改 209 var note = ABRecordCopyValue(contact, kABPersonNoteProperty)?.takeRetainedValue() as! String? ?? "" 210 211 println("备注:\(note)") 212 213 214 215 /* 216 部分多值属性 217 */ 218 // 电话 219 var propertyValues:ABMultiValueRef? = ABRecordCopyValue(contact, kABPersonPhoneProperty)?.takeRetainedValue() 220 221 if propertyValues != nil { 222 223 for i in 0 ..< ABMultiValueGetCount(propertyValues) 224 { 225 var label:String = ABMultiValueCopyLabelAtIndex(propertyValues, i).takeRetainedValue() as String 226 227 var value = ABMultiValueCopyValueAtIndex(propertyValues, i) 228 var phone = value.takeRetainedValue() as? String ?? "" 229 230 println("\(label):\(phone)"); 231 } 232 } 233 234 // E-mail 235 var emailValues:ABMultiValueRef? = ABRecordCopyValue(contact, kABPersonEmailProperty)?.takeRetainedValue() 236 237 if emailValues != nil { 238 239 for i in 0 ..< ABMultiValueGetCount(emailValues) 240 { 241 var label:String = ABMultiValueCopyLabelAtIndex(emailValues, i).takeRetainedValue() as String 242 243 var value = ABMultiValueCopyValueAtIndex(emailValues, i) 244 var email = value.takeRetainedValue() as? String ?? "" 245 246 println("\(label):\(email)"); 247 } 248 } 249 250 // 地址 251 var addressValues:ABMultiValueRef? = ABRecordCopyValue(contact, kABPersonAddressProperty)?.takeRetainedValue() 252 253 if addressValues != nil { 254 255 for i in 0 ..< ABMultiValueGetCount(addressValues) 256 { 257 var label:String = ABMultiValueCopyLabelAtIndex(addressValues, i).takeRetainedValue() as String 258 259 260 // var value = ABMultiValueCopyValueAtIndex(addressValues, i) 261 // var addrNSDict:NSMutableDictionary = value.takeRetainedValue() as NSMutableDictionary 262 // 263 // var _Country = addrNSDict.valueForKey(kABPersonAddressCountryKey) as? String ?? "" 264 // var _State = addrNSDict.valueForKey(kABPersonAddressStateKey) as? String ?? "" 265 // var _City = addrNSDict.valueForKey(kABPersonAddressCityKey) as? String ?? "" 266 // var _Street = addrNSDict.valueForKey(kABPersonAddressStreetKey) as? String ?? "" 267 // var _Contrycode = addrNSDict.valueForKey(kABPersonAddressCountryCodeKey) as? String ?? "" 268 269 270 271 //2015年5月2号修改 272 var value = ABMultiValueCopyValueAtIndex(addressValues, i) 273 var addrNSDict:NSMutableDictionary = value.takeRetainedValue() as! NSMutableDictionary 274 275 276 var _Country = addrNSDict.valueForKey(kABPersonAddressCountryKey as String) as? String ?? "" 277 var _State = addrNSDict.valueForKey(kABPersonAddressStateKey as String) as? String ?? "" 278 var _City = addrNSDict.valueForKey(kABPersonAddressCityKey as String) as? String ?? "" 279 var _Street = addrNSDict.valueForKey(kABPersonAddressStreetKey as String) as? String ?? "" 280 var _Contrycode = addrNSDict.valueForKey(kABPersonAddressCountryCodeKey as String) as? String ?? "" 281 282 283 //或 284 // var _Country = "" 285 // var _State = "" 286 // var _City = "" 287 // var _Street = "" 288 // var _Contrycode = "" 289 // 290 // if let value:String = addrNSDict.valueForKey(kABPersonAddressCountryKey as String) as? String 291 // { 292 // _Country = value 293 // } 294 // 295 // if let value:String = addrNSDict.valueForKey(kABPersonAddressStateKey as String) as? String 296 // { 297 // _State = value 298 // } 299 // 300 // if let value:String = addrNSDict.valueForKey(kABPersonAddressCityKey as String) as? String 301 // { 302 // _City = value 303 // } 304 // 305 // if let value:String = addrNSDict.valueForKey(kABPersonAddressStreetKey as String) as? String 306 // { 307 // _Street = value 308 // } 309 // 310 // if let value:String = addrNSDict.valueForKey(kABPersonAddressCountryCodeKey as String) as? String 311 // { 312 // _Contrycode = value 313 // } 314 315 316 317 println("\(label)==_Country:\(_Country) _State:\(_State) _City:\(_City) _Street:\(_Street) _Contrycode:\(_Contrycode)"); 318 } 319 } 320 321 322 // 纪念日 323 var dateValues:ABMultiValueRef? = ABRecordCopyValue(contact, kABPersonDateProperty)?.takeRetainedValue() 324 325 if dateValues != nil { 326 327 for i in 0 ..< ABMultiValueGetCount(dateValues) 328 { 329 var label:String = ABMultiValueCopyLabelAtIndex(dateValues, i).takeRetainedValue() as String 330 331 var value = ABMultiValueCopyValueAtIndex(dateValues, i) 332 var date = (value.takeRetainedValue() as? NSDate)?.description 333 334 println("\(label):\(date)"); 335 } 336 } 337 338 339 // URL 340 var urlValues:ABMultiValueRef? = ABRecordCopyValue(contact, kABPersonURLProperty)?.takeRetainedValue() 341 342 if urlValues != nil { 343 344 for i in 0 ..< ABMultiValueGetCount(urlValues) 345 { 346 var label:String = ABMultiValueCopyLabelAtIndex(urlValues, i).takeRetainedValue() as String 347 348 var value = ABMultiValueCopyValueAtIndex(urlValues, i) 349 var url = value.takeRetainedValue() as? String ?? "" 350 351 println("\(label):\(url)"); 352 } 353 } 354 355 356 // SNS 357 var snsValues:ABMultiValueRef? = ABRecordCopyValue(contact, kABPersonSocialProfileProperty)?.takeRetainedValue() 358 359 if snsValues != nil { 360 361 for i in 0 ..< ABMultiValueGetCount(snsValues) 362 { 363 var label:String = ABMultiValueCopyLabelAtIndex(snsValues, i).takeRetainedValue() as String 364 365 var value = ABMultiValueCopyValueAtIndex(snsValues, i) 366 // var snsNSDict:NSMutableDictionary = value.takeRetainedValue() as NSMutableDictionary 367 // var _Username = snsNSDict.valueForKey(kABPersonSocialProfileUsernameKey) as? String ?? "" 368 // var _URL = snsNSDict.valueForKey(kABPersonSocialProfileURLKey) as? String ?? "" 369 // var _Serves = snsNSDict.valueForKey(kABPersonSocialProfileServiceKey) as? String ?? "" 370 371 //2015年5月2后修改 372 var snsNSDict:NSMutableDictionary = value.takeRetainedValue() as! NSMutableDictionary 373 374 var _Username = snsNSDict.valueForKey(kABPersonSocialProfileUsernameKey as String) as? String ?? "" 375 var _URL = snsNSDict.valueForKey(kABPersonSocialProfileURLKey as String) as? String ?? "" 376 var _Serves = snsNSDict.valueForKey(kABPersonSocialProfileServiceKey as String) as? String ?? "" 377 378 println("\(label)==_Username:\(_Username) _URL:\(_URL) _Serves:\(_Serves)"); 379 } 380 } 381 382 383 // IM 384 var imValues:ABMultiValueRef? = ABRecordCopyValue(contact, kABPersonInstantMessageProperty)?.takeRetainedValue() 385 386 if imValues != nil { 387 388 for i in 0 ..< ABMultiValueGetCount(imValues) 389 { 390 var label:String = ABMultiValueCopyLabelAtIndex(imValues, i).takeRetainedValue() as String 391 392 var value = ABMultiValueCopyValueAtIndex(imValues, i) 393 // var imNSDict:NSMutableDictionary = value.takeRetainedValue() as NSMutableDictionary 394 // var _Serves = imNSDict.valueForKey(kABPersonInstantMessageServiceKey) as? String ?? "" 395 // var _Username = imNSDict.valueForKey(kABPersonInstantMessageUsernameKey) as? String ?? "" 396 397 398 399 //2015年5月2后修改 400 var imNSDict:NSMutableDictionary = value.takeRetainedValue() as! NSMutableDictionary 401 402 var _Serves = imNSDict.valueForKey(kABPersonInstantMessageServiceKey as String) as? String ?? "" 403 var _Username = imNSDict.valueForKey(kABPersonInstantMessageUsernameKey as String) as? String ?? "" 404 405 println("\(label)==_Serves:\(_Serves) _Username:\(_Username)"); 406 } 407 } 408 } 409 410 dispatch_semaphore_signal(authorizedSingal) 411 } 412 } 413 414 ABAddressBookRequestAccessWithCompletion(addressBook, askAuthorization) 415 dispatch_semaphore_wait(authorizedSingal, DISPATCH_TIME_FOREVER) 416 } 417 } 418 419 420 //UI打开地址薄 421 @IBAction func uiToAddressButtonClick() 422 { 423 var picker = ABPeoplePickerNavigationController() 424 picker.peoplePickerDelegate = self 425 self.presentViewController(picker, animated: true, completion: { () -> Void in 426 427 }) 428 } 429 430 431 // MARK: - ABPeoplePickerNavigationControllerDelegate 432 func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!) 433 { 434 // 姓 435 // var firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as String? ?? "" 436 437 //2015年5月2号修改 438 var firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as! String? ?? "" 439 440 println("选中人的姓:\(firstName)") 441 442 443 // 名 444 // var lastName = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as String? ?? "" 445 446 //2015年5月2号修改 447 var lastName = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as! String? ?? "" 448 449 println("选中人的名:\(lastName)") 450 } 451 452 func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) 453 { 454 455 456 } 457 458 func peoplePickerNavigationControllerDidCancel(peoplePicker: ABPeoplePickerNavigationController!) 459 { 460 //取消 461 peoplePicker.dismissViewControllerAnimated(true, completion: { () -> Void in 462 463 }) 464 } 465 466 func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, shouldContinueAfterSelectingPerson person: ABRecord!) -> Bool 467 { 468 return true 469 } 470 471 func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!, shouldContinueAfterSelectingPerson person: ABRecord!, property: ABPropertyID, identifier: ABMultiValueIdentifier) -> Bool 472 { 473 474 return true 475 } 476 477 478 479 //发短信 480 // 添加MessageUI.framework框架 481 // 482 // 为ViewController类 483 // 添加UINavigationControllerDelegate, MFMessageComposeViewControllerDelegate 接口 484 // 实现func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) 485 @IBAction func sendSMSButtonClick() 486 { 487 //判断 488 if MFMessageComposeViewController.canSendText(){ 489 let controller = MFMessageComposeViewController() 490 491 // 设置短信内容 492 controller.body = "短信内容短信内容" 493 494 // 设置收件人列表 495 controller.recipients = ["15900001111,18900001111,17011112222"] 496 497 //设置代理 498 controller.messageComposeDelegate = self 499 500 //打开界面 501 self.presentViewController(controller, animated: true, completion: nil) 502 } else { 503 let alert = UIAlertView(title: "提示信息", message: "本设备不能发短信", delegate: nil, cancelButtonTitle: "确定") 504 alert.show() 505 } 506 } 507 508 //代理 509 func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) { 510 511 controller.dismissViewControllerAnimated(true, completion: nil) 512 513 switch result.value{ 514 case MessageComposeResultSent.value: 515 println("短信已发送") 516 case MessageComposeResultCancelled.value: 517 println("短信已取消") 518 case MessageComposeResultFailed.value: 519 println("短信发送失败") 520 default: 521 break 522 } 523 } 524 525 526 //打电话 527 @IBAction func phoneButtonClick() 528 { 529 //调用打电话功能--最简单最直接的方式:直接跳到拨号界面 530 UIApplication.sharedApplication().openURL(NSURL(string: "tel://10086")!) 531 532 } 533 534 535 536 // 该实例说明了如何使用MessageUI框架组成并在程序内部发送邮件和短信。 537 // 538 // 该应用使用MessageUI框架的MFMailComposeViewController类和MFMessageComposeViewController类,这两个类分别管理用户界面并充许用户在程序内部集成发送邮件和短信。 539 // 540 // 该实例显示两个按钮,一个为“发送邮件”,另一个为“发送短信”。 541 // 542 // 当用户单击“发送邮件”与“发送短信”时,应用分别显示集成的邮件集成界面与短信集成界面。 543 // 544 // 应用不但检查它们的集成界面是否存在,还检查设备是否已经配置为了可以发送邮件和短信。另处,它提供了反馈消息。 545 546 // MFMailComposeViewControllerDelegate 547 548 //发邮件 549 @IBAction func sendEmailButtonClick() 550 { 551 if MFMailComposeViewController.canSendMail() 552 { 553 var picker = MFMailComposeViewController() 554 picker.mailComposeDelegate = self 555 556 //设置主题 557 picker.setSubject("这是一个主题") 558 559 //设置发件人 560 picker.setToRecipients(["[email protected]","[email protected]","[email protected]"]) 561 562 //设置抄送人 563 picker.setCcRecipients(["[email protected]","[email protected]"]) 564 565 //设置密送人 566 picker.setBccRecipients(["[email protected]"]) 567 568 //添加图片附件 569 var path = NSBundle.mainBundle().pathForResource("SwiftClassWeiXin.png", ofType: "") 570 var myData = NSData(contentsOfFile: path!) 571 picker.addAttachmentData(myData, mimeType: "image/png", fileName: "swift.png") 572 573 //设置邮件正文内容-支持html 574 picker.setMessageBody("这是邮件正文", isHTML: false) 575 576 //打开界面 577 self.presentViewController(picker, animated: true, completion: nil) 578 } 579 else 580 { 581 let alert = UIAlertView(title: "提示信息", message: "本设备不能发邮件", delegate: nil, cancelButtonTitle: "确定") 582 alert.show() 583 } 584 } 585 586 587 //发送邮件代理方法 588 func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) 589 { 590 591 controller.dismissViewControllerAnimated(true, completion: nil) 592 593 switch result.value{ 594 case MFMailComposeResultCancelled.value: 595 println("邮件已取消") 596 break; 597 case MFMailComposeResultSaved.value: 598 println("邮件已保存") 599 break; 600 case MFMailComposeResultSent.value: 601 println("邮件已发送") 602 break; 603 case MFMailComposeResultFailed.value: 604 println("邮件发送失败") 605 break; 606 default: 607 println("邮件没有发送") 608 break; 609 } 610 } 611 612 613 614 615 616 617 618 override func viewDidLoad() { 619 super.viewDidLoad() 620 621 if titleString != nil && titleString != ""{ 622 titleLabel.text = titleString 623 }else{ 624 titleLabel.text = "第12章 地址薄、短信、邮件"; 625 } 626 627 628 // Do any additional setup after loading the view. 629 }