-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.swift
More file actions
53 lines (46 loc) · 1.72 KB
/
main.swift
File metadata and controls
53 lines (46 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// main.swift
// Contacts
//
// Created by Chad Pytel on 10/25/14.
// Copyright (c) 2014 Chad Pytel. All rights reserved.
//
import Foundation
import AddressBook
let addressbook = ABAddressBook()
let people = addressbook.people() as! [ABPerson]
let group = dispatch_group_create()
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
for person in people {
if person.imageData() == nil {
if let emails = person.emails() {
print("Finding Gravatar for \(person.firstName()) \(person.lastName())...")
for i in 0..<emails.count() {
let email = Email(address: emails.valueAtIndex(i) as! String)
var urlSession : NSURLSession!
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
urlSession = NSURLSession(configuration: configuration)
var task = urlSession.dataTaskWithURL(email.gravatarURL()!) {
data, response, error in
let statusCode = (response as? NSHTTPURLResponse)?.statusCode ?? -1
if statusCode == 200 {
print("Found valid Gravatar for \(email.address)")
person.setImageData(data)
}
dispatch_group_leave(group)
}
dispatch_group_enter(group)
task.resume()
}
}
}
}
dispatch_group_notify(group, queue) {
sleep(5) // allow for setImageData to complete
print("All task completed.")
exit(0)
}
while true {
sleep(1)
}