3
你好,我正在嘗試一些相當簡單的想法。 我做了一個使用APNS發送數據的可可應用程序,從我的數據庫獲取令牌,一切都設置完成並運行完美。如何在目標c中接收來自APNS反饋服務器的數據
現在我想檢查APNS反饋服務器並刪除從我的數據庫收到的任何令牌。 我在php,javascript等等中發現了數十個例子,但在Objective C中沒有任何例子。我已經閱讀了蘋果的編程指南,但無法弄清楚如何去做。
我正在建立與APNS反饋的連接,但我不知道如何讀取數據。 我是新來的可可,所以請詳細解釋:)
這就是我如何連接到反饋服務器,這與發送時連接的方式相同,只是使用其他主機。
- (void)connectToFeedBackServer
{
if(self.certificate == nil)
{
return;
}
NSString *feedBackHost = @"feedback.push.apple.com";
const char *cHost = [feedBackHost UTF8String];
NSLog(@"The size of cHost is: %lu", strlen(cHost));
NSLog(@"Host is: %s", cHost);
// Define result variable.
OSStatus result;
// Establish connection to server.
PeerSpec peer;
result = MakeServerConnection(cHost, 2196, &socket, &peer);
//NSLog(@"MakeServerConnection(): %d", result);
// Create new SSL context.
result = SSLNewContext(false, &context); //NSLog(@"SSLNewContext(): %d", result);
// Set callback functions for SSL context.
result = SSLSetIOFuncs(context, SocketRead, SocketWrite);
// NSLog(@"SSLSetIOFuncs(): %d", result);
// Set SSL context connection.
result = SSLSetConnection(context, socket);
// NSLog(@"SSLSetConnection(): %d", result);
// Set server domain name.
//result = SSLSetPeerDomainName(context, cHost, sizeof(cHost));
NSLog(@"SSLSetPeerDomainName(): %d", result);
result = SSLSetPeerDomainName(context, cHost, strlen(cHost));
result = SecIdentityCopyCertificate(_theIdentity, &(certificate));
// Set client certificate.
CFArrayRef certificates = CFArrayCreate(NULL, (const void **)&_theIdentity, 1, NULL);
result = SSLSetCertificate(context, certificates);// NSLog(@"SSLSetCertificate(): %d", result);
CFRelease(certificates);
// Perform SSL handshake.
do
{
result = SSLHandshake(context); NSLog(@"SSLHandshake(): %d", result);
} while(result == errSSLWouldBlock);
}
我怎樣嘗試讀取數據,並保存接收的令牌數組
- (NSMutableArray *)CheckFeedBackServer
{
char feedback[38];
size_t feedBackSize = sizeof(feedback);
size_t processed = 0;
NSMutableData *feedbackData = [[NSMutableData alloc]init];
NSString *token = [[NSString alloc]init];
NSMutableArray *tokenArray = [[NSMutableArray alloc]init];
[self connectToFeedBackServer];
while ([self getSSLContext])
{
int bytesLength = SSLRead([self getSSLContext], &feedback, feedBackSize, &processed);
[feedbackData appendBytes:feedback length:bytesLength];
while ([feedbackData length] > 38)
{
NSData *deviceToken = [NSData dataWithBytes:[feedbackData bytes] + 6 length:32];
token = [self deviceTokenToString:deviceToken];
[tokenArray addObject:token];
[feedbackData replaceBytesInRange: NSMakeRange(0, 38) withBytes: "" length: 0];
}
}
return tokenArray;
}
- (NSString *)deviceTokenToString: (NSData *)deviceToken;
{
NSString *tmpToken = [NSString stringWithFormat:@"%@", deviceToken];
NSUInteger loc_begin = [tmpToken rangeOfString: @"<"].location+1;
NSUInteger loc_end = [tmpToken rangeOfString: @">"].location-1;
return [tmpToken substringWithRange: NSMakeRange(loc_begin, loc_end)];
}
你在Objective-C中做服務器編程嗎?我認爲這有點不尋常。你可以發佈你用來連接APNS的代碼來獲得反饋嗎? – bdesham 2013-02-28 16:29:28
我已經編輯了我的問題幷包含代碼 – 2013-02-28 16:36:54
反饋應該在服務器端在APNS中處理,而不是在您的應用程序中處理。你想實現什麼? – 2013-02-28 17:08:31