SlideShare uma empresa Scribd logo
1 de 24
Baixar para ler offline
AddressBook.swi, 
Alexsander*Akers*,*@a2
Address&Book&“Classes” 
ABAddressBookRef 
An#address#book. 
ABRecordRef 
A"person,"group,"or"source. 
ABMultiValueRef 
A"mul&'valued"property.
Why$AB_____Ref? 
When%AddressBook.framework%debuted%in%Mac%OS%X% 
10.2%Jaguar,%it%featured%both%CBbased%Core% 
FoundaDon%and%ObjecDveBC%APIs.%However,%when% 
the%iPhone%SDK%became%public%with%the%release%of% 
iPhone%OS%2.0,%its%AddressBook.framework%only% 
featured%the%CBbased%Core%FoundaDon%API. 
tl;dr 
C"based(Core(Founda.on(APIs(aren't(difficult(to(use,( 
per(se,(but(Swi<(makes(it(a(bit(harder.
ABAddressBookRef 
An#address#book.
ABAddressBookRef+Crea.on 
Objec&ve(C 
extern ABAddressBookRef ABAddressBookCreateWithOptions( 
CFDictionaryRef options, 
CFErrorRef *error 
); 
Swi$ 
func ABAddressBookCreateWithOptions( 
options: CFDictionaryRef!, 
error: UnsafeMutablePointer<Unmanaged<CFErrorRef>?> 
) -> Unmanaged<ABAddressBook>!
ABAddressBookRef+Crea.on 
var error: Unmanaged<CFErrorRef>? = nil 
var addressBook: ABAddressBookRef? = 
ABAddressBookCreateWithOptions(nil, &error)?.takeRetainedValue() 
if let addressBook = addressBook { 
// Success! 
} else { 
// Error 
let e = error!.takeUnretainedValue() as AnyObject as NSError 
println("An error occurred: (e)") 
}
ABAddressBookRef+Authoriza2on 
let addressBook: ABAddressBookRef = /* ... */ 
ABAddressBookRequestAccessWithCompletion(addressBook) { 
(success, error) in 
if success { 
// We can use the address book. 
} else { 
// User denied the request. 
let e = error as AnyObject as NSError 
println("An error occurred: (e)") 
} 
}
ABRecordRef 
A"person,"group,"or"source.
ABRecordRef 
Person
ABRecordRef 
Group
ABRecordRef 
Source
ABRecordRef*Acquisi/on 
let peoplePickerDelegate = /* some object */ 
let peoplePicker = ABPeoplePickerNavigationController() 
peoplePicker.delegate = peoplePickerDelegate 
viewController.presentViewController( 
peoplePicker, 
animated: true, 
completion: nil 
)
ABRecordRef*Acquisi/on 
// In your ABPeoplePickerNavigationControllerDelegate 
func peoplePickerNavigationController( 
peoplePicker: ABPeoplePickerNavigationController!, 
didSelectPerson person: ABRecord!) { 
// Do something with person 
} 
func peoplePickerNavigationControllerDidCancel( 
peoplePicker: ABPeoplePickerNavigationController!) { 
let p = peoplePicker.presentingViewController! 
p.dismissViewControllerAnimated(true, completion: nil) 
}
ABRecordRef*Acquisi/on 
let addressBook: ABAddressBookRef = /* ... */ 
// You can get a record's ID with ABRecordGetID() 
let recordID: ABRecordID = /* ... */ 
let unmanagedPerson: Unmanaged<ABRecordRef>? = 
ABAddressBookGetPersonWithRecordID(addressBook, recordID) 
// ABAddressBookGetGroupWithRecordID 
// ABAddressBookGetSourceWithRecordID 
if let person = unmanagedPerson?.takeUnretainedValue() { 
// You have a person! 
}
ABRecordRef*Values 
func ABRecordCopyValue( 
record: ABRecord!, 
property: ABPropertyID 
) -> Unmanaged<AnyObject>! 
let kABPersonFirstNameProperty: ABPropertyID 
let kABPersonMiddleNameProperty: ABPropertyID 
let kABPersonLastNameProperty: ABPropertyID 
// etc.
ABRecordRef*Values 
let firstName: CFStringRef = ABRecordCopyValue( 
record, 
kABPersonFirstNameProperty 
)?.takeRetainedValue() 
if firstName != nil { 
let fn = firstName! as AnyObject as String 
println("The first name is (fn).") 
}
ABRecordRef*Values 
let phones: ABMultiValueRef? = ABRecordCopyValue( 
record, 
kABPersonPhoneProperty 
)?.takeRetainedValue() 
if let phones = phones { 
// We have an ABMultiValueRef. 
}
ABMul&ValueRef 
A"mul&'valued"property.
ABMul&ValueRef 
Almost'a'dic,onary?'! 
// Duplicate keys are bad. 
let phones: [String : String] = [ 
"Home": "+1 (718) 861-4986", 
"Work": "+1 (212) 340-1938", 
"iPhone": "+1 (917) 530-9127", 
"iPhone": "+1 (917) 251-8826" 
] 
Keys%in%a%mul,-value%don't%have%to%be%unique.%A% 
person%could%have%two%mobile%numbers,%like%the%day% 
iPhone%/%night%iPhone%guy.
ABMul&ValueRef 
An#array#of#tuples.#! 
let phones: [(String, String)] = [ 
("Home", "+1 (718) 861-4986"), 
("Work", "+1 (212) 340-1938"), 
("iPhone", "+1 (917) 530-9127"), 
("iPhone", "+1 (917) 251-8826") 
] 
This%representa,on%makes%more%sense%because%the% 
uniqueness%contstraint%is%gone.%The%tuple%consists%of%a% 
label%and%a%value.
ABMul&ValueRef 
let count: Int = ABMultiValueGetCount(phones) 
for i in 0..<count { 
let label: String = ABMultiValueCopyLabelAtIndex( 
phones, i 
)!.takeRetainedValue() as AnyObject as String 
let value: String = ABMultiValueCopyValueAtIndex( 
phones, i 
)!.takeRetainedValue() as AnyObject as String 
println("(label) -> (value)") 
}
ABMul&ValueRef 
Debugger'Output 
_$!<Home>!$_ -> +1 (718) 861-4986 
_$!<Work>!$_ -> +1 (212) 340-1938 
iPhone -> +1 (917) 530-9127 
iPhone -> +1 (917) 251-8826
_$!<WTF>!$_ 
_$!<Home>!$_ !!→!!!kABHomeLabel 
_$!<Work>!$_ !!→!!!kABWorkLabel 
_$!<Other>!$_!!→!!!kABOtherLabel 
These%are%just%some%of%the%constants%used%by% 
AddressBook.framework%that%are%localized%in%Phone,% 
Contacts%and%ABPersonViewController%before% 
display%to%the%user.
AddressBook.swi, 
Alexsander*Akers*,*@a2

Mais conteúdo relacionado

Mais procurados

Awash in a sea of connections
Awash in a sea of connectionsAwash in a sea of connections
Awash in a sea of connections
Galen Charlton
 
Evolving Software with Moose
Evolving Software with MooseEvolving Software with Moose
Evolving Software with Moose
Dave Cross
 
Intro To Moose
Intro To MooseIntro To Moose
Intro To Moose
cPanel
 

Mais procurados (20)

Fewd week6 slides
Fewd week6 slidesFewd week6 slides
Fewd week6 slides
 
Intro to Ember.js
Intro to Ember.jsIntro to Ember.js
Intro to Ember.js
 
(DEV305) Building Apps with the AWS SDK for PHP | AWS re:Invent 2014
(DEV305) Building Apps with the AWS SDK for PHP | AWS re:Invent 2014(DEV305) Building Apps with the AWS SDK for PHP | AWS re:Invent 2014
(DEV305) Building Apps with the AWS SDK for PHP | AWS re:Invent 2014
 
Putting the Cat in the Catalogue: A Feline-Inspired OPAC Theme For Koha
Putting the Cat in the Catalogue: A Feline-Inspired OPAC Theme For KohaPutting the Cat in the Catalogue: A Feline-Inspired OPAC Theme For Koha
Putting the Cat in the Catalogue: A Feline-Inspired OPAC Theme For Koha
 
【AWS Developers Meetup】RESTful APIをChaliceで紐解く
【AWS Developers Meetup】RESTful APIをChaliceで紐解く【AWS Developers Meetup】RESTful APIをChaliceで紐解く
【AWS Developers Meetup】RESTful APIをChaliceで紐解く
 
Awash in a sea of connections
Awash in a sea of connectionsAwash in a sea of connections
Awash in a sea of connections
 
Moose
MooseMoose
Moose
 
(DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014
(DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014(DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014
(DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014
 
Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6Ethiopian multiplication in Perl6
Ethiopian multiplication in Perl6
 
Puppet: What _not_ to do
Puppet: What _not_ to doPuppet: What _not_ to do
Puppet: What _not_ to do
 
Evolving Software with Moose
Evolving Software with MooseEvolving Software with Moose
Evolving Software with Moose
 
Drupal, meet Assetic
Drupal, meet AsseticDrupal, meet Assetic
Drupal, meet Assetic
 
Scala Workshop
Scala WorkshopScala Workshop
Scala Workshop
 
Deep Dive: AWS Command Line Interface
Deep Dive: AWS Command Line InterfaceDeep Dive: AWS Command Line Interface
Deep Dive: AWS Command Line Interface
 
Symfony Under the Hood
Symfony Under the HoodSymfony Under the Hood
Symfony Under the Hood
 
Переход на Scala: босиком по граблям
Переход на Scala: босиком по граблямПереход на Scala: босиком по граблям
Переход на Scala: босиком по граблям
 
Deep Dive: AWS Command Line Interface
Deep Dive: AWS Command Line InterfaceDeep Dive: AWS Command Line Interface
Deep Dive: AWS Command Line Interface
 
Intro To Moose
Intro To MooseIntro To Moose
Intro To Moose
 
Rails 3.1 Asset Pipeline
Rails 3.1 Asset PipelineRails 3.1 Asset Pipeline
Rails 3.1 Asset Pipeline
 
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
Puppet Camp DC 2015: Stop Writing Puppet Modules: A Guide to Best Practices i...
 

Semelhante a AddressBook.swift

JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
Stoyan Stefanov
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
Seri Moth
 

Semelhante a AddressBook.swift (20)

Reasons To Love Ruby
Reasons To Love RubyReasons To Love Ruby
Reasons To Love Ruby
 
Rack Middleware
Rack MiddlewareRack Middleware
Rack Middleware
 
from Ruby to Objective-C
from Ruby to Objective-Cfrom Ruby to Objective-C
from Ruby to Objective-C
 
JSON and the APInauts
JSON and the APInautsJSON and the APInauts
JSON and the APInauts
 
A Lifecycle Of Code Under Test by Robert Fornal
A Lifecycle Of Code Under Test by Robert FornalA Lifecycle Of Code Under Test by Robert Fornal
A Lifecycle Of Code Under Test by Robert Fornal
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Attributes Unwrapped: Lessons under the surface of active record
Attributes Unwrapped: Lessons under the surface of active recordAttributes Unwrapped: Lessons under the surface of active record
Attributes Unwrapped: Lessons under the surface of active record
 
Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02Jsphp 110312161301-phpapp02
Jsphp 110312161301-phpapp02
 
Hidden treasures of Ruby
Hidden treasures of RubyHidden treasures of Ruby
Hidden treasures of Ruby
 
Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01
 
Ruby gems
Ruby gemsRuby gems
Ruby gems
 
Refactor like a boss
Refactor like a bossRefactor like a boss
Refactor like a boss
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
Ruby 2.0
Ruby 2.0Ruby 2.0
Ruby 2.0
 
Rails workshop for Java people (September 2015)
Rails workshop for Java people (September 2015)Rails workshop for Java people (September 2015)
Rails workshop for Java people (September 2015)
 
jRuby: The best of both worlds
jRuby: The best of both worldsjRuby: The best of both worlds
jRuby: The best of both worlds
 
Class
ClassClass
Class
 
Modern Perl
Modern PerlModern Perl
Modern Perl
 
PHP code examples
PHP code examplesPHP code examples
PHP code examples
 
Ruby
RubyRuby
Ruby
 

Último

Último (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

AddressBook.swift

  • 2. Address&Book&“Classes” ABAddressBookRef An#address#book. ABRecordRef A"person,"group,"or"source. ABMultiValueRef A"mul&'valued"property.
  • 3. Why$AB_____Ref? When%AddressBook.framework%debuted%in%Mac%OS%X% 10.2%Jaguar,%it%featured%both%CBbased%Core% FoundaDon%and%ObjecDveBC%APIs.%However,%when% the%iPhone%SDK%became%public%with%the%release%of% iPhone%OS%2.0,%its%AddressBook.framework%only% featured%the%CBbased%Core%FoundaDon%API. tl;dr C"based(Core(Founda.on(APIs(aren't(difficult(to(use,( per(se,(but(Swi<(makes(it(a(bit(harder.
  • 5. ABAddressBookRef+Crea.on Objec&ve(C extern ABAddressBookRef ABAddressBookCreateWithOptions( CFDictionaryRef options, CFErrorRef *error ); Swi$ func ABAddressBookCreateWithOptions( options: CFDictionaryRef!, error: UnsafeMutablePointer<Unmanaged<CFErrorRef>?> ) -> Unmanaged<ABAddressBook>!
  • 6. ABAddressBookRef+Crea.on var error: Unmanaged<CFErrorRef>? = nil var addressBook: ABAddressBookRef? = ABAddressBookCreateWithOptions(nil, &error)?.takeRetainedValue() if let addressBook = addressBook { // Success! } else { // Error let e = error!.takeUnretainedValue() as AnyObject as NSError println("An error occurred: (e)") }
  • 7. ABAddressBookRef+Authoriza2on let addressBook: ABAddressBookRef = /* ... */ ABAddressBookRequestAccessWithCompletion(addressBook) { (success, error) in if success { // We can use the address book. } else { // User denied the request. let e = error as AnyObject as NSError println("An error occurred: (e)") } }
  • 12. ABRecordRef*Acquisi/on let peoplePickerDelegate = /* some object */ let peoplePicker = ABPeoplePickerNavigationController() peoplePicker.delegate = peoplePickerDelegate viewController.presentViewController( peoplePicker, animated: true, completion: nil )
  • 13. ABRecordRef*Acquisi/on // In your ABPeoplePickerNavigationControllerDelegate func peoplePickerNavigationController( peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecord!) { // Do something with person } func peoplePickerNavigationControllerDidCancel( peoplePicker: ABPeoplePickerNavigationController!) { let p = peoplePicker.presentingViewController! p.dismissViewControllerAnimated(true, completion: nil) }
  • 14. ABRecordRef*Acquisi/on let addressBook: ABAddressBookRef = /* ... */ // You can get a record's ID with ABRecordGetID() let recordID: ABRecordID = /* ... */ let unmanagedPerson: Unmanaged<ABRecordRef>? = ABAddressBookGetPersonWithRecordID(addressBook, recordID) // ABAddressBookGetGroupWithRecordID // ABAddressBookGetSourceWithRecordID if let person = unmanagedPerson?.takeUnretainedValue() { // You have a person! }
  • 15. ABRecordRef*Values func ABRecordCopyValue( record: ABRecord!, property: ABPropertyID ) -> Unmanaged<AnyObject>! let kABPersonFirstNameProperty: ABPropertyID let kABPersonMiddleNameProperty: ABPropertyID let kABPersonLastNameProperty: ABPropertyID // etc.
  • 16. ABRecordRef*Values let firstName: CFStringRef = ABRecordCopyValue( record, kABPersonFirstNameProperty )?.takeRetainedValue() if firstName != nil { let fn = firstName! as AnyObject as String println("The first name is (fn).") }
  • 17. ABRecordRef*Values let phones: ABMultiValueRef? = ABRecordCopyValue( record, kABPersonPhoneProperty )?.takeRetainedValue() if let phones = phones { // We have an ABMultiValueRef. }
  • 19. ABMul&ValueRef Almost'a'dic,onary?'! // Duplicate keys are bad. let phones: [String : String] = [ "Home": "+1 (718) 861-4986", "Work": "+1 (212) 340-1938", "iPhone": "+1 (917) 530-9127", "iPhone": "+1 (917) 251-8826" ] Keys%in%a%mul,-value%don't%have%to%be%unique.%A% person%could%have%two%mobile%numbers,%like%the%day% iPhone%/%night%iPhone%guy.
  • 20. ABMul&ValueRef An#array#of#tuples.#! let phones: [(String, String)] = [ ("Home", "+1 (718) 861-4986"), ("Work", "+1 (212) 340-1938"), ("iPhone", "+1 (917) 530-9127"), ("iPhone", "+1 (917) 251-8826") ] This%representa,on%makes%more%sense%because%the% uniqueness%contstraint%is%gone.%The%tuple%consists%of%a% label%and%a%value.
  • 21. ABMul&ValueRef let count: Int = ABMultiValueGetCount(phones) for i in 0..<count { let label: String = ABMultiValueCopyLabelAtIndex( phones, i )!.takeRetainedValue() as AnyObject as String let value: String = ABMultiValueCopyValueAtIndex( phones, i )!.takeRetainedValue() as AnyObject as String println("(label) -> (value)") }
  • 22. ABMul&ValueRef Debugger'Output _$!<Home>!$_ -> +1 (718) 861-4986 _$!<Work>!$_ -> +1 (212) 340-1938 iPhone -> +1 (917) 530-9127 iPhone -> +1 (917) 251-8826
  • 23. _$!<WTF>!$_ _$!<Home>!$_ !!→!!!kABHomeLabel _$!<Work>!$_ !!→!!!kABWorkLabel _$!<Other>!$_!!→!!!kABOtherLabel These%are%just%some%of%the%constants%used%by% AddressBook.framework%that%are%localized%in%Phone,% Contacts%and%ABPersonViewController%before% display%to%the%user.