2017-08-17 4 views
0

解決策を見つけようとしていますが、私はfirebaseのデータ文字列を呼び出し、UIAlertViewの "title"と "message"として使用したいと思います。私はこのUIAlertViewをジオフェンスメッセージとして使用しています。ジオフェンスは、UIAlertViewをメッセージに入力する基本的な場所に設定しただけで、他のユーザーが読むために書いたメッセージを呼び出す必要があります。これまでのところ、このセットアップでは「OK」ボタンだけがポップアップし、一度リージョンに入力すると何も表示されません。Swift Firebase UIAlertViewとFirebaseのデータ文字列

func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) { 
    showAlert(withTitle: name, message: message) 
    //showAlert(withTitle: "Enter \(region.identifier)", message: "Geofence Message") 
    print(state) 
    print("region :\(region.identifier)") 

} 

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { 
    showAlert() 
    //showAlert(withTitle: "Enter \(region.identifier)", message: "Geofence Message") 
    print("DID ENTER REGION") 
} 

func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) { 
    //showAlert(withTitle: "Exit \(region.identifier)", message: "Message Exit") 
    //TODO: stop local sequence 
    print("DID EXIT REGION") 

} 

func showAlert(withTitle title: String?, message: String?) { 
    FIRDatabase.database().reference().child("Businesses").observeSingleEvent(of: .value, with: { snapshot in 
     if let dictionary = snapshot.value as? [String: AnyObject] { 
      self.name = dictionary["businessName"] as? String 
      self.message = dictionary["geofenceMessage"] as? String 
     } 
     let alert = UIAlertController(title: self.name, message: self.message, preferredStyle: .alert) 
     let action = UIAlertAction(title: "Ok", style: .cancel, handler: nil) 
     alert.addAction(action) 
     self.present(alert, animated: true, completion: nil) 
    }) 
} 

詳細

// Populate Map With Firebase Businesses 
func loadPlaces(){ 
    if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) { 
    FIRDatabase.database().reference().child("Businesses").observe(.value, with: { snapshot in 
     self.locationData = snapshot.value as? NSDictionary 
     if let data = self.locationData{ 
      for (key,obj) in data{ 
       let value = obj as? NSDictionary 
       let locationValue = value as! [String: Any] 
       let lat = Double(locationValue["businessLatitude"] as! String) 
       let long = Double(locationValue["businessLongitude"] as! String) 
       let businessTitle = String(locationValue["businessName"] as! String) 
       let center = CLLocationCoordinate2D(latitude: lat!, longitude: long!) 

       let radius = CLLocationDistance(500.0) 
       let geoRegion = CLCircularRegion(center: center, radius: radius, identifier: businessTitle!) 
       self.geofences.append(geoRegion) 
       self.locationManager.startMonitoring(for: geoRegion) 
       let overlay = MKCircle(center: center, radius: radius) 
       self.mapView.add(overlay) 
       geoRegion.notifyOnEntry = true 
       geoRegion.notifyOnExit = true 

       let annotation = MKPointAnnotation() 
       annotation.coordinate = geoRegion.center 
       annotation.title = businessTitle 
       self.mapView.addAnnotation(annotation) 

       self.nameKeyDict[(value?.value(forKey: "businessName") as? String)!] = key as? String 
      } 
     } 
    }) 
    } else { 
     print("No Bueno") 
    } 
} 

Firebaseデータ構造

FireBase Data Structure

答えて

0

