iphone - iOS5上收到 "kCTMessageReceivedNotification"通知时如何获取消息

标签 iphone ios5 sms jailbreak

使用 ios4.x,我可以使用下面的代码在收到“kCTMessageReceivedNotification”通知时获取消息

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为零,那么我该怎么做才能获取消息?

谢谢

最佳答案

这是我发现的......

只要看看转储的私有(private) API,看起来 ChatKit.framework 可能会有所帮助。看一眼 CKSMSService.h

CKMadridService.h用于 iMessage 消息。

我确实很快尝试混合我自己的方法,例如 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 的代码没问题......它只是不再为您提供短信内容。因此,如果您将以下代码放入通知处理程序中,您应该能够看到消息文本:

- (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 数据库时收到权限被拒绝的错误。

我的代码片段只是获取最新的文本内容。 Here's an example of doing a little more with the database 。查看 QuerySMS 方法。

另外,这里有一个 link on the database format sms.db。您可以在那里找到您需要的其他内容。或者,只需将 sms.db 复制到您的计算机,然后使用类似 Firefox SQLiteManager plugin 之类的内容进行浏览。 。祝你好运!

更新:来自question I posted on multi-process SQLite thread safety on iOS的一些信息

关于iphone - iOS5上收到 "kCTMessageReceivedNotification"通知时如何获取消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10681995/

相关文章:

ios - UITabBarController -shouldAutorotateToInterfaceOrientation : warning

python - 使用 AWS 发送短信时设置消息发送者

ios - 在 iOS 5 Storyboard 中,如何将新场景从 Popover 推送到原始 View Controller ?

iphone - 使用 CGContextRef 绘图

iphone - 如何在通过 Storyboard 使用静态内容时更改分组 tableView 标题部分中的字体样式

ios - 隐私浏览模式下的 OpenURL

java - 如何从另一个类中调用字符串

batch-file - 如何从批处理脚本发送短信?

iphone - 如何通过单元格的文本数据获取 NSIndexPath(对于表格单元格)?

javascript - 您可以使用 JavaScript 显示/隐藏 iPad Safari 键盘吗?