使用 ios4.x 我可以在收到kCTMessageReceivedNotification"通知时使用下面的代码来获取消息
With ios4.x I can use code below to get the message when get the "kCTMessageReceivedNotification" notification
CTTelephonyCenterAddObserver( ct, NULL, callback,NULL,NULL, CFNotificationSuspensionBehaviorHold);
if ([notifyname isEqualToString:@"kCTMessageReceivedNotification"])//receive message
{
NSDictionary *info = (NSDictionary *)userInfo;
CFNumberRef msgID = (CFNumberRef)[info objectForKey:@"kCTMessageIdKey"];
int result;
CFNumberGetValue((CFNumberRef)msgID, kCFNumberSInt32Type, &result);
Class CTMessageCenter = NSClassFromString(@"CTMessageCenter");
id mc = [CTMessageCenter sharedMessageCenter];
id incMsg = [mc incomingMessageWithId: result];}
但是使用 ios5 我不能这样做,因为 incMsg 是 nil,所以我该怎么做才能得到消息?
But with ios5 I can't do it as incMsg is nil,so what can i do to get the message?
谢谢
这是我发现的...
仅查看转储的私有 API,ChatKit.framework 似乎可以提供帮助.看一眼CKSMSService.h
Just looking at the dumped private APIs, it looks like ChatKit.framework could help. Take a look at CKSMSService.h
或 CKMadridService.hiMessage 消息.
or CKMadridService.h for iMessage messages.
我确实很快尝试在 CKSMSService
中混合我自己的方法:
I did quickly attempt to swizzle my own method in, for a couple methods in CKSMSService
:
- (void)_receivedMessage: (id)arg1 replace:(BOOL)arg2 replacedRecordIdentifier:(int)arg3 postInternalNotification:(BOOL)arg4;
- (void)_receivedMessage: (id)arg1 replace:(BOOL)arg2 postInternalNotification:(BOOL)arg3;
但在 iOS 5.0.1 上,我没有看到其中任何一个被调用(也许是我的错误?).因此,我尝试直接从 sqlite SMS 数据库中获取消息.注意...我没有构建完整的应用程序来注册通知.我假设您获取 kCTMessageReceivedNotification
的代码没问题……它只是不再给您 SMS content 了.因此,如果您将以下代码放入通知处理程序中,您应该能够看到消息文本:
but on iOS 5.0.1 I didn't see either of those get called (maybe my error?). So, I tried to just get the message directly from the sqlite SMS database. Note ... I didn't build the full app, to register for notifications. I'm assuming your code to get the kCTMessageReceivedNotification
is ok ... it just doesn't give you the SMS content anymore. So, if you put the following code in your notification handler, you should be able to see the message text:
- (NSString *) mostRecentSMS {
NSString *text = @"";
sqlite3 *database;
if(sqlite3_open([@"/private/var/mobile/Library/SMS/sms.db" UTF8String], &database) == SQLITE_OK) {
sqlite3_stmt *statement;
// iOS 4 and 5 may require different SQL, as the .db format may change
const char *sql4 = "SELECT text from message ORDER BY rowid DESC"; // TODO: different for iOS 4.* ???
const char *sql5 = "SELECT text from message ORDER BY rowid DESC";
NSString *osVersion =[[UIDevice currentDevice] systemVersion];
if([osVersion hasPrefix:@"5"]) {
// iOS 5.* -> tested
sqlite3_prepare_v2(database, sql5, -1, &statement, NULL);
} else {
// iOS != 5.* -> untested!!!
sqlite3_prepare_v2(database, sql4, -1, &statement, NULL);
}
// Use the while loop if you want more than just the most recent message
//while (sqlite3_step(statement) == SQLITE_ROW) {
if (sqlite3_step(statement) == SQLITE_ROW) {
char *content = (char *)sqlite3_column_text(statement, 0);
text = [NSString stringWithCString: content encoding: NSUTF8StringEncoding];
sqlite3_finalize(statement);
}
sqlite3_close(database);
}
return text;
}
现在,请确保此应用已安装在 /Applications/ 中.如果您只是构建这个应用程序,并使用 Xcode 正常安装,由于应用程序沙盒,您将在打开 sqlite 数据库时收到权限被拒绝错误.
Now, make sure this app is installed in /Applications/. If you just build this app, and install normally with Xcode, you'll get a permission denied error opening the sqlite database, because of app sandboxing.
我的代码片段只是获取最新的文本内容.这是一个对数据库进行更多操作的示例.查看 QuerySMS
方法.
My code snippet just gets the most recent text content. Here's an example of doing a little more with the database. Look at the QuerySMS
method.
另外,这里是 的关于数据库格式的链接短信.db.你可以在那里找到你需要的其他东西.或者,只需将 sms.db 复制到您的计算机,然后使用类似 Firefox SQLiteManager 插件.祝你好运!
Also, here's a link on the database format of sms.db. You can find what else you need in there. Or, just copy the sms.db to your computer, and browse it with something like the Firefox SQLiteManager plugin. Good luck!
更新:一些信息来自 我发布的关于 iOS 上多进程 SQLite 线程安全的问题一个>
这篇关于收到“kCTMessageReceivedNotification"时如何获取消息?IOS5通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!