message read or not iphone - iphone

how can i detect that received sms is read or not by user, i am using CTMessageCenter header also pleas help me..
and also tell me how can i put my application in background continuously....

i think there is no method from which we can found that message is read or not in non jailbreak ...

Ya you can read your incoming message and send also.In your main.m write this code and use three files:CoreTelephony.h,CTMessage.h,CTMessageCenter.h
One problem with this using this way you can't submit your app on app store.
static void callback(CFNotificationCenterRef center, void *observer, NSString* name, const void *object, NSDictionary* info) {
fprintf(stderr, "Notification intercepted: %s\n", [name UTF8String]);
if([name isEqualToString:#"kCTMessageReceivedNotification"] && info)
{
NSNumber* messageType = [info valueForKey:#"kCTMessageTypeKey"];
if([messageType isEqualToNumber:[NSNumber numberWithInt:1]])
{
NSNumber* messageID = [info valueForKey:#"kCTMessageIdKey"];
CTMessageCenter* mc = [CTMessageCenter sharedMessageCenter];
CTMessage* msg = [mc incomingMessageWithId:[messageID intValue]];
NSObject<CTMessageAddress>* phonenumber = [msg sender];
NSString *senderNumber = (NSString*)[phonenumber canonicalFormat];
NSString *sender = (NSString*)[phonenumber encodedString];
CTMessagePart* msgPart = [[msg items] objectAtIndex:0]; //for single-part msgs
NSData *smsData = [msgPart data];
NSString *smsText = [[NSString alloc] initWithData:smsData encoding:NSUTF8StringEncoding];
fprintf(stderr, "SMS Message from %s / %s: \"%s\"\n",[senderNumber UTF8String],[sender UTF8String],[smsText UTF8String]);
}
}
return;
}
int main(int argc, char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
id ct = CTTelephonyCenterGetDefault();
CTTelephonyCenterAddObserver(ct, NULL, callback, NULL, NULL, CFNotificationSuspensionBehaviorHold);
// Start the run loop. Now we'll receive notifications.
[[NSRunLoop currentRunLoop] run];
NSLog(#"you are in main thread");
[pool drain];
printf("Unexpectedly back from CFRunLoopRun()!\n");
[pool release];
}

Related

NSString * to Char[] (GameCenter Send Struct)

I want to send a struct to another player in GameCenter.
I have read the other questions about this, however, I cannot get any of them to work.
I need to get #"1234" into a char[4] (ex char[0] = '1', char[1] = '2', etc)
I have tried [NSString UTF8String], but it doesn't seem to do what I want.
It assigns fine, but when I pull it back into NSString * with [NSString stringWithUTF8String:], It returns blank.
If someone could show me the conversion to and from, it would be greatly appreciated.
Thanks.
EDIT:
I can't get it to work :/ Here is my code (the abridged version):
Matchmaker.h
enum { NChars = 4 };
typedef struct {
MessageType messageType;
} Message;
typedef struct {
Message message;
char code[NChars];
} MessageGameCode;
#interface Matchmaker : CCLayer <GameCenterMasterDelegate>{
NSString *_code;
}
#property (nonatomic,retain) NSString *_code;
Matchmaker.m
#synthesize _code;
-(void)viewDidLoad{
self._code = #"1234";
}
- (void)sendCode {
NSLog(#"Sending Code....");
MessageGameCode message;
message.message.messageType = kMessageTypeGameCode;
NSString * const source = self._code;
const char* const sourceAsUTF8 = source.UTF8String;
for (size_t idx = 0; idx < NChars; ++idx) {
message.code[idx] = sourceAsUTF8[idx];
}
NSData *data = [NSData dataWithBytes:&message length:NChars];
[self sendData:data];
}
- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {
Message *message = (Message *) [data bytes];
if (message->messageType == kMessageTypeGameCode) {
MessageGameCode *codeMessage = (MessageGameCode *)[data bytes];
self._code = [[NSString alloc] initWithBytes:codeMessage->code length:NChars encoding:NSUTF8StringEncoding];
[self setGameState:kGameStateWaitingForStart];
NSLog(#"Game Code Recieved");
NSLog(#"Recieved Code: %#",self._code); //This always shows self._code as blank
}
}
Your attempt will fail because the cstring which you pass to + [NSString stringWithUTF8String:] is not terminated.
Try this:
NSString * result = [[NSString alloc] initWithBytes:bytes
length:4
encoding:NSUTF8StringEncoding];
Edit
A more complete demonstration:
enum { NChars = 4 };
/* requires error checking */
void transmit() {
NSString * const source = #"1234";
char tmp[NChars] = { 0 };
const char* const sourceAsUTF8 = source.UTF8String;
for (size_t idx = 0; idx < NChars; ++idx) {
tmp[idx] = sourceAsUTF8[idx];
}
/* .. */
}
/* requires error checking */
void receive(const char bytes[NChars]) {
NSString * result = [[NSString alloc] initWithBytes:bytes length:NChars encoding:NSUTF8StringEncoding];
/* ... */
}
One way is
char bytes[4];
NSData* data = [#"1234" dataUsingEncoding: NSUTF8StringEncoding];
if ([data length] <= 4)
{
memcpy(bytes, [data bytes], [data length]);
}
And to go the other way:
NSString* recodedString = [[NSString alloc] initWithBytes: bytes
length: savedLengthFromBefore
encoding: NSUTF8StringEncoding];
There are a couple of possible pitfalls here.
One is that with [NSString UTF8String] "The returned C string is automatically freed just as a returned object would be released; you should copy the C string if it needs to store it outside of the autorelease context in which the C string is created.". So, depending on how long you're expecting the value to stick around you may need to copy it (for example, using strcpy)
The other issue is that [NSString UTF8String] and [NSString stringWithUTF8String:] both expect NULL-terminated C strings, so you need a char[5], not a char[4] to hold #"1234".

NSMutableString append data error

I have AsyncSocket like this.
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
NSString *message = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
[data getBytes:&tdata];
if (tdata > 5) {
if(bheader){
if(!charS){
if([message isEqualToString:#"S"]){
CMSG = message;
charS=YES;
}
}
else{
NSMutableString *tmp = [[NSMutableString alloc] initWithString:#""];
[tmp appendString:CMSG]; <<<<< This is code error at loop 2,
[tmp appendString:message]; the first loop success but second is fail
CMSG = tmp;
[tmp release];
}
}
else{
if (message){
cmessage = [[NSString alloc]initWithFormat:#"%#%#",cmessage,message] ;
}
else
NSLog(#"Error converting received data into UTF-8 String");
cdata++;
if(cdata==idata) {
msgComplete=YES;
}
}
if (msgComplete) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:cmessage forKey:kNotificationMessage];
[notificationCenter postNotificationName:kNotification object:self userInfo:userInfo];
cmessage=#"";
CMSG=#"";
msgComplete=NO;
bheader=YES;
cdata=0;
charS=NO;
[cmessage release];
}
}
[sock readDataToLength:1 withTimeout:-1 tag:0];
}
This code fail at second loop at [tmp appendString:CMSG]; Thread 1: Program received signal "SIGABRT"
How fix this error ?
Thanks gurus,
CMSG is assigned to tmp but tmp is released right afterwards. CMSG needs to be retained throughout the loop.
CMSG = tmp; //<< should be CMSG = [tmp retain];
[tmp release];
Now you have another potential problem. You are using a deprecated method here
[data getBytes:&tdata];
if (tdata > 5) {
getBytes: was deprecated because of a potential buffer overrun and the result of getBytes is not appropriate to use for if(tdata > 5). If you want to check the length of bytes get that that from the NSData object.
I see an error that may or may not be the ultimate problem here. In the final conditional statement, you have:
cmessage=#"";
...
[cmessage release];
I don't think you should be releasing that string. I don't actually know all of the ins and outs of NSString objects that are defined in that way (as opposed to stringWithFormat: or initWithString:), but I don't think that you have ownership of that string object. You shouldn't have to release it.
Remove that release message and see if it helps.

AsyncSocket just read once?

I make simple client server with no CLRFdata, but i give header of message with lenght message self. this is my code :
This method send data
- (void)sendMessage:(NSString *)message {
unsigned char lendata = [message length];
senddata = [NSMutableData dataWithBytes: &lendata length:sizeof(lendata)] ;
[senddata appendData:[message dataUsingEncoding:NSUTF8StringEncoding]];
[socket writeData:senddata withTimeout:-1 tag:0];
}
This method read data from socket lenght = 1 byte until long of code on header.
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
NSString *message = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
if(bheader){
cdata++;
if(cdata==2){
[data getBytes:&idata];
bheader = NO;
cdata=0;
cmessage = [[NSString alloc] initWithFormat:#""];
}
}
else{
if (message){
cmessage = [[NSString alloc]initWithFormat:#"%#%#",cmessage,message] ;
}
else
NSLog(#"Error converting received data into UTF-8 String");
cdata++;
if(cdata==idata) {
msgComplete=YES;
}
}
if (msgComplete) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:cmessage forKey:kNotificationMessage];
[notificationCenter postNotificationName:kNotification object:self userInfo:userInfo];
cmessage=#"";
msgComplete=NO;
bheader=YES;
cdata=0;
[cmessage release];
}
[sock readDataToLength:1 withTimeout:-1 tag:0];
}
and this method
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
[sock readDataToLength:1 withTimeout:-1 tag:0];
}
I'm succes send data and recv data from server use this code,
i try make login and server got data and notification, and i recv message with this code, but after that i cant recv message from server when i send message again, but on TCPDUMP i see any message sent to server and server response with a message ?
could any one fix my code where the problem ?
Thanks.

How to Create UIImage from NSData and Avatar Data of XMPP?

This question is related to Iphone SDK, NSData and UIImage.
I am trying to create an image from the Avatar Data returned from the xmpp like the following:
<presence from='yyy#184.73.164.51/spark' to='ken#184.73.164.51/424978324712783686768453' id='Oj02v-45'><status>Away due to idle.</status><priority>0</priority><show>away</show><x xmlns='vcard-temp:x:update'><photo>a3f549fa9705e7ead2905de0b6a804227ecdd404</photo></x><x xmlns='jabber:x:avatar'><hash>a3f549fa9705e7ead2905de0b6a804227ecdd404</hash></x></presence>
So in this case, I assume that a3f549fa9705e7ead2905de0b6a804227ecdd404 is the photo data.
So How can I transfer this into NSData?
I think if I can get the NSData object,
I can easily create the UIImage, right?
I think "a3f549fa9705e7ead2905de0b6a804227ecdd404" is the photo data
this is my codes:
NSString* command = #"a3f549fa9705e7ead2905de0b6a804227ecdd404";
command = [command stringByReplacingOccurrencesOfString:#" " withString:#""];
NSMutableData *commandToSend= [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [command length]/2; i++) {
byte_chars[0] = [command characterAtIndex:i*2];
byte_chars[1] = [command characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[commandToSend appendBytes:&whole_byte length:1];
}
UIImage *image = [UIImage imageWithData: commandToSend];
However,
it doesn't work.
Anyone knows what's wrong with it?
In XMPPPresence.m add this method
-(NSString *)photo {
NSXMLElement *xElement = [self elementForName:#"x" xmlns:#"vcard-temp:x:update"];
NSString *photoHash = [[xElement elementForName:#"photo"]stringValue];
return photoHash;
}
// In XMPPStream's delegate:
- (void)xmppStream:(XMPPStream *)stream didReceivePresence:
(XMPPPresence *)presence {
NSString *photoHash = [presence photo];
if ([photoHash length] > 0) { // in case when there's no photo hash
XMPPJID *rosterJID = [presence from];
BOOL requestPhoto = ... // determine if you need to request new
photo or nor
if (requestPhoto) {
NSXMLElement *iqAvatar = [NSXMLElement elementWithName:#"iq"];
NSXMLElement *queryAvatar = [NSXMLElement elementWithName:#"vCard"
xmlns:#"vcard-temp"];
[iqAvatar addAttributeWithName:#"type" stringValue:#"get"];
[iqAvatar addAttributeWithName:#"to" stringValue:[rosterJID full]];
[iqAvatar addChild:queryAvatar];
XMPPIQ *avatarRequestIQ = [XMPPIQ iqFromElement:iqAvatar];
[stream sendElement:avatarRequestIQ];
}
}
}
// And when buddy will send photo, it will be in vcard BASE64-encoded.
// You will receive it as IQ:
- (BOOL)xmppStream:(XMPPStream *)stream didReceiveIQ:(XMPPIQ *)iq {
XMPPElement *vCardPhotoElement = (XMPPElement *)[[iq
elementForName:#"vCard"] elementForName:#"PHOTO"];
if (vCardPhotoElement != nil) {
// avatar data
NSString *base64DataString = [[vCardPhotoElement
elementForName:#"BINVAL"] stringValue];
NSData *imageData = [NSData
dataFromBase64String:base64DataString]; // you need to get NSData
BASE64 category
UIImage *avatarImage = [UIImage imageWithData:imageData];
XMPPJID *senderJID = [iq from];
[self xmppStream:stream didReceiveImage:avatarImage
forBuddy:senderJID]; // this is my custom delegate method where I
save new avatar to cache
}
return NO;
}
Hope this will help you.
That is the picture hash you now have to send a vcard request which will contain the same hash for verification and binval containing the picture data in base64

My Thread Programs Block

I wrote a program that worked as a server.
Knowing that "accept" was blocking the program.
I wanted to launch a thread with this statement to prevent precisely that the program blocked, but this still happens.
Can anybody help?
Post code
Thanks
-(IBAction)Connetti{
if(switchConnessione.on){
int port = [fieldPort.text intValue];
labelStatus.text = [[NSString alloc] initWithFormat:#"Il Server รจ attivo"];
server_len = sizeof(server);
server.sin_family = AF_INET;
server.sin_port = htons((u_short)port);
server.sin_addr.s_addr = INADDR_ANY;
sd = socket (AF_INET, SOCK_STREAM, 0);
bind(sd, (struct sockaddr*)&server, sizeof(server));
listen(sd, 1);
[NSThread detachNewThreadSelector:#selector(startThreadAccept) toTarget:self withObject:nil];
}
else {
labelStatus.text = [[NSString alloc] initWithFormat:#"Server non attivo"];
switchChat.on = FALSE;
switchChat.enabled = FALSE;
}
}
-(void)startThreadAccept{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
[self performSelectorOnMainThread:#selector(acceptConnection) withObject:nil waitUntilDone:NO];
[pool release];
}
-(void)acceptConnection{
new_sd = accept(sd, (struct sockaddr*)&server, &server_len);
labelStatus.text = [[NSString alloc] initWithFormat:#"Ho accettato una connessione:%d", new_sd];
switchChat.enabled = TRUE;
}
You still call accept() on the main thread. If you want the connection to be accepted on a different thread, then you need to remove the -performSelectorOnMainThread: call.
this is my new methods
-(IBAction)Connetti{
//code
[NSThread detachNewThreadSelector:#selector(acceptConnection) toTarget:self withObject:nil];
//code
}
-(void)acceptConnection{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
new_sd = accept(sd, (struct sockaddr*)&server, &server_len);
labelStatus.text = [[NSString alloc] initWithFormat:#"Ho accettato una connessione:%d", new_sd];
switchChat.enabled = TRUE;
[pool release];
}
It 's a correct solution? Why in some occasions, the thread seems to not start? Thanks