测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | Apache 2 |
发布最后发布 | 2017年10月 |
SwiftSwift 版本 | 3.2 |
SPM支持 SPM | ✗ |
由 Michal Zaborowski 维护。
SocialServiceBrowser 由 Michał Zaborowski 为 inspace.io 编写。
SocialServiceBrowser
提供了一种简单的方式来浏览、预览和导入 Dropbox、Amazon Drive、Google Drive 等外部服务的文件。它是用 Swift
为 iOS 编写的。
目前它仅实现了 Dropbox 的实现,并使用最新的 Dropbox API v2 和 SwiftyDropbox。
let configuration = SocialServiceBrowserConfigurator(client: SocialServiceBrowserDropboxClient(), selectionMode: .select)
let viewController = SocialServiceBrowserViewController(configuration: configuration, uiConfiguration: configuration)
viewController.delegate = self
present(UINavigationController(rootViewController: viewController), animated: true, completion: nil)
extension ViewController: UIDocumentInteractionControllerDelegate {
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self.presentedViewController!
}
}
extension ViewController: SocialServiceBrowserViewControllerDelegate {
fileprivate func showDocumentBrowser(for url: URL) {
let documentBrowser = UIDocumentInteractionController(url: url)
documentBrowser.delegate = self
documentBrowser.presentPreview(animated: true)
}
func socialServiceBrowser(_ socialServiceBrowser: SocialServiceBrowserViewController, didSelect node: SocialServiceBrowerNode) {
socialServiceBrowser.importNode(node) { [weak self] node, error in
if let node = node {
self?.showDocumentBrowser(for: node.fileURL)
}
}
}
}
SocialServiceBrowserViewController 使用两个需要实现 SocialServiceBrowserViewControllerConfigurable
和 SocialServiceBrowserViewControllerUIConfigurable
的对象进行配置。
public init(configuration: SocialServiceBrowserViewControllerConfigurable, uiConfiguration: SocialServiceBrowserViewControllerUIConfigurable) {
如果您想更改导航栏按钮样式或实现自定义 UICollectionViewCells
,应在您的类中实现 SocialServiceBrowserViewControllerUIConfigurable
,如下所示
public enum SocialServiceBrowserDisplayMode {
case list
case grid
}
public protocol SocialServiceBrowserViewControllerUIConfigurable {
var displayMode: SocialServiceBrowserDisplayMode { get }
var backBarButtonItem: UIBarButtonItem { get }
var closeBarButtonItem: UIBarButtonItem { get }
var importBarButtonItem: UIBarButtonItem { get }
func registerCells(for collectionView: UICollectionView)
func reusableIdentifierForCell(`in` displayMode: SocialServiceBrowserDisplayMode) -> String
func reusableIdentifierForHeader(`in` displayMode: SocialServiceBrowserDisplayMode) -> String
}
默认情况下,浏览器支持两种布局:网格布局和列表布局,您可以通过从实现 SocialServiceBrowserViewControllerUIConfigurable
的对象返回 displayMode
属性来设置它。
要能够实现自定义浏览器客户端,您只需要实现 SocialServiceBrowserClient
协议的方法。
public protocol SocialServiceBrowserClient {
var serviceName: String { get }
var filter: SocialServiceBrowserFilterType { get }
func requestRootNode(with completion: @escaping (SocialServiceBrowserResult<SocialServiceBrowerNodeListResponse, Error>) -> Void) -> SocialServiceBrowserOperationPerformable?
func requestChildren(`for` node: SocialServiceBrowerNode, withCompletion completion: @escaping (SocialServiceBrowserResult<SocialServiceBrowerNodeListResponse, Error>) -> Void) -> SocialServiceBrowserOperationPerformable?
func requestThumbnail(`for` node: SocialServiceBrowerNode, withCompletion: @escaping (SocialServiceBrowserResult<UIImage, Error>) -> Void) -> SocialServiceBrowserOperationPerformable?
func requestData(`for` node: SocialServiceBrowerNode, withCompletion: @escaping (SocialServiceBrowserResult<URL, Error>) -> Void) -> SocialServiceBrowserOperationPerformable?
}
例如,您可以查看 SocialServiceBrowserDropboxClient
类。
func requestRootNode(with completion: @escaping (SocialServiceBrowserResult<SocialServiceBrowerNodeListResponse, Error>) -> Void) -> SocialServiceBrowserOperationPerformable? {
return client?.files.listFolder(path: "").response(completionHandler: { [weak self] response, error in
if let error = error {
completion(SocialServiceBrowserResult.failure(NSError(domain: "SocialServiceBrowserDropboxClientDomain", code: -1, userInfo: [NSLocalizedDescriptionKey: error.description] as [String: Any])))
} else if let response = response {
response.filter = self?.filter
completion(SocialServiceBrowserResult.success(response))
} else {
fatalError()
}
}).request
}
将以下内容添加到您的 Podfile
并运行 $ pod install
。
pod 'SocialServiceBrowser'
如果您还没有安装 CocoaPods,您可以在 这里 学习如何安装。
Copyright © 2017 Inspace Labs Sp z o. o. Spółka Komandytowa. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this library except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.```