Post

Replies

Boosts

Views

Activity

Search for HC-05 Bluetooth device via Bluetooth
Good afternoon guys, I want to create an app that's detects common bluetooth based credit card skimmers predominantly found in gas pumps. The app scans for available bluetooth connections looking for a device with title HC-05. If found, the app will attempt to connect using the default password of 1234. Once connected, the letter 'P' will be sent. If a response of 'M' then there is a very high likelihood there is a skimmer in the bluetooth range of your phone (5 to 15 feet). I can show you the code I already have: class ScanViewController: UIViewController, CBPeripheralDelegate, CBCentralManagerDelegate {          // Properties Back-End         private var centralManager: CBCentralManager!         private var peripherals: [CBPeripheral] = []         private var peripheral: CBPeripheral!         override func viewDidLoad() {         super.viewDidLoad()         view.backgroundColor = .systemGroupedBackground                  centralManager = CBCentralManager(delegate: self, queue: nil)            }     //MARK: - Back-End          // If we're powered on, start scanning            func centralManagerDidUpdateState(_ central: CBCentralManager) {                print("Central state update")                if central.state != .poweredOn {                    print("Central is not powered on")                } else {                    print("Central scanning for", ParticlePeripheral.skimmerDeviceUUID);                    centralManager.scanForPeripherals(withServices: [ParticlePeripheral.skimmerDeviceUUID],                                                      options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])                }            }          // Handles the result of the scan            func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {                // We've found it so stop scan                self.centralManager.stopScan()             print("Stopped to scan")                // Copy the peripheral instance                self.peripheral = peripheral                self.peripheral.delegate = self                // Connect!                self.centralManager.connect(self.peripheral, options: nil)                         }          // The handler if we do connect succesfully             func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {                 if peripheral == self.peripheral {                     print("Connected to Skimmer Scammer")                     peripheral.discoverServices([ParticlePeripheral.skimmerDeviceUUID])                 }             }          // Handles discovery event             func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {                 if let services = peripheral.services {                     for service in services {                         if service.uuid == ParticlePeripheral.skimmerDeviceUUID {                             print("Skimmer Scanner service found")                             //Now kick off discovery of characteristics                             peripheral.discoverCharacteristics([...], for: service)                             return                         }                     }                 }             } class ParticlePeripheral: NSObject {         // I think thats the uuid for HC-05         public static let skimmerDeviceUUID     = CBUUID.init(string: "00001101-0000-1000-8000-00805F9B34FB")     } I googled around for hours but I wasn't able to find a solution and I have never worked with Bluetooth connections before, that makes it really difficult but I tried. My questions: is the code I already have valid? How to can I connect via pin code? "1234" I would be so helpful if someone here has some advices for me, thanks in advance!
1
0
2.4k
Feb ’22
Swift Firebase Error: Thread 1: "Failed to get FirebaseDatabase instance: Specify DatabaseURL within FIRApp or from your databaseForApp:URL: call."
Hey guys, I want to connect my Xcode Project with Firebase to load my Userdata into the database. Although I already used the same code in another project, it doesn't work and throws me the following offer: Thread 1: "Failed to get FirebaseDatabase instance: Specify DatabaseURL within FIRApp or from your databaseForApp:URL: call." Here is my Code: @objc func registerButtonTapped() {         if acceptTermsOfUseSwitch.isOn == true && emailTextField.text?.isEmpty == false && passwordTextField.text?.isEmpty == false {             print("Register...")             //Authentification         Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (data, error) in             if let err = error {                 print(err.localizedDescription)                 return             }             guard let newUser = data?.user else { return }             let uid = newUser.uid             print("User: \(String(describing: newUser.email)) wurde erstellt, ID: \(String(describing: newUser.uid))")             //Database             self.uploadUserData(uid: uid, email: self.emailTextField.text!)             }         } else {             print("No valid data")         }     } func uploadUserData(uid: String, email: String) {         var ref = DatabaseReference()         ref = Database.database().reference().child("users").child(uid)                 print("Datenbank ID: \(ref)")                 ref.setValue(["email" : email, "userID" : uid])                 print("User data uploaded")                 //Change VC:             let vc = TabBarController()                 self.dismiss(animated: true, completion: {              UIApplication.shared.windows.first?.rootViewController = vc             })             } I hope someone can help me, thanks in advance!
2
0
1.3k
May ’21
Push data to other VC with protocol, error: force unwrapped a nil value
Hey guys, my problem: I have to different VC's with textFields, and I need their text on the third ViewController, I use a two protocols to push the textfield text as data to the third VC. Here are the two Protocols: protocol RegisterNameProtocol {     var name: String? { get set } } protocol RegisterNumberProtocol {     var phoneNumber: String? { get set } } I implemented the variables in the two vc's and afterwards I tested the functionality with print statements, it worked! Now I need them in the third VC, this is my code yet but it throws the following error: force unwrapped a nil value The third VC: weak var nameDelegate: RegisterNameProtocol? weak var phoneDelegate: RegisterNumberProtocol?     var phoneNumber = ""     var name = "" func xyzzy() { phoneNumber = (phoneDelegate?.phoneNumber)!         name = (nameDelegate?.name)! } I hope someone can help me! Thanks in advance
7
0
624
May ’21
UICollectionView Layout Error: nil layout parameter
Hey guys, when I run my project the error: 'UICollectionView must be initialized with a non-nil layout parameter', appears. I don't know why because I already used the same code in another project and it worked. This is my code:  var myCollectionView : UICollectionView? func setUpContactsCollectionView() {         let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()         layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)         layout.itemSize = CGSize(width: 100, height: 100)         layout.scrollDirection = .horizontal                myCollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)         myCollectionView?.showsHorizontalScrollIndicator = false                  myCollectionView?.register(InserateCollectionCell.self, forCellWithReuseIdentifier: "InserateCollectionCell")         myCollectionView?.backgroundColor = UIColor.white                  myCollectionView?.dataSource = self         myCollectionView?.delegate = self   myCollectionView?.translatesAutoresizingMaskIntoConstraints = false         myCollectionView?.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true         myCollectionView?.topAnchor.constraint(equalTo: topAnchor, constant: 50).isActive = true         myCollectionView?.widthAnchor.constraint(equalTo: widthAnchor).isActive = true         myCollectionView?.heightAnchor.constraint(equalTo: heightAnchor).isActive = true     } Thanks in advance!
12
0
2.6k
May ’21
How to make a searcher behave like a searchController?
Hey guys, on Stackoverflow I read a bit about the differences between UISearchBar and UISearchController. I already created the GUI of my Project and I'm absolutely in love with it, the problem is that I am using a Searchbar and of course I could change that to a SearchController but I don't want it to be in the navigation bar. So what and HOW I have to implement to make my Searchbar behave and usable like a SearchController? Thanks in advance
0
0
289
Apr ’21
How to set up a expandable TableView Cell?
Hey guys, I want to create a expandable Cell in my tableView but there are a few things which don't work. This is my Code: // In TableView Cell var bottomViewIsVisible = false func setUpArrowButton() {         arrowButton.tintColor = .lightGray         arrowButton.setImage(downImage, for: .normal)         if bottomViewIsVisible == false {         arrowButton.addTarget(self, action: #selector(expandView), for: .touchUpInside)         }         if bottomViewIsVisible == true {             arrowButton.addTarget(self, action: #selector(self.shrinkView), for: .touchUpInside)         }                  //Constraints         arrowButton.translatesAutoresizingMaskIntoConstraints = false         arrowButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true         arrowButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -15).isActive = true         arrowButton.widthAnchor.constraint(equalToConstant: 40).isActive = true         arrowButton.heightAnchor.constraint(equalToConstant: 40).isActive = true     }          @objc func expandView() {         bottomViewIsVisible = true         UIButton.animate(withDuration: 0.3) {             self.bottomView.isHidden = false             self.arrowButton.setImage(self.upImage, for: .normal)                     }     }          @objc func shrinkView() {         bottomViewIsVisible = false         UIButton.animate(withDuration: 0.3) {             self.bottomView.isHidden = true             self.arrowButton.setImage(self.downImage, for: .normal)     }     }          func setUpBottomView() {         bottomView.backgroundColor = .systemPink                  bottomView.translatesAutoresizingMaskIntoConstraints = false         bottomView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true         bottomView.topAnchor.constraint(equalTo: Label.bottomAnchor, constant: 5).isActive = true         bottomView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true         bottomView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10).isActive = true         bottomView.heightAnchor.constraint(equalToConstant: 60).isActive = true     }      In TableView: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) - UITableViewCell  { if indexPath.row == 2 {             let priceSliderCell = tableView.dequeueReusableCell(withIdentifier: "PriceSlider", for: indexPath) as! PriceRangeCell             priceSliderCell.setUpTitleLabel(text: "Price")             priceSliderCell.setUpArrowButton()             priceSliderCell.setUpBottomView()                 tableView.beginUpdates()                 tableView.endUpdates()                 tableView.deselectRow(at: indexPath, animated: true)             priceSliderCell.delegate = self             priceSliderCell.selectionStyle = .none             return priceSliderCell         } } The first and biggest problem is that the cell height in the tableView doesn't fit to the height it should have after the cell expands. How can I do that? The second problem is that the shrink functions doesn't work. I hope someone can help me, thanks in advance!
7
0
575
Apr ’21
Problems with UILabel Constraints TopAnchor
Hey guys, I wrote a function which creates an UIView with UIImageView and some Label in it. This is my Code: func setUpInserateWindows(images: [UIImage], priceText: String, descriptionText: String, streetAddress: String, cityAddress: String) {         var inserates = [UIView]()                  for image in images {                          let inserateView = UIView()             let imageView = UIImageView()             let price = UILabel()             let description = UILabel()             let address = UILabel()             let city = UILabel()                          inserateView.addSubview(imageView)             inserateView.addSubview(price)             inserateView.addSubview(description)                          //InserateView             inserateView.sizeToFit()             inserateView.backgroundColor = .clear             inserateView.translatesAutoresizingMaskIntoConstraints = false             inserateView.widthAnchor.constraint(equalToConstant: 190).isActive = true             inserateView.heightAnchor.constraint(equalToConstant: 100).isActive = true                          //ImageView             imageView.backgroundColor = .systemBlue             imageView.image = image             imageView.contentMode = .scaleAspectFill             imageView.layer.cornerRadius = 20             imageView.layer.masksToBounds = true                          imageView.translatesAutoresizingMaskIntoConstraints = false             imageView.centerXAnchor.constraint(equalTo: inserateView.centerXAnchor).isActive = true             imageView.topAnchor.constraint(equalTo: inserateView.topAnchor, constant: 5).isActive = true             imageView.widthAnchor.constraint(equalToConstant: 260).isActive = true             imageView.heightAnchor.constraint(equalToConstant: 130).isActive = true                          //Pricetag             price.text = priceText             price.textAlignment = .left             price.textColor = .black             price.font = UIFont.boldSystemFont(ofSize: 20)                          price.translatesAutoresizingMaskIntoConstraints = false             price.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 5).isActive = true             price.leadingAnchor.constraint(equalTo: imageView.leadingAnchor).isActive = true             price.widthAnchor.constraint(equalToConstant: 150).isActive = true             price.heightAnchor.constraint(equalToConstant: 30).isActive = true                          //Description             description.text = descriptionText             description.numberOfLines = 1             description.textColor = .systemGray             description.textAlignment = .left             description.font = UIFont.boldSystemFont(ofSize: 15)                          description.translatesAutoresizingMaskIntoConstraints = false             description.topAnchor.constraint(equalTo: price.bottomAnchor).isActive = true             description.leadingAnchor.constraint(equalTo: imageView.leadingAnchor).isActive = true             description.widthAnchor.constraint(equalToConstant: 250).isActive = true             description.heightAnchor.constraint(equalToConstant: 20).isActive = true                          //Street Address             address.text = streetAddress             address.numberOfLines = 1             address.textColor = .systemGray             address.textAlignment = .left             address.font = UIFont.boldSystemFont(ofSize: 15)                          address.translatesAutoresizingMaskIntoConstraints = false             address.topAnchor.constraint(equalTo: description.bottomAnchor).isActive = true             address.leadingAnchor.constraint(equalTo: imageView.leadingAnchor).isActive = true             address.widthAnchor.constraint(equalToConstant: 250).isActive = true             address.heightAnchor.constraint(equalToConstant: 20).isActive = true                                       inserates.append(inserateView)         }                  for inserate in inserates {             stackView.addArrangedSubview(inserate)         }              } The problem is at the topAnchor of the address Label, it throws me this error: "Thread 1: "Unable to activate constraint with anchors NSLayoutYAxisAnchor:0x60000344ea40 \"UILabel:0x7f9198d6e830.top\" and NSLayoutYAxisAnchor:0x60000344e7c0 \"UILabel:0x7f9198d6e5c0.bottom\" because they have no common ancestor.  Does the constraint or its anchors reference items in different view hierarchies?  That's illegal."" I really do not understand where the problem is, can somebody help me? :)
3
0
2.9k
Mar ’21
UI Elements in TableViewCell are not working by touching, why?
Hey guys, built a tableView with Custom Cells and everything is looking good but when I want to use the elements in the Cells (Searchbar, SegmentControl) they are doing nothing, can someone help me please For example, this is my SearchBar Cell: class SearchCell: UITableViewCell, UISearchBarDelegate {     let searchbar = UISearchBar()     override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {         super.init(style: style, reuseIdentifier: reuseIdentifier)         addSubview(searchbar)     }     required init?(coder: NSCoder) {         fatalError("init(coder:) has not been implemented")     }     func setConstraints() {         searchbar.placeholder = "Country, City"         searchbar.searchBarStyle = .minimal         let TapGesture = UITapGestureRecognizer(target: self, action: #selector(searchBarTapped))         searchbar.addGestureRecognizer(TapGesture)         searchbar.isUserInteractionEnabled = true         Constraints         searchbar.translatesAutoresizingMaskIntoConstraints = false         searchbar.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true         searchbar.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true         searchbar.widthAnchor.constraint(equalToConstant: 380).isActive = true         searchbar.heightAnchor.constraint(equalToConstant: 40).isActive = true     }     @objc func searchBarTapped() {         searchbar.delegate = self     }     override func awakeFromNib() {         super.awakeFromNib() {     }     override func setSelected(_ selected: Bool, animated: Bool) {         super.setSelected(selected, animated: animated)     } } And this is my TableView Controller: class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {          let cellClasses = SearchCell()          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) - Int {         return 2     }          func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) - IndexPath? {         return indexPath     }          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) - UITableViewCell {         if indexPath.row == 0 {                 let cell = tableView.dequeueReusableCell(withIdentifier: "SearchCell", for: indexPath) as! SearchCell         cell.setConstraints()             cell.selectionStyle = .none             return cell         }         if indexPath.row == 1 {                 let cell = tableView.dequeueReusableCell(withIdentifier: "SegmentControlCell", for: indexPath) as! SegmentControlCell                 cell.setUpSegmentControlCell()             cell.selectionStyle = .none                 return cell             } else {                 let basicCell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath) as! TestCell                 basicCell.setUpabel()                 basicCell.selectionStyle = .none                 return basicCell         }                  }                    @IBAction func jumpToSearchViewController() {         tabBarController?.selectedIndex = 2     }     let tableView = UITableView()              override func viewDidLoad() {         super.viewDidLoad()                  tableView.register(SearchCell.self, forCellReuseIdentifier: "SearchCell")         tableView.register(SegmentControlCell.self, forCellReuseIdentifier: "SegmentControlCell")         tableView.register(TestCell.self, forCellReuseIdentifier: "basicCell")         tableView.dataSource = self         tableView.delegate = self         tableView.allowsSelection = true         view.backgroundColor = .white         navigationController?.navigationBar.prefersLargeTitles = true         navigationItem.title = "Search"         Functions         configureTableView()     }        func configureTableView() {         view.addSubview(tableView)         tableView.rowHeight = 80         Constraints         tableView.translatesAutoresizingMaskIntoConstraints = false         tableView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true         tableView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true         tableView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true         tableView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true     }          Thanks in advance! :)
3
0
811
Mar ’21