Swift 3: fix issue Type "className" does not conform to protocol 'UIDocumentPickerDelegate' - swift

I'm migrating my code from Swift 2 to Swift 3 but my code throws this error: Type "className" does not conform to protocol 'UIDocumentPickerDelegate'. I've migrate many parts of my document as you can see but error stills,
extension className: UIDocumentMenuDelegate {
//Opening the Document Menu
func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
print ("documentMenu")
self.present(documentPicker, animated: true, completion: nil)
}
func documentMenuWasCancelled(_ documentMenu: UIDocumentMenuViewController) {
}
}
//Using UIDocumentPickerDelegate, error persists here.
extension className: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
if(controller.documentPickerMode == UIDocumentPickerMode.import){
print ("success")
}
}
}
class className: BaseViewController{
...Tons of code here...
#IBAction func importKeyButtonPressed(_ sender: UIButton) {
let importMenu = UIDocumentMenuViewController(documentTypes: ["public.data","public.text","public.content"], in: UIDocumentPickerMode.import)
var documentPicker = UIDocumentPickerViewController(documentTypes: ["public.txt"], in: UIDocumentPickerMode.import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = UIModalPresentationStyle.fullScreen
self.present(documentPicker, animated: true, completion: nil)
}
How can I solve it?, I've use all methods that procotol requires. Thanks

I revised your information on this link that has a similar problem.
Cannot conform to STPAddCardViewControllerDelegate since Xcode 8 GM on Swift 3
The problem is the swift compiler than can´t recognize automatically some information. So in this case:
didPickDocumentAt url: URL
Then i follow the problem to this link:
https://bugs.swift.org/browse/SR-2596
In this link the information is that Swift 3 mistake data types, so i research some more and get to this page. The mistaken type in this case is "URL".
Then the solution is in the same page. I write it bellow:
weak var delegate : UIDocumentPickerDelegate?
#available(iOS 8.0, *)
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: Foundation.URL ){
//
print("something")
}
To that matter i reproduce the error in my computer, it is caused when diferent imported libraries has the same definition for data types, in this case URL type, which Swift 3 doesnt recognize automatically neither tells correctly the error. So it has to be defined directly.

Related

How can I use a UIKit ViewController inside Flutter Plugin code?

I'm trying to use UIDocumentPickerViewController in the same way as this documentation. When I try to use the example code in the plugin, I get the following errors:
flutter_download_manager_darwin/ios/Classes/SwiftFlutterDownloadManagerDarwinPlugin.swift:16:15: No exact matches in call to initializer
flutter_download_manager_darwin/ios/Classes/SwiftFlutterDownloadManagerDarwinPlugin.swift:16:15: Found candidate with type '([URL]) -> UIDocumentPickerViewController'
flutter_download_manager_darwin/ios/Classes/SwiftFlutterDownloadManagerDarwinPlugin.swift:16:15: Found candidate with type '([URL]) -> UIDocumentPickerViewController'
flutter_download_manager_darwin/ios/Classes/SwiftFlutterDownloadManagerDarwinPlugin.swift:16:72: Type 'Array<URL>.ArrayLiteralElement' (aka 'URL') has no member 'folder'
flutter_download_manager_darwin/ios/Classes/SwiftFlutterDownloadManagerDarwinPlugin.swift:21:41: Cannot find 'startingDirectory' in scope
flutter_download_manager_darwin/ios/Classes/SwiftFlutterDownloadManagerDarwinPlugin.swift:25:11: Cannot find 'present' in scope
Here is the exact code:
import Flutter
import UIKit
public class SwiftFlutterDownloadManagerDarwinPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "flutter_download_manager_darwin", binaryMessenger: registrar.messenger())
let instance = SwiftFlutterDownloadManagerDarwinPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: #escaping FlutterResult) {
switch call.method {
case "pickDirectoryUrl":
// Create a document picker for directories.
let documentPicker =
UIDocumentPickerViewController(forOpeningContentTypes: [.folder])
documentPicker.delegate = self
// Set the initial directory.
documentPicker.directoryURL = startingDirectory
// Present the document picker.
present(documentPicker, animated: true, completion: nil)
default:
result(FlutterMethodNotImplemented)
}
}
}
init(forOpeningContentTypes:) is supported in iOS 14.0+, while most likely the minimum iOS version that your app supports is lower.

Why am I receiving "Cannot find 'performSegueWithIdentifier' in scope" in Swift?

I am writing my application in Swift. Here is the viewDidLoad() function:
override func viewDidLoad() {
super.viewDidLoad()
if !self.check_file() {
print("Missing file to read")
performSegueWithIdentifier("missingfileViewController", sender: nil)
}
}
Here is check_file() function:
func check_file() -> Bool {
let fileManager = FileManager.default
return fileManager.fileExists(atPath:"/Applications/Readfile.app/Contents/Resources/textfile/readthis.txt")
}
I'm receiving this error Cannot find 'performSegueWithIdentifier' in scope.
Can anyone help me with this? Great thanks
EDIT: I have even tried to use self.performSegueWithIdentifier, but another error comes up: Value of type 'ViewController' has no member 'performSegueWithIdentifier'
self.performSegue(withIdentifier: "missingfileViewController", sender: self)
try this

