【问题标题】:Alternative to NSDistributedNotificationCenter for comms between iPhone appsiPhone 应用程序之间通信的 NSDistributedNotificationCenter 替代方案
【发布时间】:2012-02-20 20:36:58
【问题描述】:

我一直在寻找一种方法来实时比较同一部手机上的应用程序之间的时间戳,而 NSDistributedNotificationCenter 听起来像是一个理想的解决方案,因为我可能不知道正在监听它的应用程序的名称,但听起来像是在 iOS 中不可用。

是否有一种等效的方法可以在不知道其名称的情况下将时间敏感事件通知多个应用程序?

为 iOS 5+ 编写代码并假设相关应用将注册通知。

【问题讨论】:

  • 在研究了以下几个答案(感谢您的帮助!)之后,我一直在使用跨应用 URL 处理程序,我几乎可以完成我需要做的事情。如果您有兴趣跟随,我在这里问了第二个特定于 URL 处理程序的问题:stackoverflow.com/questions/9044160/…
  • 到目前为止,MachPorts 之外的唯一/最佳答案是从外部服务器反弹。我要让这个人暂时撒谎......

标签: iphone notifications message center


【解决方案1】:

查看/System/Library/PrivateFrameworks/AppSupport.framework 中的CPDistributedMessagingCenter。但是,它是一个私有框架(可能会随操作系统版本而变化,并且不允许在 AppStore 中使用)。

此处的文档:http://iphonedevwiki.net/index.php/CPDistributedMessagingCenter

这里是我的示例代码:

https://github.com/H2CO3/PwnTube

https://github.com/H2CO3/Cereal

【讨论】:

  • 谢谢,这正是我要找的!不幸的是,我需要能够正确提交到应用商店...
【解决方案2】:

我很确定您可以使用 Mach 端口。它们的级别有点低,但效果很好。

【讨论】:

  • 他们甚至可能在 AppStore 中被允许 :)
【解决方案3】:

我找到了一种在 iOS 上使用 CFNotificationCenterGetDistributedCenter() 的方法。它存在于设备上,但不存在于 iOS SDK 中

void *libHandle = dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
CFNotificationCenterRef (*CFNotificationCenterGetDistributedCenter)() = (CFNotificationCenterRef (*)())dlsym(libHandle, "CFNotificationCenterGetDistributedCenter");
if(CFNotificationCenterGetDistributedCenter) {
    CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(), NULL, &NotificationUpdateApp, CFSTR("TestApp"), NULL, CFNotificationSuspensionBehaviorCoalesce);
}
dlclose(libHandle);

【讨论】:

    【解决方案4】:

    不是真的。在未越狱的设备上,您可以做的最接近您的要求的是让您的服务器与其他应用程序的服务器通信,并让该服务器向相关应用程序发送推送通知。如果没有 NSDistributedNotificationCenter(据您推测,它在 iOS 上不可用),您真的别无选择。

    【讨论】:

    • 并非如此。检查我的答案。它只是隐藏在喜欢的操作系统中 :) Foundation 不是唯一的框架。
    【解决方案5】:

    这个问题有点老了,但我会发布我的答案仅供参考。

    NSDistributedNotificationCenter 还不能用于 iOS,除非你正在开发一个你不想在 AppStore 上发布的应用程序,否则你不能使用 AppSupport.framework,因为它是私有的。

    iOS8 发布了 App Extensions,让我们能够与其他应用程序进行通信。我不知道你到底想做什么,但我相信如果你只是想比较一些其他应用程序的时间戳,它应该可以解决你的问题。

    AppExtensions 文档链接: https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/

    希望对某人有所帮助。

    【讨论】:

      【解决方案6】:

      NSDistributedNotificationCenter 存在于 iOS 上,但标头不提供给开发人员。

      在您的项目中创建一个头文件以使该类可用:

      @interface NSDistributedNotificationCenter : NSNotificationCenter
      
      + (NSDistributedNotificationCenter *)defaultCenter;
      // Returns the default distributed notification center - cover for [NSDistributedNotificationCenter notificationCenterForType:NSLocalNotificationCenterType]
      
      - (void)postNotificationName:(NSNotificationName)name object:(nullable NSString *)object userInfo:(nullable NSDictionary *)userInfo deliverImmediately:(BOOL)deliverImmediately;
      
      @end
      

      这在尝试从应用获取信息到其 UITest 运行器时非常有用,但您显然应该尝试将其放入 AppStore。

      我创建了一个项目XCTestBackChannel 来展示这一点。

      【讨论】:

        猜你喜欢
        • 2013-08-16
        • 1970-01-01
        • 2011-04-24
        • 2012-10-06
        • 2011-10-26
        • 2011-07-08
        • 2015-12-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多