I have an application where I would like to connect to two different firebase-databases.
I have two viewControllers which are individually able to connect to each database, but if I connect to the first database, and afterwards connect to the other - the app Crashes
I have 2 info.plist files and 2 ViewControllers connected with a button/segue.
this is my first viewController
import UIKit
import Firebase
class ViewController: UIViewController {
var ref: DatabaseReference!
var handle:UInt!
override func viewDidLoad() {
super.viewDidLoad()
let filePath = Bundle.main.path(forResource: "GoogleService-Info-anden", ofType: "plist")
guard let fileopts = FirebaseOptions.init(contentsOfFile: filePath!)
else { assert(false, "Couldn't load config file") }
FirebaseApp.configure(options: fileopts)
ref = Database.database().reference()
ref?.child("UserVandreture").child("navn").setValue("Inger")
}
override func viewDidDisappear(_ animated: Bool) {
ref.removeObserver(withHandle: handle)
}
}
I can connect to firebase - which happens when I load the app.
Second viewController
import UIKit
import Firebase
class AndenDBViewController: UIViewController {
var ref: DatabaseReference!
var handle:UInt!
let fileName = "Bambusstien"
override func viewDidLoad() {
super.viewDidLoad()
let filePath = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist")
guard let fileopts = FirebaseOptions.init(contentsOfFile: filePath!)
else { assert(false, "Couldn't load config file") }
FirebaseApp.configure(options: fileopts)
ref = Database.database().reference()
response()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//kan læse navn,type, længde og link
func response() {
ref?.child("Vandreture").child(fileName).observe(.value, with: { (snapshot) in
let dict = snapshot.value as? [String: AnyObject]
let navn = dict!["navn"] as? String
print(navn as Any)
let type = dict!["type"] as? String
print(type as Any)
let Længde = dict!["length"] as? String
print(Længde as Any)
let link = dict!["link"] as? String
print(link as Any)
})
}
override func viewDidDisappear(_ animated: Bool) {
ref.removeObserver(withHandle: handle)
}
}
If I out-comment the database call in the previous viewController, this controller is also able to connect to firebase/database and read data, but if I don't outcomment this data, it will set the value in the first database, and when I press the button to go to the second viewController the app crashes.
I have set the targetMemebeship for the 2 plist-files to the same application-name.
The error message says that the app has already been configured - however I thought that ref.removeObserver(handle) in ViewDidDissapear() would disconnect the connection to the database, before connecting to the other database - so what is wrong??
Basically i needed protection of my data, but I had not populated the databases yet so..
The solution to my problem was to work with the rules-part - having just a single database.
Allowing users to have write-access to certain part of the database, and only having read-access to other parts - something like:
{
"rules": {
"myPersonal":{
".read": true,
".write": "auth != null"
},
"myPublic": {
".read": "auth != null",
".write": true
}
}
}
Related
i have a list of music at my firebase real time database and i am retriving them but i have 1000 musics data and i want to show loader when i reading data and stop loader when if there is a error(internet connection, or something else) or reading completed.
when i turn off the internet i couldn't get the data and can't stop loader to show error alert like there is no internet connection.
please help me how to handle that problem.
here is my code
didload function called from viewdidload()
private var musicArray = [ItemModal]() {
didSet {
view?.updateTableView()
}
}
func didLoad() {
view?.showLoader()
getAllMusics { ItemModal in
self.musicArray = ItemModal
self.view?.hideLoader()
}
}
func getAllMusics(completion: #escaping ([ItemModal]) -> Void) {
var musicArray = [ItemModal]()
ref.child("music").observeSingleEvent(of: .value) { snapshot in
let enumerator = snapshot.children
while let rest = enumerator.nextObject() as? DataSnapshot {
guard let data = try? JSONSerialization.data(withJSONObject: rest.value as Any, options: []) else { return }
if let itemModal = try? JSONDecoder().decode(ItemModal.self, from: data) {
musicArray.append(itemModal)
}
}
completion(musicArray)
}
}
You can use reachability function by using https://github.com/ashleymills/Reachability.swift. To get to notify when the internet is turned off, you can implement reachabilityChanged Notification. In the selector method of reachabilityChanged, you can hide the loader.
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged), name: .reachabilityChanged)
}
#objc func changed() {
if reachability?.isReachable {
//Continue success implementation
} else {
view?.hideLoder
//Implement Error handling
}
}
I have username in firestore, whenever person logs in my app, I want to read that data(username) in firestore immediately, but it needs some time, if the problem is in slow internet, what other possible ways would you suggest to get my end result?
as yo can see in the gif above, loading username takes a little bit of time, but it is still a problem, I want it to be already loaded
#IBOutlet weak var welcomeNameLabel: UILabel!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
readData()
}
func readData(){
self.db.collection("users").getDocuments{ (snapshot, err) in
if let err = err {
print("error happened \(err)")
}else {
if let userId = Auth.auth().currentUser?.uid {
if let currentUserDoc = snapshot?.documents.first(where: { ($0["uid"] as? String) == userId }) {
var welcomeName = currentUserDoc["username"] as! String
self.welcomeNameLabel.text = "Welcome, \(welcomeName)"
}
}
}
}
}
I got an error when I drag files to my macOS app,
[sandbox] Failed to get a sandbox extension,when i set App Sandboxvalue boolean no,it is ok,but i want put my app to appstore,I must set App Sandbox YES, how can I do?
class FYOpenDragFileView: NSView{
override func draggingEnded(_ sender: NSDraggingInfo) {
print("松手了")
setupWithActive(active: false)
}
override func draggingExited(_ sender: NSDraggingInfo?) {
isDraging = false
setupWithActive(active: false)
print("draggingExited 进去又出来")
}
override func updateDraggingItemsForDrag(_ sender: NSDraggingInfo?) {
guard let next = delegate else {
return;
}
next.fileDraging()
print("更新拖动文件")
}
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
guard let items = sender.draggingPasteboard.pasteboardItems else{
return false
}
var datas = [Data]()
for i in 0..<items.count{
let item = items[i] .string(forType: .fileURL)
if item != nil {
// this have an error
//[sandbox] Failed to get a sandbox extension
let da = try? Data(contentsOf: URL(string: item!)!)
guard let next = da else {
continue
}
datas.append(next)
}
}
QiniuUploadManger.uploadImage(nil, data: datas)
return true
}
}
With sandboxing, you'll have to set the appropriate "entitlements" for your app to receive drag-&-dropped files, which is probably why you're getting this error.
The entitlements file should already have been generated for you when you enabled Sandboxing. See this Apple documentation for details.
I need to do the following-
I have another app in which i will export the users config(.txt) and contacts(.vcf) in a zip format.
In the second app i have a share extension to get the exported zip and in the share extension, i need to extract the zip file and get both the txt and vcf files and then upload them to a parse server.
I have done till opening the exported zip in the share extension. but i could not get the zip extracted.
I couldn't get the answer in internet.
Here is my 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)
}
}
I use SSZipAchive to extract the file. Link : https://github.com/ZipArchive/ZipArchive
I ran the application in the Xcode 9 beta 1. I used the new Files app from simulator to share the zip.
Below is my Share Extensions Info.Plist
I am newbie to share extension so i don't know much about it. All the code above are from bits and pieces from the following tutorials and a little googling.
1.https://www.appcoda.com/ios8-share-extension-swift/
2.https://hackernoon.com/how-to-build-an-ios-share-extension-in-swift-4a2019935b2e
Please guide me.
I use swift 3.
I found out the solution. It was my mistake to give the destination file path for the extracted items to be the same as the source files path. After changing it to the app's documents directory i got it working.
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
SSZipArchive.unzipFile(atPath: url.path!, toDestination: documentsPath)
I'm following the instructions as shown in firebase but I'm still getting crashes even after making sure that the text entry is type String.
Here's the error:
Terminating app due to uncaught exception 'InvalidPathValidation', reason: '(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''
and here's the code:
import UIKit
import Firebase
import FirebaseDatabase
import FirebaseAuth
class BioEditViewController: UIViewController {
#IBOutlet weak var bioTextView: UITextView!
let databaseRef = FIRDatabase.database().reference()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(animated: Bool) {
let userID = FIRAuth.auth()?.currentUser?.uid
databaseRef.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user level
let userBio = snapshot.value!["userBio"] as! String
self.bioTextView.text = userBio
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func doneSave(sender: UIButton) {
let textView = bioTextView.text
self.dismissViewControllerAnimated(false, completion: nil)
databaseRef.child("users/(user.uid)/userBio").setValue(textView)
}
}
I'm just trying to update a specific child: userBio and not affect the entire object.
Warning
Avoid instantiating your database reference to a variable out of scope. Reason why :- Outside your scope, when you instantiate a class to a variable you don't know wether or not your FIRApp has already been configured or not, or in general if that class has even been initialised as of yet or not. Just provide a reference(!) to the variable and instantiate later in a scope.
Change:-
let databaseRef = FIRDatabase.database().reference()
to
let databaseRef = FIRDatabaseReference!
And before using it just initialise it as:-
databaseRef = FIRDatabase.database().reference()
Try :-
#IBAction func doneSave(sender: UIButton) {
let textView = bioTextView.text
self.dismissViewControllerAnimated(false, completion: nil)
databaseRef = FIRDatabase.database().reference()
databaseRef.child("users/\(FIRAuth.auth!.currentUser!.uid)/userBio").setValue(textView)
}