I am using Socket.IO for my iOS chat application. Chat library called socket.io-client-swift is in Swift and i could manage to manually integrate it to my Objective-C project using bridging.
I imported 'Source' folder from above library into my Xcode and placed #import "MyProjectName-Swift.h" in my ChatViewController with code below:
//in viewDidLoad
NSURL* url = [[NSURL alloc] initWithString:#"http://localhost:3000/"];
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:url options:#{#"log": #YES, #"forcePolling": #YES}];
[socket on:#"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
NSLog(#"socket connected");
}];
[socket on:#"currentAmount" callback:^(NSArray* data, SocketAckEmitter* ack) {
double cur = [[data objectAtIndex:0] floatValue];
[socket emitWithAck:#"canUpdate" withItems:#[#(cur)]](0, ^(NSArray* data) {
[socket emit:#"update" withItems:#[#{#"amount": #(cur + 2.50)}]];
});
[ack with:#[#"Got your currentAmount, ", #"dude"]];
}];
[socket connect];
I am using node.js locally(http://localhost:3000/) by the help of this tutorial to send message from, and i can see it simultaneously in my Xcode console:
2016-05-13 14:59:20.345 CoreData_Chat[45303:372543] LOG SocketEnginePolling: Doing polling request
2016-05-13 14:59:24.033 CoreData_Chat[45303:373287] LOG SocketEnginePolling: Got polling response
2016-05-13 14:59:24.033 CoreData_Chat[45303:373287] LOG SocketEnginePolling: Doing polling request
2016-05-13 14:59:24.033 CoreData_Chat[45303:373285] LOG SocketEngine: Got message: 42["chat message","hii"]
2016-05-13 14:59:24.034 CoreData_Chat[45303:373285] LOG SocketIOClient: Should parse message: 2["chat message","hii"]
2016-05-13 14:59:24.034 CoreData_Chat[45303:373285] LOG SocketParser: Parsing 2["chat message","hii"]
2016-05-13 14:59:24.035 CoreData_Chat[45303:373285] LOG SocketParser: Decoded packet as: SocketPacket {type: 2; data: [chat message, hii]; id: -1; placeholders: -1; nsp: /}
2016-05-13 14:59:24.035 CoreData_Chat[45303:373285] LOG SocketIOClient: Handling event: chat message with data: (
hii)
So, how can i emit this message to show in my app and/or send message to server. There is no any clear tutorial explaining this, especially in Objective-C. Can anyone help? Thank you.
NSMutableDictionary *messageServer = [[NSMutableDictionary alloc]init];
[messageServer setObject:#"bet" forKey:userActivity];
[messageServer setObject:#"0" forKey:betMoney];
SocketIOClient emit:EVENTNAME withItems:#[messageServer];
Related
I in turn to the making relevant mailcore2 problems, based on past errors, and all of the share, or didn't find the solution to this problem below, so the problem I have encountered redistribution, also hope to get more help, thank you!
Mailcore2 using the code below:
self.imapSession = [[MCOIMAPSession alloc] init];
self.imapSession.hostname = hostname;
self.imapSession.port = 993;
self.imapSession.username = username;
self.imapSession.password = password;
if (oauth2Token != nil) {
self.imapSession.OAuth2Token = oauth2Token;
self.imapSession.authType = MCOAuthTypeXOAuth2;
}
self.imapSession.connectionType = MCOConnectionTypeTLS;
MasterViewController * __weak weakSelf = self;
self.imapSession.connectionLogger = ^(void * connectionID, MCOConnectionLogType type, NSData * data) {
#synchronized(weakSelf) {
if (type != MCOConnectionLogTypeSentPrivate) {
NSLog(#"event logged:%p %i withData: %#", connectionID, type, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
}
};
// Reset the inbox
self.messages = nil;
self.totalNumberOfInboxMessages = -1;
self.isLoading = NO;
self.messagePreviews = [NSMutableDictionary dictionary];
[self.tableView reloadData];
NSLog(#"checking account");
self.imapCheckOp = [self.imapSession checkAccountOperation];
[self.imapCheckOp start:^(NSError *error) {
MasterViewController *strongSelf = weakSelf;
NSLog(#"finished checking account.");
if (error == nil) {
[strongSelf loadLastNMessages:NUMBER_OF_MESSAGES_TO_LOAD];
} else {
NSLog(#"error loading account: %#", error);
}
strongSelf.imapCheckOp = nil;
}];
When I use iOS8.1 equipment, error message as follows, the console output:
checking account
2015-02-15 10:58:36.712 MailCoreTest[11971:2988194] event logged:0x166a4d50 0 withData: * OK [CAPABILITY IMAP4 IMAP4rev1 IDLE XAPPLEPUSHSERVICE ID UIDPLUS AUTH=LOGIN NAMESPACE] QQMail IMAP4Server ready
2015-02-15 10:58:36.821 MailCoreTest[11971:2988194] event logged:0x166a4d50 0 withData: (null)
2015-02-15 10:58:36.824 MailCoreTest[11971:2988128] finished checking account.
2015-02-15 10:58:36.825 MailCoreTest[11971:2988128] error loading account: Error Domain=MCOErrorDomain Code=5 "Unable to authenticate with the current session's credentials." UserInfo=0x16596ec0 {NSLocalizedDescription=Unable to authenticate with the current session's credentials.}
But when I use iOS8.0 devices, console to print the results are as follows:
checking account
2015-02-15 11:22:53.249 MailCoreTest[7853:1742121] finished checking account.
2015-02-15 11:22:53.249 MailCoreTest[7853:1742121] error loading account: Error Domain=MCOErrorDomain Code=1 "A stable connection to the server could not be established." UserInfo=0x17e50ab0 {NSLocalizedDescription=A stable connection to the server could not be established.}
Please help me, I contact with Objective - C time is short, but I must do my best to be aggressive, still hope to give a detailed solution, sincere thank you once again, hard work!
Yes its an authentication issue, I had same issue with my app but it was working fine before.
After I got this error, I checked my gmail account and there was an email from Google with subject "Suspicious Sign in prevented" and This sign in was of CA, USA. Actually My Application was under review so it was rejected for this reason.
Then I found that gmail is disabling unauthorised IPs.
You have to enable the 2 step verification for the gmail account to resolve this and generate an app specific password. Use that password in your app and It'll work fine.
Follow below google link to generate application specific password.
https://support.google.com/mail/answer/1173270?hl=en
In my iOS app I'm using RabbitMQ wrapper from https://github.com/bimawa/AMQProtocol
AMQPConnection *connection = [[AMQPConnection alloc] init];
NSError *error,*error1,*error2;
[connection connectToHost:server onPort:port error:&error];
[connection loginAsUser:login withPasswort:password onVHost:#"/" error:&error1];
AMQPChannel *channel = [connection openChannelError:&error2]; // Get error here!
On the last line I get the error "Failed to open a channel":
2014-06-27 11:17:06.094 MyApp[40055:60b] TCLib>> AMQPException AMQP_RESPONSE_LIBRARY_EXCEPTION: Failed to open a channel: Argument list too long
2014-06-27 11:17:06.107 MyApp[40055:60b] TCLib>> open channel error: Error Domain=AMQPChannel Code=-5 "Failed to open a channel" UserInfo=0x9abe330 {NSLocalizedDescription=Failed to open a channel}
Server, host, login and password are working. I've checked them on android app.
Can't find out why it fails to open a channel. Can you recommend something?
may be its too late for answer but may be it's helpful for other.:)
in my situation the problem was from i had error in login to 'vhost' or the
[connection loginAsUser:login withPasswort:password onVHost:#"/" error:&error1];
line and i was trying to open the channel of null connection .
the problem was permission check mechanism in rabbit server.
update:
for example check the error after each line that you pass it to a method like this:
if(error!=nil){
NSLog(#"%#",error);
return;
}
I'm using the iOS SDK to access download content from the content module. It seems in the delegate method -(void)completedWithResult:(Result*)result, I can have cases where result.success is YES while result.file would be nil.
-(void)completedWithResult:(Result*)result
{
if (result.success) { // YES here
if ([result isKindOfClass: [QBCFileDownloadTaskResult class]]) {
FileDownloadTaskResult *res = (QBCFileDownloadTaskResult *)result;
res.file; // This is NULL.
res.errors; // This is an empty NSArray
}
}
}
Console log prints this message:
<QBASIHTTPRequest: 0xc26d200>
headers:(null)
body:
error:Error Domain=QBASIHTTPRequestErrorDomain Code=1 "A connection failure occurred" UserInfo=0x9f27a50 {NSUnderlyingError=0x9f18300 "The operation couldn’t be completed. Connection reset by peer", NSLocalizedDescription=A connection failure occurred}
Is this normal? I would expect in this case res.success a NO.
Let's try latest version of iOS SDK 1.8
http://quickblox.com/developers/IOS#Framework_changelog:
Here is one big change:
replaced core network library ASIHTTPRequest with AFNetworking
I'm using the iOS DataStore API to upload data to StackMob. I get this error when I try to use my smclient initialized with my public key.
HTTP Code=401 "The operation couldn’t be completed. (HTTP error 401.)" UserInfo=0xa14dac0 {error=Insufficient authorization}
Sample code
[[self.smclient dataStore] createObject:eventDictObj
inSchema:#"EventSchema"
onSuccess:^(NSDictionary *object, NSString *schema)
{
NSLog(#"Created online event : %#", object);
successBlock();
}
onFailure:^(NSError *error, NSDictionary* object, NSString *schema)
{
failedBlock(error);
}];
And smclient is initialized as follows
self.smclient = [[SMClient alloc] initWithAPIVersion:#"0" publicKey:#"xxxxxxxxxx"];
For this use case I don't need to use the logged in user credentials to create this entry in StackMob
Make sure that the permissions are set to Open on your stack mob database.
It's been two days that I'm looking for a simple code for RestKit API for logging into a website without using JSON. Here is the code that I wrote so far:
- (void)login
{
[RKClient clientWithBaseURLString:#"http://Mywebsite.com/login.php"];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObject:#"user" forKey:#"username"];
[params setObject:#"pass" forKey:#"password"];
[params setObject:#"login" forKey:#"type"];
[[RKClient sharedClient] post:#"/login" params:params delegate:self];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
NSLog(#"HTTP status code: %d", response.statusCode);
NSLog(#"HTTP status message: %#", [response localizedStatusCodeString]);
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
NSRange range = [[error localizedDescription] rangeOfString:#"-1012"];
if (range.length > 0){
//Do whatever here to handle authentication failures
}
RKLogError(#"Hit error: %#", error);
}
Here is the log that I receive:
2012-09-08 21:44:14.633 RestKitTest[889:fb03] I restkit:RKLog.m:33 RestKit initialized...
2012-09-08 21:44:15.010 RestKitTest[889:fb03] I restkit.network.reachability:RKReachabilityObserver.m:369 Network availability has been determined for reachability observer <RKReachabilityObserver: 0x6c5a560 host=0.0.0.0 isReachabilityDetermined=YES isMonitoringLocalWiFi=NO reachabilityFlags=-R -----l->
2012-09-08 21:44:21.021 RestKitTest[889:fb03] I restkit.network:RKRequest.m:676 Status Code: 404
2012-09-08 21:44:21.036 RestKitTest[889:fb03] HTTP status code: 404
2012-09-08 21:44:21.090 RestKitTest[889:fb03] HTTP status message: not found
Any idea how I can fix this issue would be appreciated.
I'd say it 404s because you set baseURL as http://Mywebsite.com/login.php yet you POST as post:#"/login". Do you really want to access http://Mywebsite.com/login.php/login?
The baseURL should be set to a common prefix to all your backend calls, in your case http://Mywebsite.com, the resource itself should be posted as post:#"/login.php". The resource name and baseURL are concatenated before the request is sent.
You'll need to provide info on what login.php is doing. I don't see why it should 404 if the script url resolves correctly.
Also, if that PHP script does a redirect on success, you should use the RKClient method post:usingBlock: and inside that block do request.followRedirect = NO;. That way you'll get the correct status code, if you are returning one.
If may also help to log more of the response:
NSLog(#"Header fields: %#", response.allHeaderFields);
NSLog(#"Body: %#", response.bodyAsString);