I am integrating XMPP functionality in my ios app and i came across a problem i cannot solve.
The problem is i cannot get archived messages from the server. My client is able to log in and i have tested several service calls (send, receive messages, getting info about a user) with success.
Upon sending
<iq type='get' id='pref1'>
<pref xmlns='urn:xmpp:archive'/>
</iq>
The response is
SEND: <iq type="get"><pref xmlns="urn:xmpp:archive"/></iq>
RECV: <iq xmlns="jabber:client" type="error" to="1#iis2/ae76edc"><error code="501"
type="cancel"><feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-
stanzas"/</error></iq>
The server administrator is able to see the archived messages, as he activated archiving.
Must something be done server or client side in order to achieve this functionality? Could it be that seeing old messages and the server actually implementing and supporting XEP-0136, are two different things?
If you want fetch from server means use this code
internal var xmppMAM: XMPPMessageArchiveManagement?
func setupXMPPMam(){
xmppMAM = XMPPMessageArchiveManagement.init()
xmppMAM?.addDelegate(self, delegateQueue: .global(qos: .background))
// stream is XMPPStream
xmppMAM?.activate(stream)
}
call the setupMam function once XMPP connect
func retrieveArchiveMessage(){
let set = XMPPResultSet(max: totalCount)
xmppMAM?.retrieveMessageArchive(at: XMPPJID(string: user), withFields: nil, with: set)
}
func xmppStream(_ sender: XMPPStream, willReceive message: XMPPMessage) -> XMPPMessage? {
if let forwardedMessage = message.mamResult?.forwardedMessage{
debugPrint(forwardedMessage)
return message
}
}
if you using Robbiehanson framework above code is working perfectly for fetch value from server.
I hope this article is useful for you #Akash Thakkar
an example to get archived messages in Swift 4
declares and initializes the variables XMPPMessageArchivingCoreDataStorage where I initialize the XMPPStream
var xmppMessageStorage: XMPPMessageArchivingCoreDataStorage?
var xmppMessageArchiving: XMPPMessageArchiving?
xmppMessageStorage = XMPPMessageArchivingCoreDataStorage.sharedInstance()
xmppMessageArchiving = XMPPMessageArchiving(messageArchivingStorage: xmppMessageStorage)
xmppMessageArchiving?.clientSideMessageArchivingOnly = true
xmppMessageArchiving?.activate(stream)
xmppMessageArchiving?.addDelegate(self, delegateQueue: DispatchQueue.main)
doing this, whenever a message arrives, this will cause it to be archived without needing to do anything else.
then, to retrieve the archived message
func RecibedMessageArchiving(idFriend: String) {
let JabberIDFriend = idFriend //id friend chat, example test1#example.com
let moc = xmppMessageStorage?.mainThreadManagedObjectContext
let entityDescription = NSEntityDescription.entity(forEntityName: "XMPPMessageArchiving_Message_CoreDataObject", in: moc!)
let request = NSFetchRequest<NSFetchRequestResult>()
let predicateFormat = "bareJidStr like %# "
let predicate = NSPredicate(format: predicateFormat, JabberIDFriend)
request.predicate = predicate
request.entity = entityDescription
//jabberID id del usuario, cliente
var jabberIDCliente = ""
if let jabberj = globalChat.value(forKey: "jabberID"){
jabberIDCliente = jabberj as! String
}
do {
let results = try moc?.fetch(request)
for message: XMPPMessageArchiving_Message_CoreDataObject? in results as? [XMPPMessageArchiving_Message_CoreDataObject?] ?? [] {
var element: DDXMLElement!
do {
element = try DDXMLElement(xmlString: (message as AnyObject).messageStr)
} catch _ {
element = nil
}
let body: String
let sender: String
let date: NSDate
let isIncomings: Bool
if message?.body != nil {
body = (message?.body)!
} else {
body = ""
}
if element.attributeStringValue(forName: "to") == JabberIDFriend {
sender = jabberIDCliente
isIncomings = false
} else {
sender = "test2#example.com"
isIncomings = true
}
var m: [AnyHashable : Any] = [:]
m["msg"] = message?.body
print("body", message?.body)
print("test", element.attributeStringValue(forName: "to"))
print("test2", element.attributeStringValue(forName: "body"))
}
} catch _ {
//catch fetch error here
}
}
Related
Error Domain=MCOErrorDomain Code=5 "Unable to authenticate with the current session's credentials." UserInfo={NSLocalizedDescription=Unable to authenticate with the current session's credentials.}
I put this code in my project.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
prepareImapSession()
}
var imapsession:MCOIMAPSession = MCOIMAPSession()
var error : Error? = nil
func prepareImapSession()
{
// CONFIGURE THAT DEPENDING OF YOUR NEEDS
imapsession.hostname = "imap.gmail.com" // String
imapsession.username = my email // String
imapsession.password = password // String
imapsession.port = 993 // UInt32 number
imapsession.authType = MCOAuthType.saslLogin
imapsession.connectionType = MCOConnectionType.TLS
DispatchQueue.main.asyncAfter(deadline: .now() + 8) { [self] in
self.useImapWithUIDS()
}
imapsession.connectOperation()
}
func useImapWithUIDS() {
// There is more than one option here, explore depending of your needs
// let kind = MCOIMAPMessagesRequestKind()
// let headers = kind.union(MCOIMAPMessagesRequestKind.headers)
// let request = headers.union(MCOIMAPMessagesRequestKind.flags)
let requestKind: MCOIMAPMessagesRequestKind = [.headers, .flags]
let folder : String = "INBOX"
// HERE ALSO EXPLORE DEPENDING OF YOUR NEEDS, RANGE IT IS THE RANGE OF THE UIDS THAT YOU WANT TO FETCH, I SUGGEST TO YOU TO CHANGE THE // NUMBER ONE IF YOU HAVE A LOWER BOUND TO FETCH EMAIL
let uids : MCOIndexSet = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))
let fetchOperation = imapsession.fetchMessagesOperation(withFolder: folder, requestKind: requestKind, uids: uids)
fetchOperation?.start
{ [self] (err, msg, vanished) -> Void in
if (err != nil)
{
self.error = err
NSLog((err?.localizedDescription)!)
}
else
{
guard let msgs = msg as? [MCOIMAPMessage]
else
{
print("ERROR GETTING THE MAILS")
return
}
for i in 0..<msgs.count
{
// THE SUBJECT
let subject = msgs[i].header.subject
// THE uid for this email. The uid is unique for one email
let uid = msgs[i].uid
self.useImapFetchContent(uidToFetch: uid)
// The sequenceNumber like the nomber say it is the sequence for the emails in the INBOX from the first one // (sequenceNumber = 1) to the last one , it not represent always the same email. Because if you delete one email then //next one will get the sequence number of that email that was deleted
let sequenceNumber = msgs[i].sequenceNumber
}
}
}
}
// MARK: - EXTRACT THE CONTENT OF ONE EMAIL, IN THIS FUNCTION YOU NEED THE uid, THE UNIQUE NUMBER FOR ONE EMAIL
func useImapFetchContent(uidToFetch uid: UInt32) {
let operation: MCOIMAPFetchContentOperation = imapsession.fetchMessageOperation(withFolder: "INBOX", uid: uid)
operation.start { (Error, data) in
if (Error != nil)
{
NSLog("ERROR")
return
}
let messageParser: MCOMessageParser = MCOMessageParser(data: data)
// IF YOU HAVE ATTACHMENTS USE THIS
let attachments = messageParser.attachments() as? [MCOAttachment]
// THEN YOU NEED THIS PROPERTIE, IN THIS EXAMPLE I TAKE THI FIRST, USE WHAT EVER YOU WANT
let attachData = attachments?.first?.data
// FOR THE MESSAGEPARSER YOU CAN EPLORE MORE THAN ONE OPTION TO OBTAIN THE TEXT
let msgPlainBody = messageParser.plainTextBodyRendering()
}
}
}
I using the mailcore2 framework. I got error description Unable to authenticate with the current session's credentials.
It can be related with 2nd factor authentication on your account. It was described on Apple forum.
I am developing a chatting application where I could receive a number of messages at a time which leads to app freezing. Following is my socket receiver:
func receiveNewDirectMessages() {
self.socket?.on(EventListnerKeys.message.rawValue, callback: { (arrAckData, ack) in
print_debug(arrAckData)
guard let dictMsg = arrAckData.first as? JSONDictionary else { return }
guard let data = dictMsg[ApiKey.data] as? JSONDictionary else { return }
guard let chatData = data[ApiKey.data] as? JSONDictionary else { return }
guard let messageId = chatData[ApiKey._id] as? String , let chatId = chatData[ApiKey.chatId] as? String else { return }
if MessageModel.getMessageModel(msgId: messageId) != nil { return }
let isChatScreen = self.isChatScreen
let localMsgId = "\(arc4random())\(Date().timeIntervalSince1970)"
if let senderInfo = data[ApiKey.senderInfo] as? JSONDictionary, let userId = senderInfo[ApiKey.userId] as? String, userId != User.getUserId() {
_ = AppUser.writeAppUserModelWith(userData: senderInfo)
}
let msgModel = MessageModel.saveMessageData(msgData: chatData, localMsgId: localMsgId, msgStatus: 2, seenByMe: false)
let chatModel = ChatModel.saveInboxData(localChatId: msgModel.localChatId, inboxData: chatData)
if isChatScreen {
self.emitMessageStatus(msgId: messageId, chatId: chatId, socketService: .messageStatus, status: .delivered)
self.emitMessageStatus(msgId: messageId, chatId: chatId, socketService: .messageStatus, status: .seen)
} else {
ChatModel.updateUnreadCount(localChatId: chatModel.localChatId, incrementBy: 1)
self.emitMessageStatus(msgId: messageId, chatId: chatId, socketService: .messageStatus, status: .delivered)
}
TabController.shared.updateChatBadgeCount()
})
}
What's happening above:
1. I am receiving all the undelivered messages ONE-By-ONE in this socket listener.
2. Fetching the message data
3. Saving the received sender's info to Realm DB
4. Saving the message model to realm DB
5. SAVING/UPDATING Chat Thread in realm DB
6. Emitting acknowledgement for the received message
7. Update Chat badge count on tab bar
Below is my emitter for acknowledging the message delivery.
func emitMessageStatus(msgId: String, chatId: String, socketService: SocketService, status: MessageStatusAction) {
// Create Message data packet to be sent to socket server
var msgDataPacket = [String: Any]()
msgDataPacket[ApiKey.type] = socketService.type
msgDataPacket[ApiKey.actionType] = socketService.listenerType
msgDataPacket[ApiKey.data] = [
ApiKey.messageId: msgId,
ApiKey.chatId: chatId,
ApiKey.userId: User.getUserId(),
ApiKey.statusAction: status.rawValue
]
// send the messsage data packet to socket server & wait for the acknowledgement
self.emit(with: EventListnerKeys.socketService.rawValue, msgDataPacket) { (arrAckData) in
print_debug(arrAckData)
guard let dictMsg = arrAckData.first as? JSONDictionary else { return }
if let msgData = dictMsg[ApiKey.data] as? [String: Any] {
// Update delivered Seen Status here
if let msgId = msgData[ApiKey.messageId] as? String, let actionType = msgData[ApiKey.statusAction] as? String, let msgStatusAction = MessageStatusAction(rawValue: actionType) {
switch msgStatusAction {
case .delivered:
if let deliveredTo = msgData[ApiKey.deliveredTo] as? [[String: Any]] {
_ = MessageModel.updateMsgDelivery(msgId: msgId, deliveredTo: deliveredTo)
}
case .seen:
if let seenBy = msgData[ApiKey.seenBy] as? [[String: Any]] {
_ = MessageModel.updateMsgSeen(msgId: msgId, seenBy: seenBy)
}
case .pin:
MessageModel.clearPinnedMessages(chatId: chatId)
if let pinTime = msgData[ApiKey.pinTime] as? Double {
MessageModel.updatePinnedStatus(msgId: msgId, isPinned: true, pinTime: pinTime)
}
case .unPin:
if let pinTime = msgData[ApiKey.pinTime] as? Double {
MessageModel.updatePinnedStatus(msgId: msgId, isPinned: false, pinTime: pinTime)
}
case .delete:
MessageModel.deleteMessage(msgId: msgId)
case .ackMsgStatus, .like, .unlike:
break
}
}
}
}
}
What's happening above:
Encapsulating all the related information to acknowledge the event
Update realm DB after acknowledgement delivery
Now, I'm not able to defies a perfect threading policy here. What to write in background thread and what should I write in Main thread. I tried doing it however but that leades to random crashes or packet lossses.
Can anyone please lead me forward on this topic. I will be highly grateful.
Try to use background thread for data processing/ non-UI processing.
Reduce number of updating UI times
Instead of processing 1 by 1 message, using debounce - like. You can store new messages, then update UI with n new messages. So instead of updating UI/saving data to db 100 times for 100 messages, you can do it 1 time for 100 messages. More detail: with every new message, add it to an array. Call debouncer. The Debouncer will delay a function call, and every time it's getting called it will delay the preceding call until the delay time is over. So after e.g 200ms, if there're no new message, the update func will be call (the callback func that debounce's handling). Then update ui/db with n new stored messages.
You can group message by time, like group by 1 hour. And then update with delay between each time group. You can do it when the debouncer is called -> group messages by time -> update db/ui by each group. You can use setTimeout, like update group 1, 100ms later update group 2, so the ui won't be freezed
I have checked all the tutorial and also did a lot of R & D on gateway integration.
But didn't find a way to integrate paytm payment gateway.
func paymentConfiguration()
{
var orderDict = [AnyHashable: Any]()
orderDict["MID"] = "WorldP64425807474247"
orderDict["CHANNEL_ID"] = "WAP"
orderDict["INDUSTRY_TYPE_ID"] = "Retail"
orderDict["WEBSITE"] = "worldpressplg"
orderDict["TXN_AMOUNT"] = "1"
orderDict["ORDER_ID"] = ViewController.generateOrderID(withPrefix: "")
orderDict["CALLBACK_URL"] = "https://securegw.paytm.in/theia/paytmCallback?ORDER_ID=<ORDER_ID>"
orderDict["CHECKSUMHASH"] = "w2QDRMgp1/BNdEnJEAPCIOmNgQvsi+BhpqijfM9KvFfRiPmGSt3Ddzw+oTaGCLneJwxFFq5mqTMwJXdQE2EzK4px2xruDqKZjHupz9yXev4="
orderDict["REQUEST_TYPE"] = "DEFAULT"
orderDict["CUST_ID"] = "1234567890"
var order = PGOrder(params: orderDict)
}
func openPaytmController()
{
PGServerEnvironment.selectServerDialog(view, completionHandler: {(_ type: ServerType) -> Void in
var txnController = PGTransactionViewController.initTransaction(forOrder: order)
if type != eServerTypeNone {
txnController.serverType = type
txnController.merchant = mc
txnController.delegate = self
self.show(txnController)
}
})
}
Any help would be greatly appreciated.
Thanks in advance
**
PayTM Integaration with swift with detail
**
##Download paytm sdk##
https://github.com/Paytm-Payments/Paytm_iOS_App_Kit
Make sure the dynamic lib and systemConfiguration.framwork is added in “Linked binaries and frameworks”
Add bridging header file into the project
#import "PaymentsSDK.h"
You have to generate CheckSumHash key - PayTm unique key for transaction purpose
Pass the below parameters to generate checkSumHash
let params:[String: Any] = [
"CUST_ID”:<Your Customer ID>, // you have to generate unique customer ID (Generate random string - less than 50 length count )
"TXN_AMOUNT":"10.00", // sample amount
“MID": <Your merchant ID>,
"ORDER_ID”:<Your Order ID>, // you have to generate unique order ID (Generate random string - less than 50 length count )
"INDUSTRY_TYPE_ID":"Retail", //Staging Environment
"CHANNEL_ID":"WAP", //Staging Environment
"WEBSITE":"APPSTAGING", //Staging Environment - Mobile
"CALLBACK_URL":"https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID=\(<Your Order ID>)” // This should be important one and make sure the correct order ID.
]
In your server , You have to setUp the the file that should generate CheckSumHash key based on the given parameters and merchant key (Which is stored in the file; Don’t use inside your app ). That should be your CHECKSUM URL along with the above mentioned parameter. And finally, we get the CheckSumHash in the response
You have to given the above mentioned parameters along with checkSumHash (which you got the response - refer :step 3) to imitate PGTransactionViewCOntroller
let params:[String: Any] = [
"CUST_ID”:<Your Customer ID>, // you have to generate unique customer ID (Generate random string - less than 50 length count )
"TXN_AMOUNT":"10.00", // sample amount
“MID": <Your merchant ID>,
"ORDER_ID”:<Your Order ID>, // you have to generate unique order ID (Generate random string - less than 50 length count )
"INDUSTRY_TYPE_ID":"Retail", //Staging Environment
"CHANNEL_ID":"WAP", //Staging Environment
"WEBSITE":"APPSTAGING", //Staging Environment - Mobile
"CALLBACK_URL":"https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID=\(<Your Order ID>)” // This should be important one and make sure the correct order ID. ,“CHECKSUMHASH”:<your geenrated CheckSumHash key> // which you got the response of generate CheckSumHash
]
let order = PGOrder(params: params)
let txnController = PGTransactionViewController(transactionFor: order)
txnController?.serverType = eServerTypeStaging
txnController?.merchant = PGMerchantConfiguration.default()
txnController?.merchant.checksumGenerationURL = CheckSumGenerationURL
txnController?.merchant.merchantID = "FlotaS90100524961231"
txnController?.merchant.checksumValidationURL = CheckSumVerifyURL + orderID
txnController?.loggingEnabled = true
txnController?.merchant.website = "APPSTAGING"
txnController?.merchant.industryID = "Retail"
txnController?.serverType = eServerTypeStaging
txnController?.delegate = self
self.navigationController?.pushViewController(txnController!, animated: true)
PayTM delegates to handle response
func didSucceedTransaction(controller: PGTransactionViewController, response: [NSObject : AnyObject]) {
print(response)
}
func didFinishedResponse(_ controller: PGTransactionViewController!, response responseString: String!) {
print(responseString) // Response will be in string
let data = responseString.data(using: .utf8)!
let obj = JSON(data: data)
if obj["STATUS"].stringValue != "TXN_SUCCESS" {
//handle what you want
}
}
}
func didFailTransaction(_ controller: PGTransactionViewController!, error: Error!, response: [AnyHashable : Any]!) {
print(error)
}
func didCancelTrasaction(_ controller: PGTransactionViewController!) {
print("User camcelled the trasaction")
controller.navigationController?.popViewController(animated: true)
}
func errorMisssingParameter(_ controller: PGTransactionViewController!, error: Error!) {
print(error.localizedDescription)
controller.navigationController?.popViewController(animated: true)
}
Try this code:
func showController(controller: PGTransactionViewController) {
if self.navigationController != nil {
self.navigationController?.pushViewController(controller, animated: true)
} else {
self.present(controller, animated: true, completion: nil)
}
}
func removeController(controller: PGTransactionViewController) {
if self.navigationController != nil {
self.navigationController?.popViewController(animated: true)
} else {
controller.dismiss(animated: true, completion: nil)
}
}
//Creat Payment----------------
func creatPayment(CheckSum: String) {
let mc = PGMerchantConfiguration.default()!
var orderDict = [String : Any]()
orderDict["MID"] = "WorldP64425807474247";
orderDict["ORDER_ID"] = ViewController.generateOrderID(withPrefix: "");
orderDict["CUST_ID"] = "1234567890";
orderDict["INDUSTRY_TYPE_ID"] = "Retail";
orderDict["CHANNEL_ID"] = "WAP";
orderDict["TXN_AMOUNT"] = self.FINAL_AMOUNT;
orderDict["WEBSITE"] = "APP_STAGING";
orderDict["CALLBACK_URL"] = "https://pguat.paytm.com/paytmchecksum/paytmCallback.jsp";
orderDict["CHECKSUMHASH"] = CheckSum;
let pgOrder = PGOrder(params: orderDict )
let transaction = PGTransactionViewController.init(transactionFor: pgOrder)
transaction!.serverType = eServerTypeStaging
transaction!.merchant = mc
transaction!.loggingEnabled = true
transaction!.delegate = self
self.showController(controller: transaction!)
}
func didFinishedResponse(_ controller: PGTransactionViewController!, response responseString: String!) {
print(responseString)
}
func didCancelTrasaction(_ controller: PGTransactionViewController!) {
print("CANCELLED")
}
func errorMisssingParameter(_ controller: PGTransactionViewController!, error: Error!) {
self.removeController(controller: controller)
print(error)
}
You have given the checksum as hardcore so, when the paytm decode your checksum and comapare with your parameters, the comparison will get fail. so you need to provide a new checksum when ever you make a payment.
The main thing you need to be care is that, the given parameters for checksum generation and payment setup should be same.
Checksum can be generated in the backend with the help of checksum generation kit provided by paytm
you should call the func creatPayment(CheckSum: String) in success of Generate checksumApi and give the checksum to the argument in the function
In Swift 5.0 Follow the following steps:-
1:- Follow these steps to download and import the library in your project:-
Download SDK from:-
https://github.com/Paytm-Payments/Paytm_iOS_App_Kit
Open your project in XCode and from the File menu, select Add files to
"your project"
Select Paytm.framework in the directory you just unzipped
Make sure 'Copy items if needed' is checked and click 'Add' Under
"Link Binary With Libraries" in the "Build Phases" tab of your
project settings, add SystemConfiguration.framework
Check if PaytmSDK.framework is added in both “Link Binary With
Libraries” and “Embedded Binaries”. If not, add by clicking on the plus icon.
Import PaytmSDK to ViewController
import PaymentSDK
Generate ChecksumHash
Security parameters to avoid tampering. Generated using server-side checksum utility provided by Paytm. Merchant has to ensure that this always gets generated on the server. Utilities to generate checksum hash is available
Add code on your payment ViewController to execute the payment process
var txnController = PGTransactionViewController()
var serv = PGServerEnvironment()
var params = [String:String]()
var order_ID:String?
var cust_ID:String?
func beginPayment() {
serv = serv.createStagingEnvironment()
let type :ServerType = .eServerTypeStaging
let order = PGOrder(orderID: "", customerID: "", amount: "", eMail: "", mobile: "")
order.params = ["MID": "rxazcv89315285244163",
"ORDER_ID": "order1",
"CUST_ID": "cust123",
"MOBILE_NO": "7777777777",
"EMAIL": "username#emailprovider.com",
"CHANNEL_ID": "WAP",
"WEBSITE": "WEBSTAGING",
"TXN_AMOUNT": "100.12",
"INDUSTRY_TYPE_ID": "Retail",
"CHECKSUMHASH": "oCDBVF+hvVb68JvzbKI40TOtcxlNjMdixi9FnRSh80Ub7XfjvgNr9NrfrOCPLmt65UhStCkrDnlYkclz1qE0uBMOrmuKLGlybuErulbLYSQ=",
"CALLBACK_URL": "https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID=order1"]
self.txnController = self.txnController.initTransaction(for: order) as! PGTransactionViewController
self.txnController.title = "Paytm Payments"
self.txnController.setLoggingEnabled(true)
if(type != ServerType.eServerTypeNone) {
self.txnController.serverType = type;
} else {
return
}
self.txnController.merchant = PGMerchantConfiguration.defaultConfiguration()
self.txnController.delegate = self
self.navigationController?.pushViewController(self.txnController, animated: true)
}
Change "CHECKSUMHASH": value in params that comes from API
order.params["CHECKSUMHASH"] = checkSum // API checkSum value
confirm the protocol-delegate of ‘PGTransactionDelegate’ to Handle error and success responses
To handle success/errors on completion of payment, implement "didFinishedResponse", "didCancelTrasaction", "errorMisssingParameter" methods of the "PGTransactionDelegate". Code snippet provided below:-
extension PaymentViewController : PGTransactionDelegate {
//this function triggers when transaction gets finished
func didFinishedResponse(_ controller: PGTransactionViewController, response responseString: String) {
let msg : String = responseString
var titlemsg : String = ""
if let data = responseString.data(using: String.Encoding.utf8) {
do {
if let jsonresponse = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any] , jsonresponse.count > 0{
titlemsg = jsonresponse["STATUS"] as? String ?? ""
}
} catch {
debugLog("Something went wrong")
}
}
let actionSheetController: UIAlertController = UIAlertController(title: titlemsg , message: msg, preferredStyle: .alert)
let cancelAction : UIAlertAction = UIAlertAction(title: "OK", style: .cancel) {
action -> Void in
controller.navigationController?.popViewController(animated: true)
}
actionSheetController.addAction(cancelAction)
self.present(actionSheetController, animated: true, completion: nil)
}
//this function triggers when transaction gets cancelled
func didCancelTrasaction(_ controller : PGTransactionViewController) {
controller.navigationController?.popViewController(animated: true)
}
//Called when a required parameter is missing.
func errorMisssingParameter(_ controller : PGTransactionViewController, error : NSError?) {
controller.navigationController?.popViewController(animated: true)
}
}
Hi all im trying to create an ios app that sync data using Realm Object server
but there is something im missing
i installed corretly Realm object server on my server (remote)
i can access the dashboard correctly
i can login with Syncuser
but i have problem syncing data.. when i open the app i login the user but after that ( im unable to sync anyting )
i searched online for a full example but without success
any one can suggest a simple and easy example for use realm sync with swift?
Thanks
This is my actual implementation for a chat app using RealmSwift. I used a model object called Message defined below:
class Message: Object {
dynamic var userId = ""
dynamic var name = ""
dynamic var text = ""
dynamic var dateCreated = NSDate()
}
Next I used created a controller for sending and sync messages:
// Instance variables
var messages: Results<Message>!
var token: NotificationToken!
var configuration: Realm.Configuration!
override func viewDidLoad() {
super.viewDidLoad()
// Your viewDidLoad implementation
self.setupRealm()
}
Next the setup methods for Realm. As mentioned in Real Documentation, if a Realm has read-only permissions, then you must use the asyncOpen API as described in Asynchronously Opening Realms. Opening a read-only Realm without asyncOpen will cause an error.
private func setupRealm() {
let serverURL = URL(string: "http://your.server.ip:9080")!
let credentials = SyncCredentials.usernamePassword(username: "example#mail.com", password: "password")
SyncUser.logIn(with: credentials, server: serverURL) { user, error in
if let user = user {
let syncURL = URL(string: "realm://your.server.ip:9080/~/Message")!
let syncConfig = SyncConfiguration(user: user, realmURL: syncURL)
self.configuration = Realm.Configuration(syncConfiguration: syncConfig)
self.setupDataSource()
} else if let error = error {
// handle error
debugPrint("error: \(error.localizedDescription)")
}
}
}
private func setupDataSource() {
Realm.asyncOpen(configuration: self.configuration) { realm, error in
if let realm = realm {
// Realm successfully opened, with all remote data available
self.messages = realm.objects(Message.self).sorted(byKeyPath: "dateCreated", ascending: true)
self.token = self.messages.addNotificationBlock({ (changes: RealmCollectionChange) in
debugPrint("Message update event")
self.collectionView.reloadData()
})
} else if let error = error {
// Handle error that occurred while opening or downloading the contents of the Realm
debugPrint("error: \(error.localizedDescription)")
}
}
}
deinit {
self.token?.stop()
}
Finally the sendMessage() method that I use to send the message:
func sendMessage(id: String, name: String, text: String) {
let message = Message()
message.userId = id
message.name = name
message.text = text
message.dateCreated = NSDate()
Realm.asyncOpen(configuration: self.configuration) { realm, error in
if let realm = realm {
// Realm successfully opened, with all remote data available
try! realm.write() {
realm.add(message)
}
} else if let error = error {
// Handle error that occurred while opening or downloading the contents of the Realm
debugPrint("error: \(error.localizedDescription)")
}
}
}
Hope it helps!
Bye!
I am receiving up to four push notifications for each event I am subscribed to. I have gone through everything related to my CloudKit subscriptions and notification registry and I am convinced this is an Apple problem. I have instead turned my attention toward correctly processing the notifications no matter how many I receive. Here is a simplified version of what I am doing:
func recievePrivatePush(_ pushInfo: [String:NSObject], completion: #escaping ()->Void) {
let notification = CKNotification(fromRemoteNotificationDictionary: pushInfo)
let alertBody = notification.alertBody
if let queryNotification = notification as? CKQueryNotification {
let recordID = queryNotification.recordID
guard let body = queryNotification.alertBody else {
return
}
if recordID != nil {
switch body {
case "Notification Type":
let id = queryNotification.recordID
switch queryNotification.queryNotificationReason {
case .recordCreated:
DataCoordinatorInterface.sharedInstance.fetchDataItem(id!.recordName, completion: {
//
})
break
default:
break
}
}
}
}
}
The fetching code looks something like this:
func fetchDataItem(_ id: String, completion: #escaping ()-> Void) {
if entityExistsInCoreData(id) {return}
let db = CKContainer.default().privateCloudDatabase
let recordID = CKRecordID(recordName: id)
db.fetch(withRecordID: recordID) { (record, error) in
if let topic = record {
//Here I create and save the object to core data.
}
completion()
}
}
All of my code works, the problem I am having is that when I receive multiple notifications, multiple fetch requests are started before the first core data entity is created, resulting in redundant core data objects.
What I would like to do is find a way to add the fetch requests to a serial queue so they are processed one at a time. I can put my request calls in a serial queue, but the callbacks always run asynchronously, so multiple fetch requests are still make before the first data object is persisted.
I have tried using semaphores and dispatch groups with a pattern that looks like this:
let semaphore = DispatchSemaphore(value: 1)
func recievePrivatePush(_ pushInfo: [String:NSObject], completion: #escaping ()->Void) {
_ = semaphore.wait(timeout: .distantFuture)
let notification = CKNotification(fromRemoteNotificationDictionary: pushInfo)
let alertBody = notification.alertBody
if let queryNotification = notification as? CKQueryNotification {
let recordID = queryNotification.recordID
guard let body = queryNotification.alertBody else {
return
}
if recordID != nil {
switch body {
case "Notification Type":
let id = queryNotification.recordID
switch queryNotification.queryNotificationReason {
case .recordCreated:
DataCoordinatorInterface.sharedInstance.fetchDataItem(id!.recordName, completion: {
semaphore.signal()
})
break
default:
break
}
}
}
}
}
Once the above function is called for the second time, and semaphore.wait is called, the execution of the first network request pauses, resulting in a frozen app.
Again, what I would like to accomplish it adding the asynchronous network requests to a queue so that they are made only one at a time i.e. the first network call is completed before the second request is started.
Carl,
Perhaps you'll find your solutions with dispatch groups, a few key expressions to look into.
let group = DispatchGroup()
group.enter()
... code ...
group.leave
group.wait()
I use them to limit the number of http requests I send out in a batch, to wait for the response. Perhaps you could use them together with the suggestion in my comment. Watch this video too, dispatch groups in here, I think more.
https://developer.apple.com/videos/play/wwdc2016/720/
These simple classes helped me solve the problem.
class PushQueue {
internal var pushArray: Array<String> = [String]()
internal let pushQueue = DispatchQueue(label: "com.example.pushNotifications")
public func addPush(_ push: Push) {
pushQueue.sync {
if pushArray.contains(push.id) {
return
} else {
pushArray.append(push.id)
processNotification(push: push)
}
}
}
internal func processNotification(push: Push) {
PushInterface.sharedInstance.recievePrivatePush(push.userInfo as! [String: NSObject])
}
}
class CKPush: Equatable {
init(userInfo: [AnyHashable: Any]) {
let ck = userInfo["ck"] as? NSDictionary
let id = ck?["nid"] as? String
self.id = id!
self.userInfo = userInfo
}
var id: String
var userInfo: [AnyHashable:Any]
public static func ==(lhs: CKPush, rhs: CKPush) -> Bool {
return lhs.id == rhs.id ? true : false
}
}
Please ignore the sloppy force unwraps. They need to be cleaned up.