iOS WatchKit - 如何确定您的代码是在手表扩展程序还是应用程序中运行 [英] iOS WatchKit - how to determine if your code is running in watch extension or the app

查看:113
本文介绍了iOS WatchKit - 如何确定您的代码是在手表扩展程序还是应用程序中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用WatchKit,您可以在手机上运行您的应用程序,以及作为扩展程序运行的监视应用程序。

With WatchKit you have your app that runs on the phone, and the watch app that runs as an extension.

如果您创建一个包含公共代码的库在手机应用和手表扩展程序中使用,有没有办法判断代码是在手机应用程序还是手表扩展程序中运行?

If you create a library that contains common code to be used in both the phone app and the watch extension, is there a way to tell if the code is running in the phone app or the watch extension?

if ([self isRunningInWatchExtension]) {
    NSLog(@"this is running on watch");
} else {
    NSLog(@"this is running on phone app");
}


- (BOOL)isRunningInWatchExtension {
    ???
}


推荐答案

我已经完成了这个检查包标识符:

I've accomplished this by checking the bundle identifier:

if ([[[NSBundle mainBundle] bundleIdentifier] isEqualToString:kAppBundleIdentifier]) {

    // Running in main app
}
else if ([[[NSBundle mainBundle] bundleIdentifier] isEqualToString:kWatchBundleIdentifier]) {

    // Running in extension
}

这篇关于iOS WatchKit - 如何确定您的代码是在手表扩展程序还是应用程序中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