簡體   English   中英

iOS共享擴展名-loadItemForTypeIdentifier調用返回iOS11中的文件url和iOS12中的文件內容

[英]iOS Share extension - the loadItemForTypeIdentifier call returns file url in iOS11 and file content in iOS12

我在iOS 11和12上看到了不同的行為。

在iOS 11上-我獲得在完成處理程序中共享的文件的文件路徑。

在iOS 12上-我收到URL域錯誤。 但是,如果我根據類型(例如:UIImage)進行處理,那么我會得到文件內容。

這種行為僅在模擬器上還是在設備上?

每個iOS版本是否需要處理此問題?

是的,您還將在設備上同時獲得這兩種東西(文件路徑或數據)。 您無需在iOS版本上添加任何檢查。

請流我的代碼。 它很快,但您可以理解。

func share() {
let inputItem = extensionContext!.inputItems.first! as! NSExtensionItem
let attachment = inputItem.attachments!.first as! NSItemProvider
if attachment.hasItemConformingToTypeIdentifier( kUTTypeImage as String) {
    attachment.loadItem(forTypeIdentifier: kUTTypeImage as String, options: [:]) { (data, error) in
        var image: UIImage?
        if let someURl = data as? URL {
            image = UIImage(contentsOfFile: someURl.path)
        }else if let someImage = data as? UIImage {
            image = someImage
        }

        if let someImage = image {
            guard let compressedImagePath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?.appendingPathComponent("shareImage.jpg", isDirectory: false) else {
                return
            }

            let compressedImageData = UIImageJPEGRepresentation(someImage, 1)
            guard (try? compressedImageData?.write(to: compressedImagePath)) != nil else {
                return
            }

        }else{
            print("bad share data")
        }
    }

}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM