Skip to content

Instantly share code, notes, and snippets.

@wata
Last active March 9, 2020 14:51
Show Gist options
  • Save wata/149f4e3bbee7318bc437773cce902a7d to your computer and use it in GitHub Desktop.
Save wata/149f4e3bbee7318bc437773cce902a7d to your computer and use it in GitHub Desktop.
Batch uploader with UIActivityViewController
override func viewDidLoad() {
super.viewDidLoad()
let items: [[Any]] = [
["Foo", URL(string: "http://www.africau.edu/images/default/sample.pdf")!]
["Bar", URL(string: "http://www.africau.edu/images/default/sample.pdf")!]
]
presentActivityViewControllerRecursively(items: items) { (error, items) in
if let error = error, let items = items {
print("Failed to complete activity items: \(items)")
return
}
print("done")
}
}
private func presentActivityViewControllerRecursively(items: [[Any]], completion: @escaping (Error?, [Any]?) -> Void) {
var _items = items
let vc = UIActivityViewController(activityItems: _items.remove(at: 0), applicationActivities: nil)
vc.completionWithItemsHandler = { [weak self] (_, completed, items, error) in
guard completed else {
completion(error, items)
return
}
if _items.isEmpty {
completion(nil, nil)
return
}
self?.presentActivityViewControllerRecursively(items: _items, completion: completion)
}
present(vc, animated: true, completion: nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment