How to setup a Bar Button with a value of firebase? - swift

I want to check if the user is a admin or not. If the user is a admin, I want to show a bar button. If not, the bar button should be hidden.
I call the following code in viewDidLoad:
#IBOutlet weak var beitraegeMelden: UIBarButtonItem!
var admin = false
func setupBarButton() {
observeAdmin()
if admin == true {
self.beitraegeMelden.isEnabled = true
self.navigationItem.rightBarButtonItem = self.beitraegeMelden
} else {
self.beitraegeMelden.isEnabled = false
self.navigationItem.rightBarButtonItem = nil
}
}
func observeAdmin() {
guard let currentUserUid = UserApi.shared.CURRENT_USER_ID else { return }
let REF_ADMIN = Database.database().reference().child("users").child(currentUserUid).child("admin")
REF_ADMIN.observeSingleEvent(of: .value) { (admin) in
let adminRecht = admin.value as? Bool
if adminRecht == true {
self.admin = true
} else {
self.admin = false
}
}
}
Here my database structure of the actually logged in user:
users
currentUid
admin: true
The admin value never gets true.
Thanks in advance for your help!

You need a completion as the call to firebase is asynchronous
func observeAdmin(completion:#escaping((Bool) -> () )) {
guard let currentUserUid = UserApi.shared.CURRENT_USER_ID else { return }
let REF_ADMIN = Database.database().reference().child("users").child(currentUserUid).child("admin")
REF_ADMIN.observeSingleEvent(of: .value) { (admin) in
completion( (admin.value as? Bool) ?? false )
}
}
Call
observeAdmin { (res) in
self.beitraegeMelden.isEnabled = res
self.navigationItem.rightBarButtonItem = res ? self.beitraegeMelden : nil
}

Related

use a switch to change the value of boolean saved core data value

In my swift code below right now the code saves 2 boolean values to a core data boolean value. The two values are true false. I would like the user to turn on the switch and have the 2nd value be true as well. So it would be true true. I am trying to do that in func alternate() but I dont know how to exactly effect a specific value. Looking for any kind of help.
import UIKit;import CoreData
class ViewController: UIViewController {
var lbl = UILabel()
var sw = UISwitch()
var checkmarkButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
[ lbl,sw,checkmarkButton].forEach {
view.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
$0.layer.borderWidth = 1
}
helpBool.shareInstance.saveBoolean(true)
helpBool.shareInstance.saveBoolean(false)
getBool(imageNo: 1)
}
func getBool(imageNo:Int) {
// first check the array bounds
let info = helpBool.shareInstance.fetchBool()
if info.count > imageNo {
// if info[imageNo].bool {
if info[imageNo].bool == true {
checkmarkButton.setImage(UIImage(named:"unnamed"), for: .normal);
}
if info[imageNo].bool == false {
checkmarkButton.setImage(nil, for: .normal);
}
// }
}
}
#objc func alternate(){
//fetch alternate
getBool(imageNo: 1)
if sw.isOn = true {
helpBool.shareInstance.saveBoolean()
}
else {
helpBool.shareInstance.saveBoolean()
}
}
}
class helpBool: UIViewController{
static let shareInstance = helpBool()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
func saveBoolean(_ boolean: Bool) {
let imageInstance = OnOff(context: context)
imageInstance.bool = boolean
do {
try context.save()
print("text is saved")
} catch {
print(error.localizedDescription)
}
}
func fetchBool() -> [OnOff] {
var fetchingImage = [OnOff]()
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "OnOff")
do {
fetchingImage = try context.fetch(fetchRequest) as! [OnOff]
} catch {
print("Error while fetching the image")
}
return fetchingImage
}
}

Youtube WkWebView blank space issue

At first, YouTube videos run well. However, when the app enters the background thread, an empty screen appears. Even if the tableview is refreshed, the video screen does not return to normal, and the app must be restarted.
class HomeTableViewCell: UITableViewCell {
var disposeBag = DisposeBag()
var data = PublishRelay<ViewPost>()
var isLoaded = false
func bind() {
data.asDriver() { _ in .never() }
.drive(onNext: { [weak self] currentPost in
guard let self = self else { return }
if self.isLoaded == true {
self.videoContainerView.cueVideo(byId: currentPost.url, startSeconds: 0, suggestedQuality: .default)
}
else {
self.videoContainerView.load(withVideoId: currentPost.url)
self.isLoaded = true
}
......
} )
.disposed(by: disposeBag)
let videoContainerView: WKYTPlayerView = {
let videoContainerView = WKYTPlayerView()
videoContainerView.translatesAutoresizingMaskIntoConstraints = false
videoContainerView.clipsToBounds = true
videoContainerView.layer.cornerRadius = 15
return videoContainerView
}()
}

Firestore pagination using MVVM architecture swift

I don't quite understand what I am doing wrong since I am very new to MVVM. It worked in MVC architecture. I've setup my VM and am able to get the first set of results and even then that's not working properly. I get 4 results instead of 10 which is what LOADLIMIT is set as. I was able to get it to work in an MVC architecture without any issues. The VM function which triggers the query is called multiple (3) times instead of just once i.e. even prior to scrolling.
Here is my VM:
enum FetchRestaurant {
case success
case error
case location
case end
}
class ListViewModel {
let restaurant: [Restaurant]?
let db = Firestore.firestore()
var restaurantArray = [Restaurant]()
var lastDocument: DocumentSnapshot?
var currentLocation: CLLocation?
typealias fetchRestaurantCallback = (_ restaurants: [Restaurant]?, _ message: String?, _ status: FetchRestaurant) -> Void
var restaurantFetched: fetchRestaurantCallback?
var fetchRestaurant: FetchRestaurant?
init(restaurant: [Restaurant]) {
self.restaurant = restaurant
}
func fetchRestaurantCallback (callback: #escaping fetchRestaurantCallback) {
self.restaurantFetched = callback
}
func fetchRestaurants(address: String) {
print("address received: \(address)")
getLocation(from: address) { location in
if let location = location {
self.currentLocation = location
self.queryGenerator(at: location)
} else {
self.restaurantFetched?(nil, nil, .location)
}
}
}
func queryGenerator(at location: CLLocation) {
var query: Query!
if restaurantArray.isEmpty {
query = db.collection("Restaurant_Data").whereField("distributionType", isLessThanOrEqualTo: 2).limit(to: Constants.Mealplan.LOADLIMIT)
} else {
print("last document:\(String(describing: lastDocument?.documentID))")
query = db.collection("Restaurant_Data").whereField("distributionType", isLessThanOrEqualTo: 2).start(afterDocument: lastDocument!).limit(to: Constants.Mealplan.LOADLIMIT)
}
batchFetch(query: query)
}
func batchFetch(query: Query) {
query.getDocuments { (querySnapshot, error) in
if let error = error {
self.restaurantFetched?(nil, error.localizedDescription, .error)
} else if querySnapshot!.isEmpty {
self.restaurantFetched?(nil, nil, .end)
} else if !querySnapshot!.isEmpty {
let queriedRestaurants = querySnapshot?.documents.compactMap { querySnapshot -> Restaurant? in
return try? querySnapshot.data(as: Restaurant.self)
}
guard let restaurants = queriedRestaurants,
let currentLocation = self.currentLocation else {
self.restaurantFetched?(nil, nil, .end)
return }
self.restaurantArray.append(contentsOf: self.applicableRestaurants(allQueriedRestaurants: restaurants, location: currentLocation))
DispatchQueue.main.asyncAfter(deadline: .now(), execute: {
self.restaurantFetched?(self.restaurantArray, nil, .success)
})
self.lastDocument = querySnapshot!.documents.last
}
}
}
func getLocation(from address: String, completionHandler: #escaping (_ location: CLLocation?) -> Void) {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { (placemarks, error) in
guard let placemarks = placemarks,
let location = placemarks.first?.location else {
completionHandler(nil)
return
}
completionHandler(location)
}
}
}
And in the VC viewDidLoad:
var fetchMore = false
var reachedEnd = false
let leadingScreensForBatching: CGFloat = 5.0
var searchController = UISearchController(searchResultsController: nil)
var currentAddress : String?
var listViewModel = ListViewModel(restaurant: [Restaurant]())
override func viewDidLoad() {
super.viewDidLoad()
listViewModel.fetchRestaurantCallback { (restaurants, error, result) in
switch result {
case .success :
self.loadingShimmer.stopShimmering()
self.loadingShimmer.removeFromSuperview()
guard let fetchedRestaurants = restaurants else { return }
self.restaurantArray.append(contentsOf: fetchedRestaurants)
self.tableView.reloadData()
self.fetchMore = false
case .location :
self.showAlert(alertTitle: "No businesses nearby", message: "Try going back and changing the address")
case .error :
guard let error = error else { return }
self.showAlert(alertTitle: "Error", message: error)
case .end :
self.fetchMore = false
self.reachedEnd = true
}
}
if let currentAddress = currentAddress {
listViewModel.fetchRestaurants(address: currentAddress)
}
}
I would really appreciate links or resources for implementing MVVM in Swift for a Firestore back-end. I'm coming up short on searches here and on Google. Even tried medium.
EDIT
class ListViewController: UITableViewController {
lazy var loadingShimmer: UIImageView = {
let image = UIImage(named: "shimmer_background")
let imageview = UIImageView(image: image)
imageview.contentMode = .top
imageview.translatesAutoresizingMaskIntoConstraints = false
return imageview
}()
var restaurantArray = [Restaurant]()
var planDictionary = [String: Any]()
var fetchMore = false
var reachedEnd = false
let leadingScreensForBatching: CGFloat = 5.0
var searchController = UISearchController(searchResultsController: nil)
var currentAddress : String?
var listViewModel = ListViewModel(restaurant: [Restaurant]())
override func viewDidLoad() {
super.viewDidLoad()
setupTable()
}
override func viewWillAppear(_ animated: Bool) {
clearsSelectionOnViewWillAppear = false
}
func setupTable() {
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Restaurant", style: .plain, target: nil, action: nil)
tableView.register(RestaurantCell.self, forCellReuseIdentifier: "Cell")
tableView.delegate = self
tableView.dataSource = self
let navigationBarHeight: CGFloat = self.navigationController!.navigationBar.frame.height
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: -navigationBarHeight, right: 0)
tableView.separatorStyle = .none
tableView.showsVerticalScrollIndicator = false
tableView.addSubview(loadingShimmer)
loadingShimmer.topAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.topAnchor).isActive = true
loadingShimmer.leadingAnchor.constraint(equalTo: tableView.leadingAnchor).isActive = true
loadingShimmer.trailingAnchor.constraint(equalTo: tableView.trailingAnchor).isActive = true
loadingShimmer.startShimmering()
initialSetup()
}
func initialSetup() {
let addressOne = planDictionary["addressOne"] as! String + ", "
let city = planDictionary["city"] as! String + ", "
let postalCode = planDictionary["postalCode"] as! String
currentAddress = addressOne + city + postalCode
setupSearch()
listViewModel.fetchRestaurantCallback { (restaurants, error, result) in
switch result {
case .success :
self.loadingShimmer.stopShimmering()
self.loadingShimmer.removeFromSuperview()
guard let fetchedRestaurants = restaurants else { return }
self.restaurantArray.append(contentsOf: fetchedRestaurants)
self.tableView.reloadData()
self.fetchMore = false
case .location :
self.showAlert(alertTitle: "No businesses nearby", message: "Try going back and changing the address")
case .error :
guard let error = error else { return }
self.showAlert(alertTitle: "Error", message: error)
case .end :
self.fetchMore = false
self.reachedEnd = true
}
}
if let currentAddress = currentAddress {
listViewModel.fetchRestaurants(address: currentAddress)
}
}
override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let off = scrollView.contentOffset.y
let off1 = scrollView.contentSize.height
if off > off1 - scrollView.frame.height * leadingScreensForBatching {
print("\(fetchMore), \(reachedEnd)")
if !fetchMore && !reachedEnd {
if let address = self.currentAddress {
print("address sent: \(address)")
listViewModel.fetchRestaurants(address: address)
}
}
}
}
}
That you're only getting back 4 results instead of 10 is not due to a faulty query or get-document request—those are coded properly. You're either losing documents when you parse them (some are failing Restaurant initialization), Constants.Mealplan.LOADLIMIT is wrong, or there aren't more than 4 documents in the collection itself that satisfy the query.
That the query is executed 3 times instead of once is also not due to anything in this code—viewDidLoad is only called once and geocodeAddressString only returns once. You're making a fetch request elsewhere that we can't see.
In the batchFetch method, you have a guard that returns out of the function without ever calling its completion handler. This will leave the UI in a state of limbo. I'd recommend always calling the completion handler no matter why the function returns.
You never manage the document cursor. If the get-document return has less documents than the load limit, then nil the last-document cursor. This way, when you attempt to get the next page of documents, guard against a nil cursor and see if there is even more to fetch.
There's no need to pass in an empty array and have your function fill it; simply construct and return an array of results within ListViewModel itself.
We can't see how you trigger pagination. Is it through a scroll delegate when the user reaches the bottom or through a button tap, for example? If it's through a scroll delegate, then I'd disable that for now and see how many returns you get—I suspect one, instead of 3.
What is the particular reason you've ditched MVC for MVVM here? With MVC, you can get pagination up with just a few lines of code. I think MVVM is overkill for iOS applications and would advise against using it unless you have a compelling reason.

Show indicator for two request

I have two requests. Each of them getting different data. I need to show an indicator when the all of two request is requesting. How i can do this?
this is my first request:
func productList(tableView:UITableView,spinner:UIActivityIndicatorView,index1:Int,index2:Int,index3:Int){
if product.count<=0{
alamoFireManager?.request(.GET, "http://mobile.unimax.kz/api/Default1",parameters: ["type1id":index1,"type2id":index2,"type3id":index3,"name":"","userid":1089])
.responseJSON { response in
guard response.result.error == nil else {
if let httpError = response.result.error {
switch(httpError.code){
case -1009:
let alert = UIAlertView(title: "Ошибка",message: "Нету интернета!!",delegate: nil,cancelButtonTitle: "OK")
alert.show()
break
default:
let alert = UIAlertView(title: "Ошибка",message: "Повторите попытку!!",delegate: nil,cancelButtonTitle: "OK")
alert.show()
break
}
} else { //no errors
let statusCode = (response.response?.statusCode)!
print(statusCode)
}
spinner.stopAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
return
}
if let value = response.result.value {
// handle the results as JSON, without a bunch of nested if loops
let product = JSON(value)
for (_,subJson):(String, JSON) in product {
let img:NSData
if let src=subJson["sphoto"].string{
if src.containsString("jpg"){
let range = src.startIndex.advancedBy(2)..<src.endIndex
let substring = src[range]
var urlString = "http://admin.unimax.kz/\(substring)"
urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
if let dataFromURL=NSData(contentsOfURL: NSURL(string: urlString)!){
img=dataFromURL
}
else{
img=NSData(contentsOfURL: NSURL(string: "http://zhaksy-adam.kz/Images/domalak.png")!)!
}
}
else{
img=NSData(contentsOfURL: NSURL(string: "http://zhaksy-adam.kz/Images/domalak.png")!)!
}
}
else{
img=NSData(contentsOfURL: NSURL(string: "http://zhaksy-adam.kz/Images/domalak.png")!)!
}
//Do something you want
let id=subJson["id"].int!
let name=subJson["name"].string!
let price=subJson["price"].int!
let description=subJson["description"].rawString()
self.product.append(Product(id:id,title: name, img: UIImage(data: img), price: price,desc:description!))
}
spinner.stopAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
tableView.reloadData()
}
}
}
else{
spinner.stopAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
tableView.reloadData()
}
}
and this is my second request:
func makeGetFav(userID:Int,completionHandler: (responseObject:JSON) -> ()) {
alamoFireManager?.request(.GET, "http://mobile.unimax.kz/api/Klientapi/?authid=\(userID)")
.responseJSON {response in
guard response.result.error == nil else {
if let httpError = response.result.error {
switch(httpError.code){
case -1009:
let alert = UIAlertView(title: "Ошибка",message: "Нету интернета!!",delegate: nil,cancelButtonTitle: "OK")
alert.show()
break
default:
let alert = UIAlertView(title: "Ошибка",message: "Повторите попытку!!",delegate: nil,cancelButtonTitle: "OK")
alert.show()
break
}
} else { //no errors
let statusCode = (response.response?.statusCode)!
print(statusCode)
}
return
}
completionHandler(responseObject: JSON(response.result.value!))
}
}
func getFavs(userID:Int,tableView:UITableView,spinner:UIActivityIndicatorView){
getFavRequets(userID){(responseObject) in
if responseObject != nil{
self.favs.removeAll()
self.localDB.clearFav()
for (_,subJson):(String, JSON) in responseObject {
self.favs.append(FavModel(id: subJson["id"].int!, title: subJson["name"].string!, price: subJson["price"].int!))
}
spinner.stopAnimating()
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
tableView.reloadData()
}
}
}
there are i call it all:
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
indicator.startAnimating()
localDB.getUserInfo()
getRequests.productList(tableView, spinner: indicator, index1: catalog1Index, index2: catalog2Index, index3: catalog3Index)
if localDB.user.count>0{
getRequests.getFavs(localDB.user[0].id, tableView: tableView, spinner: indicator)
}
localDB.checkCart(tableView, tabCtrl: tabBarController!)
You can control it using a singleton to start and stop it according to the number of running requests:
class NetworkActivityIndicator: NSObject {
static let sharedInstance = NetworkActivityIndicator()
private override init() {
}
var count = 0 {
didSet {
self.updateIndicator()
}
}
private func updateIndicator() {
if count > 0 {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
} else {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
}
}
The you simply call NetworkActivityIndicator.sharedInstance.count += 1 just before the request and NetworkActivityIndicator.sharedInstance.count += 1 when you get the response
the easiest way is to add two variables to your class that indicate whether the associated request is complete, and then to create a function that stops the spinner only if both the variables indicate the calls are complete.
if you want to use the class for more than one ViewController then I suggest adding a struct-enum combo to organise the variables that are indicating which requests are currently underway.
eg
class GetRequests {
var productsLoaded = false
var favoritesLoaded = false
func stopSpinnerIfNeeded(spinner: UIActivityIndicatorView) {
if productsLoaded && favoritesLoaded {
spinner.stopAnimating()
spinner.hidden = true
}
}
func productList(tableView:UITableView,spinner:UIActivityIndicatorView,index1:Int,index2:Int,index3:Int){
defer {
productsLoaded = true
stopSpinnerIfNeeded(spinner)
}
if product.count<=0{
alamoFireManager?.request(.GET, "http://mobile.unimax.kz/api/Default1",parameters: ["type1id":index1,"type2id":index2,"type3id":index3,"name":"","userid":1089])
.responseJSON { response in
guard response.result.error == nil else {
if let httpError = response.result.error {
switch(httpError.code){
case -1009:
let alert = UIAlertView(title: "Ошибка",message: "Нету интернета!!",delegate: nil,cancelButtonTitle: "OK")
alert.show()
break
default:
let alert = UIAlertView(title: "Ошибка",message: "Повторите попытку!!",delegate: nil,cancelButtonTitle: "OK")
alert.show()
break
}
} else { //no errors
let statusCode = (response.response?.statusCode)!
print(statusCode)
}
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
return
}
if let value = response.result.value {
// handle the results as JSON, without a bunch of nested if loops
let product = JSON(value)
for (_,subJson):(String, JSON) in product {
let img:NSData
if let src=subJson["sphoto"].string{
if src.containsString("jpg"){
let range = src.startIndex.advancedBy(2)..<src.endIndex
let substring = src[range]
var urlString = "http://admin.unimax.kz/\(substring)"
urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
if let dataFromURL=NSData(contentsOfURL: NSURL(string: urlString)!){
img=dataFromURL
}
else{
img=NSData(contentsOfURL: NSURL(string: "http://zhaksy-adam.kz/Images/domalak.png")!)!
}
}
else{
img=NSData(contentsOfURL: NSURL(string: "http://zhaksy-adam.kz/Images/domalak.png")!)!
}
}
else{
img=NSData(contentsOfURL: NSURL(string: "http://zhaksy-adam.kz/Images/domalak.png")!)!
}
//Do something you want
let id=subJson["id"].int!
let name=subJson["name"].string!
let price=subJson["price"].int!
let description=subJson["description"].rawString()
self.product.append(Product(id:id,title: name, img: UIImage(data: img), price: price,desc:description!))
}
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
tableView.reloadData()
}
}
}
else{
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
tableView.reloadData()
}
}
func getFavs(userID:Int,tableView:UITableView,spinner:UIActivityIndicatorView){
getFavRequets(userID){(responseObject) in
if responseObject != nil{
self.favs.removeAll()
self.localDB.clearFav()
for (_,subJson):(String, JSON) in responseObject {
self.favs.append(FavModel(id: subJson["id"].int!, title: subJson["name"].string!, price: subJson["price"].int!))
}
favoritesLoaded = true
stopSpinnerIfNeeded(spinner)
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
tableView.reloadData()
}
}
}
A very fast way to do it:
// global var
var isIndicatorActive : Bool = false
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
indicator.startAnimating()
self.isIndicatorActive = true
In the line before each alamoFireManager?.request you call :
if isIndicatorActive == false {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
indicator.startAnimating()
self.isIndicatorActive = true
}
And after each line spinner.stopAnimating() add:
self.isIndicatorActive = false
I suggest you to using single network activity indicator and keep a counter which keep track of network activity within the application. I know my answer is more in text, but looking at your code it seems you can implement the following.
0 indicates no activity indicator.
As soon as a new activity starts, increment the counter do a check if counter is greater than 0 then show the indicator.
Decrement the counter when the activity task finishes. On decrement keep a check if counter is 0 then set the indicator visibility to false.
PS: Don't forget to increment/decrement in sync blocks. You may use objc_sync_enter(..) and objc_sync_exit(..) methods for this.
Thx
thanks everybody for help. I solve it like this:
func makeReuest1(){
if localDB.user.count>0{
getRequests.getFavs(localDB.user[0].id)
}
makeRequest2()
}
func makeRequest2(){
getRequests.productList(tableView, spinner: indicator, index1: catalog1Index, index2: catalog2Index, index3: catalog3Index)
}

NSOpenPanel as sheet

Ive looked around at other answers, but nothing seems to be helping my case.
I have a viewController class which contains an IBAction for a button. This button should open a NSOpenPanel as a sheet from that viewController:
class ViewController: NSViewController {
#IBAction func folderSelection(sender: AnyObject) {
var myFiledialog: NSOpenPanel = NSOpenPanel()
myFiledialog.prompt = "Select path"
myFiledialog.worksWhenModal = true
myFiledialog.allowsMultipleSelection = false
myFiledialog.canChooseDirectories = true
myFiledialog.canChooseFiles = false
myFiledialog.resolvesAliases = true
//myFiledialog.runModal()
myFiledialog.beginSheetModalForWindow(self.view.window!, completionHandler: nil)
var chosenpath = myFiledialog.URL
if (chosenpath!= nil)
{
var TheFile = chosenpath!.absoluteString!
println(TheFile)
//do something with TheFile
}
else
{
println("nothing chosen")
}
}
}
The problem comes from myFileDialog.beginSheetModalForWindow(..) , it works with the line above, but that is not a sheet effect
You need to call beginSheetModalForWindow from your panel on your window, and use a completion block:
let myFiledialog = NSOpenPanel()
myFiledialog.prompt = "Select path"
myFiledialog.worksWhenModal = true
myFiledialog.allowsMultipleSelection = false
myFiledialog.canChooseDirectories = true
myFiledialog.canChooseFiles = false
myFiledialog.resolvesAliases = true
myFiledialog.beginSheetModalForWindow(window, completionHandler: { num in
if num == NSModalResponseOK {
let path = myFiledialog.URL
print(path)
} else {
print("nothing chosen")
}
})
Swift 5
let dialog = NSOpenPanel()
dialog.beginSheetModal(for: self.view.window!){ result in
if result == .OK, let url = dialog.url {
print("Got", url)
}
}