Kau*_*hna 5 zip swift ios8-share-extension swift3
我需要执行以下操作-
我有另一个应用程序,其中我将以zip格式导出用户config(.txt)和contact(.vcf)。
在第二个应用程序中,我有一个共享扩展名以获取导出的zip,在共享扩展名中,我需要提取zip文件并获取txt和vcf文件,然后将它们上传到解析服务器。
在共享扩展名中打开导出的zip之前,我已经做完了。但我无法提取压缩文件。我在互联网上找不到答案。
这是我的 ShareViewController
import UIKit
import Social
import Parse
import MobileCoreServices
import SSZipArchive
class ShareViewController: SLComposeServiceViewController {
var requird_data : NSData!
var path : URL!
override func viewDidLoad() {
super.viewDidLoad()
//Parse.setApplicationId("cGFyc2UtYXBwLXdob3N1cA==", clientKey: "")
initUI()
getURL()
textView.delegate = self
textView.keyboardType = .numberPad
}
// override func viewWillAppear(_ animated: Bool) {
// super.viewWillAppear(true)
//
// }
func initUI()
{
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
title = "upup"
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.backgroundColor = UIColor(red:0.97, green:0.44, blue:0.12, alpha:1.00)
placeholder = "Please enter your Phone number"
}
private func getURL() {
let extensionItem = extensionContext?.inputItems.first as! NSExtensionItem
let itemProvider = extensionItem.attachments?.first as! NSItemProvider
let zip_type = String(kUTTypeZipArchive)
if itemProvider.hasItemConformingToTypeIdentifier(zip_type) {
itemProvider.loadItem(forTypeIdentifier: zip_type, options: nil, completionHandler: { (item, error) -> Void in
guard let url = item as? NSURL else { return }
print("\(item.debugDescription)")
OperationQueue.main.addOperation {
self.path = url as URL
SSZipArchive.unzipFile(atPath: url.path!, toDestination: url.path!)
}
})
} else {
print("error")
}
}
override func isContentValid() -> Bool {
// Do validation of contentText and/or NSExtensionContext attachments here
return true
}
override func didSelectPost() {
// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
// Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}
override func configurationItems() -> [Any]! {
// To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
return []
}
override func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool
{
let length = ((textView.text)?.characters.count)! + text.characters.count - range.length
let allowedset : CharacterSet = CharacterSet(charactersIn: "0123456789+").inverted as CharacterSet
let filtered = (text.components(separatedBy: allowedset)).joined(separator: "")
return (length<17) && (text == filtered)
}
}
Run Code Online (Sandbox Code Playgroud)
我使用SSZipAchive提取文件。链接:https : //github.com/ZipArchive/ZipArchive
我在Xcode 9 beta 1中运行了该应用程序。我使用了Files模拟器中的新应用程序来共享zip。以下是我的扩展名信息列表
我是分享扩展程序的新手,所以我对此不太了解。上面的所有代码均来自以下教程中的点点滴滴和一些谷歌搜索。
1. https://www.appcoda.com/ios8-share-extension-swift/
2. https://hackernoon.com/how-to-build-an-ios-share-extension-in-swift-4a2019935b2e
请指导我。我用swift 3。
我找到了解决办法。我错误地将提取项目的目标文件路径指定为与源文件路径相同。将其更改为应用程序的文档目录后,我就可以使用它了。
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
SSZipArchive.unzipFile(atPath: url.path!, toDestination: documentsPath)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
335 次 |
| 最近记录: |