How to get all friends programmatically from openfire server in objective C, I am using XMPP Framework for chat Functionality.
Here is a function to fetch friends.
Add your host name in below function.
func getList() {
let query = try! XMLElement(xmlString: "<query xmlns='http://jabber.org/protocol/disco#items' node='all users'/>")
let iq = XMPPIQ(type: "get", to: XMPPJID(string: "Your Host Name"), elementID: xmppStream.generateUUID(), child: query)
iq?.addAttribute(withName: "id", stringValue: "get")
xmppStream.send(iq)
}
You will get the list in delegate method.
extension YourClassName: XMPPRosterDelegate {
func xmppRosterDidEndPopulating(_ sender: XMPPRoster!) {
if let jids = xmppRoster.xmppRosterStorage.jids(for: xmppStream) as? [XMPPJID] {
print("JIDS: \(String(describing: jids))")
for item in jids {
print(item.user)
}
}
}
}
You can have a look at my this link for XMPP Connection and different delegates.
https://stackoverflow.com/a/50149977/2781720
In iOS you can easily get Group members/friends using below function in xmpp
- (void)xmppRoom:(XMPPRoom *)sender didFetchModeratorsList:(NSArray *)items
Related
I'm using the new SSE endpoints for Live comments on Live videos. my question is how can I achive this on IOS and what library should I use
I tried using the Event Source for IOS https://github.com/inaka/EventSource
But Even though I tried to use OnMessage and addOnEventsource it dosent seem to work.
Here is my code for listening to the live comments
var live_commentURL = URL.init(string:"https://streaming-graph.facebook.com/\(fbLiveStreamId!)/live_comments?access_token=\(accessToken ?? "")&comment_rate=one_per_two_seconds&fields=from{name,id},message")
let queryItems = [NSURLQueryItem(name: "access_token", value: accessToken!), NSURLQueryItem(name: "comment_rate", value: "one_hundred_per_second"),NSURLQueryItem(name: "fields", value: "from{name,id},message")]
let urlComps = NSURLComponents(string: "https://streaming-graph.facebook.com/\(fbLiveStreamId!)/live_comments")!
// var headders:[String:String] = [:]
// headders["access_token"] = accessToken!
// headders["comment_rate"] = "one_hundred_per_second"
// headders["fields"] = "from{name,id},message"
var eventSource = EventSource.init(url:urlComps.url!)
eventSource.connect()
eventSource.onOpen {
print("Successfully Connected to server")
}
eventSource.addEventListener("user-connected") {(id, event, data) in
print("id:", id)
print("event:" , event)
print("data: ", data)
}
eventSource.onMessage { (id, event, data) in
print("id:", id)
print("event:" , event)
print("data: ", data)
}
print(eventSource.events())
eventSource.onComplete { (code, isErro, error) in
print(code)
}
I have tried to send the access tokesn and other fields in headders too but got no luck with it.
I tried 2 methods
method 1
send accesstoken,comment_rateand fields as headders but I dont think that is the right way.
method 2
Since they are all query params I used NSURLComponents.
It was an issue with my auth tokens My bad
On version 2, the sender app was able to send messages.
func deviceManager(_ deviceManager: GCKDeviceManager!,
didConnectToCastApplication
applicationMetadata: GCKApplicationMetadata!,
sessionID: String!,
launchedApplication: Bool) {
deviceManager.add(self.textChannel)
}
However, the API says that we are now using GCKSessionManager instead of GCKDeviceManager.
The API says I must have a GCKSession add the textChannel, which I did here:
Once the session starts, I add the textChannel (because sessionManager.currentCastSession was nil before the session started).
func sessionManager(_ sessionManager: GCKSessionManager, didStart session: GCKSession) {
if session.device == connectionQueue {
connectionQueue = nil
}
self.sessionManager!.currentCastSession!.add(textChannel)
print("")
}
Meanwhile, I send the text message in another function:
let result = self.textChannel.sendTextMessage("\(self.textField.text)", error: &error)
But the result is always false, and the error is always "Channel is not connected or is not registered with a session".
In addition, when I do:
print("isConnected1 \(self.textChannel.isConnected)")
the result is false.
Do you know what other steps I am missing for it to be connected?
Just learned that it was an issue of my namespace. It connects now.
Problem was the namespace wasn't matching the namespace from my receiver code.
fileprivate lazy var textChannel:TextChannel = {
return TextChannel(namespace: NAMESPACE)
}()
I am trying to retrieve all contacts and save them in VCard form (swift 4, XCode 9.0). But CNContactVCardSerialization.data(with:) always returns nil. Here is my code:
var contacts = [CNContact]()
let request = CNContactFetchRequest(keysToFetch:[CNContact.descriptorForAllComparatorKeys()])
do {
try contactsStore.enumerateContacts(with: request, usingBlock:
{ (contact:CNContact, result:UnsafeMutablePointer<ObjCBool>) in
self.contacts.append(contact)
})
}
catch {
}
// at this point all contacts are in the "contacts" array.
var data = Data()
do {
try data = CNContactVCardSerialization.data(with: contacts)
}
catch {
print("some error in contacts:" + String(describing: error));
}
print(">>>data:" + String(data.count))
Output:
2017-11-02 XXX [5224:449081]
Exception writing contacts to vCard (data): A property was not
requested when contact was fetched.
2017-11-02 XXX [5224:449362] XPC
connection interrupted
some error in contacts:nilError
>>>data:0
I red the question below but it does not help.
How to use method dataWithContacts in CNContactVCardSerialization?
I added "Privacy - Contacts Usage Description" into info.plist
Maybe you need to provide some specific keys to fetch?
UPD: Yep, if you want to fetch requests and serialize them, you have to set keys to fetch:
keysToFetch:#[[CNContactVCardSerialization descriptorForRequiredKeys]]
Change
let request = CNContactFetchRequest(keysToFetch:[CNContact.descriptorForAllComparatorKeys()])
To
let request = CNContactFetchRequest(keysToFetch:[CNContactVCardSerialization.descriptorForRequiredKeys()])
I need to get data from SOAP WebService in Swift https://codedump.io/share/JDHdwpOOmqne/1/swift-2-soap-web-service-call
I tried the procedures in the referred link but it was not working,Can any one help wit a working code.
You can use SWXML library for get data from soap web service this is easy to use and simple see the code below.
Alamofire.request(.GET, is_URL)
.responseJSON { response in
let xmls = SWXMLHash.parse(response.data!)
func enumerate(indexer: XMLIndexer, level: Int) {
for child in indexer.children {
let name:String? = child.element!.name
print("\(level) \(name)")
// Take Link from XML data here
if name! == "link" {
let text = child.element!.text
if text?.isEmpty == false{
print(text)
// Finish here Process
completion(result: text!)
}
}
enumerate(child, level: level + 1)
}
}
enumerate(xmls, level: 0)
}
}
And i having demo link also for this related code for Soap parsing Demo
How would I determine if a user is online/offline using XMPPFramework for iPhone?
I have their JID etc etc. Is there a way of requesting a presence or something?
Thank you.
Have you checked out the source code of the XMPPFramework sample project?
If I remember correctly, this should be the relevant code snippet:
// Subscribe to the buddy's presence
//
// <presence to="bareJID" type="subscribe"/>
NSXMLElement *presence = [NSXMLElement elementWithName:#"presence"];
[presence addAttributeWithName:#"to" stringValue:[jid bare]];
[presence addAttributeWithName:#"type" stringValue:#"subscribe"];
[xmppStream sendElement:presence];
And the callback your stream delegate gets should be
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence;
I assume that you already have the xmmpframework source, if not, you can clone the repository here
hg clone https://xmppframework.googlecode.com/hg/ xmppframework
The sample projects are in the "Xcode" folder.
First, Both side subscription in ROSTER List is must required for getting user status.
Here, How to subscribe & Accept a Presence Request.
let senderName = strReceiverEjabberedName + "#" + eJabVirtualHost
let roomJID = XMPPJID(string: senderName)
// Send subscribe request
let detxTag:XMLElement = XMLElement(name: "presence")
detxTag.addAttribute(withName: "to", stringValue: roomJID?.bare ?? "")
detxTag.addAttribute(withName: "type", stringValue: "subscribe")
xmppController.xmppStream.send(detxTag)
// Send subscribed request
let detxTaged:XMLElement = XMLElement(name: "presence")
detxTaged.addAttribute(withName: "to", stringValue: roomJID?.bare ?? "")
detxTaged.addAttribute(withName: "type", stringValue: "subscribed")
xmppController.xmppStream.send(detxTaged)
// Subscribe & Accept Presence Request
xmppController.xmppRoster?.subscribePresence(toUser: roomJID!)
xmppController.xmppRoster?.acceptPresenceSubscriptionRequest(from: roomJID!, andAddToRoster: true)
If your friend send precense request to you then you received request in Roster Delegate.
extension XMPPServiceController: XMPPRosterDelegate {
func xmppRoster(_ sender: XMPPRoster, didReceivePresenceSubscriptionRequest presence: XMPPPresence) {
sender.acceptPresenceSubscriptionRequest(from: presence.from!, andAddToRoster: true)
}
}
Once the above steps did you will receive your friend Online/Offline Status in the below XMPP delegate method.
func xmppStream(sender: XMPPStream?, didReceivePresence presence: XMPPPresence?) {
let presenceType = presence?.type
if presenceType == "available" { //ONLINE }
else if presenceType == "unavailable" { //OFFLINE }
}