What's the library should I import for UTTypeImage, which is the replacement of kUTTypeImage in iOS 15?
It's a bit confusing. First, you'll need to import UniformTypeIdentifiers
. Then, replace kUTTypeImage
with UTType.image
(the Swift version of UTTypeImage
).
Path extension and MIME type of file in swift
Assuming you are receiving your data as NSData, follow this Post: https://stackoverflow.com/a/5042365/2798777
In Swift for example:
var c = [UInt32](count: 1, repeatedValue: 0)
(data as! NSData).getBytes(&c, length: 1)
switch (c[0]) {
case 0xFF, 0x89, 0x00:
println("image")
case 0x47:
println("gif")
default:
println("unknown: \(c[0])")
}
Getting extensions of a given UTType
UTTypeCopyPreferredTagWithClass
is used to convert a UTI to another tag, like file extensions:
NSString *extension = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(myUTI, kUTTagClassFilenameExtension);
Implement Document Picker in swift (iOS)
Update for iOS 14: You do not need any capabilities. Just create a UIDocumentPickerViewController
with the appropriate types, implement the delegate, and you are done.
More info in this answer. Code from there:
import UIKit
import MobileCoreServices
import UniformTypeIdentifiers
func selectFiles() {
let types = UTType.types(tag: "json",
tagClass: UTTagClass.filenameExtension,
conformingTo: nil)
let documentPickerController = UIDocumentPickerViewController(
forOpeningContentTypes: types)
documentPickerController.delegate = self
self.present(documentPickerController, animated: true, completion: nil)
}
From your project's capabilities, enable both the iCloud
and the Key-Sharing
.
Import MobileCoreServices
in your class and then extend the following three classes inside your UIViewController
:
UIDocumentMenuDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate
Implement the following functions:
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let myURL = urls.first else {
return
}
print("import result : \(myURL)")
}
public func documentMenu(_ documentMenu:UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("view was cancelled")
dismiss(animated: true, completion: nil)
}
How do you call all of this? Add the following bit of code to your click function:
func clickFunction(){
let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
Click your button. The following menu will pop up ..
In the case of Dropbox. Upon clicking on any item. You will be redirected back to your app and the URL will be logged in your terminal.
Manipulate the documentTypes to your need. In my app, Users permitted to Pdf only. So, suit yourself.
kUTTypePDF
kUTTypePNG
kUTTypeJPEG
...
Also if you feel like customizing your own menu bar. Add the following code and customize your own function inside the handler
importMenu.addOption(withTitle: "Create New Document", image: nil, order: .first, handler: { print("New Doc Requested") })
Enjoy it.
Related Topics
In Which Cases Arscnview.Raycastquery Returns Nil
How to Add an Uicollectionviewlayout Programmatically
Is It Safe to Force Unwrap an Optional After Checking It Is Not Nil
Swift Ui: Center Text into Circle
Swift Combine: What Are Those Multicast Functions for and How to Use Them
Best Way to Avoid Capturing a Copy of the Value in Closure
How to Respond with an Image Using Vapor
How to Access a Custom Function from Any File in the Same Swift Project
Video Upload to Amazon S3 in Swift
Sktexture Nearest Filtering Mode Doesn't Work (Making Pixel Art)
Swiftui View and Uihostingcontroller in Uiscrollview Breaks Scrolling
Swift: Convert String to Hex Color Code
Playing Hls (M3U8) in Cocoa Os X Avplayer - Swift
Swift 3 Type Inference Confusion