Swift Document Picker, can not reach the file

I am writing a project which involves picking a file and getting the content of the file. However, I think it is not reaching the correct url of the file.
Here is the function where it calls the document picker. It is activated by a button.
#IBAction func selectFile(_ sender: Any) {
let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypePlainText as String], in: .import)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
present(documentPicker, animated: true, completion: nil)
}
And here is the extension to UIDocumentPickerViewController
extension ViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
print(urls)
do {
let fileContent = try String(contentsOf: urls[0], encoding: .utf8)
print(fileContent)
} catch {
return
}
}
}
In the console output, fileContent is not being printed out, instead, here is what it prints out.
Failed to associate thumbnails for picked URL
file:///Users/<user>/Library/Developer/CoreSimulator/Devices/480A2D02-810F-435E-BF44-4B2F6FC614A9/data/Containers/Data/Application/BAA825D0-C4D8-4B33-AFB3-3737ADCA0B29/Documents/test2.txt with the Inbox copy
file:///Users/<user>/Library/Developer/CoreSimulator/Devices/480A2D02-810F-435E-BF44-4B2F6FC614A9/data/Containers/Data/Application/BAA825D0-C4D8-4B33-AFB3-3737ADCA0B29/tmp/<project>-Inbox/test2.txt:
Error Domain=QLThumbnailErrorDomain Code=102 "(null)"
UserInfo={NSUnderlyingError=0x600003348060
{Error Domain=GSLibraryErrorDomain Code=3 "Generation not found" UserInfo={NSDescription=Generation not found}}}
There aren't many resources about this online, can someone help look at what I did wrong here?
Actually I made a small change on the UIDocumentPickerViewController initialiser. Adding the updated code below.
#IBAction func selectFile(_ sender: Any) {
let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text"], in: .import)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
present(documentPicker, animated: true, completion: nil) }
Rest of the code same, only replaced this ["public.text"] instead of [kUTTypePlainText as String].
May be it resolves your problem. Please let me know the feedback.
Credit gose to adrian check this link using-uidocumentpickerviewcontroller-to-import-text-in-swift
I know this is an old question, but if it is of help to anyone, I fixed the console issue by changing the instance type of UIDocumentPickerViewController, entering .open instead of .import
In my case, the behaviour does not change and I can choose a file without losing the expected behaviour.

PKAddPaymentPassViewControllerDelegate methods implementations

We have to add debit/credit cards from App to Apple Wallet
let config = PKAddPaymentPassRequestConfiguration.init(encryptionScheme: PKEncryptionScheme.ECC_V2)
config?.cardholderName = "John"
config?.primaryAccountSuffix = "9999" //last 4 or 5digits of card
config?.localizedDescription = "This will add the card to Apple Pay";
config?.primaryAccountIdentifier = "test";
config?.paymentNetwork = PKPaymentNetwork(rawValue: "VISA");
guard let addPaymentPassVC = PKAddPaymentPassViewController.init(requestConfiguration: config!, delegate: self) else { return }
self.present(addPaymentPassVC, animated: true, completion: nil)
extension ViewController: PKAddPaymentPassViewControllerDelegate {
func addPaymentPassViewController(_ controller: PKAddPaymentPassViewController,
generateRequestWithCertificateChain certificates: [Data],
nonce: Data,
nonceSignature: Data,
completionHandler handler: #escaping (PKAddPaymentPassRequest) -> Void) {
}
func addPaymentPassViewController(_ controller: PKAddPaymentPassViewController,
didFinishAdding pass: PKPaymentPass?,
error: Error?) {
print("didFinishAdding")
}
}
What should be implementation for PKAddPaymentPassViewControllerDelegate methods?
I think that first, you should check PassKit documentation from Apple to understand correctly how it works(https://developer.apple.com/wallet/). Then maybe this website could works for you: https://github.com/tschoffelen/php-pkpass
But you could not do anything until you speak directly with apple to give you their permission.

DropBox Chooser and documentsPicker for Swift developers 3.0

While the Chooser implementations for iOS are present Here. It is however limited to Objective-C. Is it possible to create a chooser in swift manually?
(A dropBox chooser)
I am also unable to sufficiently call the documentspicker functions, where one can pull any document from any app the user may have installed.
Thank you in advance
⭐Solved
From your project's capabilites. First enable both the iCloud serivces and the key Sharing, now import MobileCoreServices in your class. Finally extended the following three classes inside your class.
UIDocumentMenuDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate
Now implement the following functions..
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let myURL = url as URL
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 to call all of this? Add the following bit of code to your clickable 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 to your app. And the Url will be printed.
Manipulate the documentTypes to your need. In my app, Users permitted to Pdf only. So, suit yourself.
kUTTypePDF
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.