gpt4 book ai didi

swift - Twilio API 错误

转载 作者:行者123 更新时间:2023-11-30 13:36:08 26 4
gpt4 key购买 nike

我正在 swift 中制作一个聊天应用程序,并且获得了 Twilio 网页的源代码。我的 Viewcontroller 代码中不断出现这些 Unresolved 标识符错误。我是否必须导入 Json 代码,或者是否必须导入我的桥接 header 。

这是我的 View Controller 代码。

class ViewController: UIViewController {

// MARK: IP messaging memebers
var client: TwilioIPMessagingClient? = nil
var generalChannel: TWMChannel? = nil
var identity = ""
var messages: [TWMMessage] = []

// MARK: UI controls
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

// Fetch Access Token form the server and initialize IPM Client - this assumes you are running
// the PHP starter app on your local machine, as instructed in the quick start guide
let deviceId = UIDevice.currentDevice().identifierForVendor!.UUIDString
let urlString = "http://localhost:8000/token.php?device=\(deviceId)"
let defaultChannel = "general"

// Get JSON from server
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config, delegate: nil, delegateQueue: nil)
let url = NSURL(string: urlString)
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "GET"

// Make HTTP request
session.dataTaskWithRequest(request, completionHandler: { data, response, error in
if (data != nil) {
// Parse result JSON
let json = JSON(data: data!)
let token = json["token"].stringValue
self.identity = json["identity"].stringValue

// Set up Twilio IPM client and join the general channel
self.client = TwilioIPMessagingClient.ipMessagingClientWithToken(token, delegate: self)

// Auto-join the general channel
self.client?.channelsListWithCompletion { result, channels in
if (result == .Success) {
if let channel = channels.channelWithUniqueName(defaultChannel) {
// Join the general channel if it already exists
self.generalChannel = channel
self.generalChannel?.joinWithCompletion({ result in
print("Channel joined with result \(result)")
})
} else {
// Create the general channel (for public use) if it hasn't been created yet
channels.createChannelWithFriendlyName("General Chat Channel", type: .Public) {
(channelResult, channel) -> Void in
if result == .Success {
self.generalChannel = channel
self.generalChannel?.joinWithCompletion({ result in
self.generalChannel?.setUniqueName(defaultChannel, completion: { result in
print("channel unqiue name set")
})
})
}
}
}
}
}

// Update UI on main thread
dispatch_async(dispatch_get_main_queue()) {
self.navigationItem.prompt = "Logged in as \"\(self.identity)\""
}
} else {
print("Error fetching token :\(error)")
}
}).resume()

// Listen for keyboard events and animate text field as necessary
NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("keyboardWillShow:"),
name:UIKeyboardWillShowNotification,
object: nil);

NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("keyboardDidShow:"),
name:UIKeyboardDidShowNotification,
object: nil);

NSNotificationCenter.defaultCenter().addObserver(self,
selector: Selector("keyboardWillHide:"),
name:UIKeyboardWillHideNotification,
object: nil);

// Set up UI controls
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 66.0
self.tableView.separatorStyle = .None
}

// MARK: Keyboard Dodging Logic

func keyboardWillShow(notification: NSNotification) {
let keyboardHeight = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue.height
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.bottomConstraint.constant = keyboardHeight! + 10
self.view.layoutIfNeeded()
})
}

func keyboardDidShow(notification: NSNotification) {
self.scrollToBottomMessage()
}

func keyboardWillHide(notification: NSNotification) {
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.bottomConstraint.constant = 20
self.view.layoutIfNeeded()
})
}

// MARK: UI Logic

// Dismiss keyboard if container view is tapped
@IBAction func viewTapped(sender: AnyObject) {
self.textField.resignFirstResponder()
}

// Scroll to bottom of table view for messages
func scrollToBottomMessage() {
if self.messages.count == 0 {
return
}
let bottomMessageIndex = NSIndexPath(forRow: self.tableView.numberOfRowsInSection(0) - 1,
inSection: 0)
self.tableView.scrollToRowAtIndexPath(bottomMessageIndex, atScrollPosition: .Bottom,
animated: true)
}

}

// MARK: Twilio IP Messaging Delegate
extension ViewController: TwilioIPMessagingClientDelegate {
// Called whenever a channel we've joined receives a new message
func ipMessagingClient(client: TwilioIPMessagingClient!, channel: TWMChannel!,
messageAdded message: TWMMessage!) {
self.messages.append(message)
self.tableView.reloadData()
dispatch_async(dispatch_get_main_queue()) {
if self.messages.count > 0 {
self.scrollToBottomMessage()
}
}
}
}

// MARK: UITextField Delegate
extension ViewController: UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
let msg = self.generalChannel?.messages.createMessageWithBody(textField.text!)
self.generalChannel?.messages.sendMessage(msg) { result in
textField.text = ""
textField.resignFirstResponder()
}
return true
}
}

// MARK: UITableView Delegate
extension ViewController: UITableViewDelegate {

// Return number of rows in the table
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.messages.count
}

// Create table view rows
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MessageCell", forIndexPath: indexPath)
let message = self.messages[indexPath.row]

// Set table cell values
cell.detailTextLabel?.text = message.author
cell.textLabel?.text = message.body
cell.selectionStyle = .None
return cell
}
}

// MARK: UITableViewDataSource Delegate
extension ViewController: UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
}

有错误的代码行是,

var client: TwilioIPMessagingClient? = nil
var generalChannel: TWMChannel? = nil

var messages: [TWMMessage] = []

self.client = TwilioIPMessagingClient.ipMessagingClientWithToken(token, delegate: self)


extension ViewController: TwilioIPMessagingClientDelegate {
// Called whenever a channel we've joined receives a new message
func ipMessagingClient(client: TwilioIPMessagingClient!, channel: TWMChannel!,
messageAdded message: TWMMessage!)

谢谢。

最佳答案

以下内容来自 GitHub 上的项目:

注意:当前版本的应用程序使用固定版本的 Twilio SDK(包含在存储库中)。该版本仅适用于arm设备,因为SDK包含一些 native 代码。

https://github.com/TwilioDevEd/twiliochat-swift/tree/master

关于swift - Twilio API 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36051430/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com