を助けることを願っています。この方法を使用してみてくださいはfirebaseに誰も興味を持って、非常に単純なクエリのチェックのための答えです。すべての狂気の代わりに、他の人たちがここに投稿しました。 "region.identifier"を "queryOrderded"と同じものに設定して、Firebaseが正しい "users"データを取得するようにしてください。 firebaseの呼び出しで ".value"の代わりに ".childAdded"を使用してFirebaseデータベースの "users"をその値ではなくチェックします。

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { 
    FIRDatabase.database().reference().child("Businesses").queryOrdered(byChild: "businessName").queryEqual(toValue: region.identifier).observe(.childAdded, with: { snapshot in 
     if snapshot.exists() { 
      if let dictionary = snapshot.value as? [String:Any] { 
       self.name = dictionary["businessName"] as? String 
       self.message = dictionary["geofenceMessage"] as? String 
      } 
      let alert = UIAlertController(title: self.name, message: self.message, preferredStyle: .alert) 
      let action = UIAlertAction(title: "Ok", style: .cancel, handler: nil) 
      alert.addAction(action) 
      self.present(alert, animated: true, completion: nil) 
     } else { 
      print("Snap doesnt exist") 
     } 
    }) 
print("DID ENTER REGION") 
} 
+0

あなたが答えを理解していないからといって、答えが「狂っている」わけではありません。それはあなたを助けるために自由時間を費やす人々について少し謙虚であるようにあなたに適しています。 – matiastofteby

0

nametitle変数がSEになる前にアラートを作成しているため、この問題が発生しますt。名前とタイトルの初期化をメインキューに変更してみてください。また、Firebase observeSingleEventを使用して完了ブロックを作成し、補完内に警告を作成してfirebaseからのフェッチ値が完全であることを確認します。

+0

あなたのデータ構造が正しいことを望みます。 –

+0

これらの名前の値を印刷して、実際にfirebaseから応答があるかどうかを確認してください。 –

+0

私のデータ構造は正しいです、それはすべて動作します。ちょうど私のフォーマットはオフです –

0

これは、オブザーバイベントが非同期に呼び出され、値がフェッチされる前に警告コントローラが呼び出されたためです。

使っそうのような完了ハンドラを使用してfirebase値取得するための一つの機能:観察イベントが実行された場合には

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { 
    retrieveGeofenceMessage(with: region){ 
      if $0 { 
       // Create Alert controller here 
       let alert = UIAlertController(title: self.name, message: self.message, preferredStyle: .alert) 
       let action = UIAlertAction(title: "Ok", style: .cancel, handler: nil) 
       alert.addAction(action) 
       self.present(alert, animated: true, completion: nil) 

      } 

     } 

     print("DID ENTER REGION") 
} 

:あなたはあなたが書いた関数を呼び出すところから次に

func retrieveGeofenceMessage(with region: CLRegion, completionHandler: @escaping (Bool) -> Void){ 
FIRDatabase.database().reference().child("Businesses").child(region.identifier).observeSingleEvent(of: .value, with: { snapshot in 
    if let dictionary = snapshot.value as? [String: AnyObject] { 
     self.name = dictionary["businessName"] as? String 
     self.message = dictionary["geofenceMessage"] as? String 

     } 
     completionHandler(true) 
    }) 
} 

をあなたは今通知され、まだ設定されていない変数を使用しないことを保証します。

+0

"YourFirebaseClassObject"をどこに配置するか分かりません –

+0

これは、firebase-callsがすべて作成されたクラスを作成し、そのクラスの外から関数を呼び出すと仮定しているためです。あなたがクラス内でそれを呼び出す場合は、単に関数名や "self"を正しく設定する必要があります。 – matiastofteby

+0

私はFIRDatabase.database()を使ってファイヤーベースの呼び出しを行います。毎回 –

0

それはここで

FIRDatabase.database().reference().child("Businesses").observeSingleEvent(of: .value, with: { snapshot in 
     if let dictionary = snapshot.value as? [String: AnyObject] { 
      self.name = dictionary["businessName"] as? String 
      self.message = dictionary["geofenceMessage"] as? String 
      self.alertPopup(name: self.name, message: self.message) 
     } 

    }) 

} 

func alertPopup (name: NSString, message: NSString){ 
    let alert = UIAlertController(title: name as String, message: message as String, preferredStyle: .alert) 
    let action = UIAlertAction(title: "Ok", style: .cancel, handler: nil) 
    alert.addAction(action) 
    self.present(alert, animated: true, completion: nil) 
} 
関連する問題