iOS WatchKit - 如何确定您的代码是在 watch 扩展程序中运行还是在应用程序中运行

标签 ios watchkit

有了 WatchKit,您的应用程序可以在手机上运行,​​ watch 应用程序可以作为扩展程序运行。

如果您创建的库包含在手机应用和 watch 扩展中使用的通用代码,有没有办法判断代码是在手机应用还是在 watch 扩展中运行?

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


- (BOOL)isRunningInWatchExtension {
    ???
}

最佳答案

在目标条件句中,有一些条件句可能对您有所帮助,

#if TARGET_OS_WATCH
//do something for watch
#else
//do something for ios ==> assuming you only support two platforms
#endif

关于iOS WatchKit - 如何确定您的代码是在 watch 扩展程序中运行还是在应用程序中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29354990/

相关文章:

ios - 如何在iOS中取消AWS 1.4.6 AmazonServiceRequest

ios - React Native 选择器值在选择时重置

iOS Safari 和 chrome 上的 Javascript Canvas 缓冲区/性能低下

swift - iPhone语言设置为中文或日文时,WatchKit Extension连接的outlet在运行时为nil

ios - 快速检索 WKInterfaceLabel 的文本

ios - session (_ :activationDidCompleteWith:error:) not called on Apple Watch (but is called in Simulator)

ios - WatchOS2 WCSession WCSession 在 WatchKitExtension 中可以有多少个委托(delegate)?

ios - 从 NSMutableArray 错误中删除值

ios - AFNetworking:getPath:参数方法导致问题

swift - handleWatchKitExtentionRequest ...如何访问回复元素以将值返回给 watchkit 扩展?