swift Firebase教程:使用JSQMessagesViewController进行实时聊天

时间:2016-08-06 17:25:24

标签: swift jsqmessagesviewcontroller

import UIKit
import Firebase
import FirebaseAuth
import JSQMessagesViewController

class ChatViewController: JSQMessagesViewController {

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        super.prepareForSegue(segue, sender: sender)
        let navVc = segue.destinationViewController as! UINavigationController // 1
        let chatVc = navVc.viewControllers.first as! ChatViewController // 2
        chatVc.senderId = FIRAuth.auth()?.currentUser?.uid  // 3
        chatVc.senderDisplayName = "" // 4
    }

}

我尝试使用此链接https://www.raywenderlich.com/122148/firebase-tutorial-real-time-chat中的JSQMessagesViewController来构建聊天,并且始终收到如下错误:

<p>2016-08-06 19:45:41.706 ChatChat[13399:] <FIRAnalytics/INFO> Firebase Analytics enabled<br> Signed in with uid: RhBSL8rOBjTfd11pcdB1aJ076fl2<br> 2016-08-06 19:45:51.276 ChatChat[13399:361284] RhBSL8rOBjTfd11pcdB1aJ076fl2<br> 2016-08-06 19:45:51.448 ChatChat[13399:361284] *** <br> Assertion failure in
-[ChatChat.ChatViewController viewWillAppear:], /Users/admin/Downloads/ChatChat-starter/Pods/JSQMessagesViewController/JSQMessagesViewController/Controllers/JSQMessagesViewController.m:276 2016-08-06 19:45:51.463 ChatChat[13399:361284] *** <br> Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: self.senderId != nil'<br>
*** First throw call stack:<br> (<br>   0   CoreFoundation                      0x0000000106c50d85
__exceptionPreprocess + 165 <br>    1   libobjc.A.dylib                     0x0000000108a2cdeb objc_exception_throw + 48 <br>   2   CoreFoundation   0x0000000106c50bea +[NSException raise:format:arguments:] + 106 <br>   3 Foundation                          0x00000001070a1d5a
-[NSAssertionHandler  handleFailureInMethod:object:file:lineNumber:description:] + 198 <br> 4   JSQMessagesViewController           0x0000000106827a06
-[JSQMessagesViewController viewWillAppear:] + 342 <br> 5   UIKit                               0x00000001076192bd -[UIViewController
_setViewAppearState:isAnimating:] + 710 <br>    6   UIKit                               0x0000000107619958 -[UIViewController __viewWillAppear:] + 149 <br> 7  UIKit                               0x0000000107658c83
-[UINavigationController _startTransition:fromViewController:toViewController:] + 781 <br>  8   UIKit                               0x0000000107659c4d
-[UINavigationController _startDeferredTransitionIfNeeded:] + 890 <br>  9   UIKit                               0x000000010765ad0b
-[UINavigationController __viewWillLayoutSubviews] + 57 <br>    10  UIKit                               0x0000000107809503 -[UILayoutContainerView layoutSubviews] + 248 <br>   11  UIKit                               0x0000000107533980
-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 <br>   12  QuartzCore                          0x000000010c13ac00 -[CALayer layoutSublayers] + 146 <br>    13  QuartzCore                          0x000000010c12f08e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 <br> 14  QuartzCore                          0x000000010c12ef0c
_ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 <br> 15  QuartzCore                          0x000000010c1233c9
_ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 <br>    16  QuartzCore                          0x000000010c151086
_ZN2CA11Transaction6commitEv + 486 <br> 17  UIKit                               0x00000001074a519b _afterCACommitHandler + 174 <br> 18  CoreFoundation 0x0000000106b75c37
__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 <br> 19  CoreFoundation                      0x0000000106b75ba7
__CFRunLoopDoObservers + 391 <br>   20  CoreFoundation                      0x0000000106b6b7fb __CFRunLoopRun + 1147 <br>   21  CoreFoundation       0x0000000106b6b0f8 CFRunLoopRunSpecific + 488 <br> 22  GraphicsServices                    0x0000000109d49ad2 GSEventRunModal
+ 161 <br>  23  UIKit                               0x0000000107478f09 UIApplicationMain + 171 <br> 24  ChatChat                            0x0000000105344be2 main + 114 <br>  25  libdyld.dylib                   0x000000010952b92d start + 1 <br>   26  ???                              0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) </p><br>

2 个答案:

答案 0 :(得分:0)

查看错误原因。具体来说,reason: 'Invalid parameter not satisfying: self.senderId != nil'。这意味着您为senderId设置的值为nil。即,在这种情况下FIRAuth.auth()?.currentUser?.uidnil。确保您有登录Firebase的有效用户。希望这有帮助!

答案 1 :(得分:0)

let currentUser: FIRUser = (FIRAuth.auth()?.currentUser)!
let navVc = segue.destinationViewController as! UINavigationController // 1
let chatVc = navVc.viewControllers.first as! ChatViewController // 2
chatVc.senderId = currentUser.uid  // 3
chatVc.senderDisplayName = "" // 4

试试这个