简体   繁体   English

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

[英]iOS WatchKit - how to determine if your code is running in watch extension or the app

With WatchKit you have your app that runs on the phone, and the watch app that runs as an extension. 使用WatchKit,您可以在手机上运行您的应用程序,以及作为扩展程序运行的手表应用程序。

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? 如果您创建的库包含要在手机应用和手表扩展程序中使用的公共代码,是否有办法判断代码是在手机应用程序还是手表扩展程序中运行?

Ie

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


- (BOOL)isRunningInWatchExtension {
    ???
}

In target conditionals there are some conditionals that may help you, 在目标条件下,有一些条件可以帮助你,

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

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
}
  • This can be easy if you are calling any custom methods in your common framework class. 如果要在公共框架类中调用任何自定义方法,这可能很容易。 You just need to add additional method parameters to method. 您只需要向方法添加其他方法参数。 And if you are calling this method from iOS app or Watchkit app then add appropriate key-value pair to dictionary for parameters. 如果您从iOS应用程序或Watchkit应用程序调用此方法,则将相应的键值对添加到字典中以获取参数。 And compare this in your framework methods. 并在您的框架方法中进行比较。

  • To determine this from init or any other method then you can still get to know by this code, 要从init或任何其他方法确定这一点,您仍然可以通过此代码了解,

     NSLog(@"%@",[NSThread callStackSymbols]); 

So, you need to parse this string and get appropriate target names. 因此,您需要解析此字符串并获取适当的目标名称。 If it is called by iOS app then you will get ' UIKit ' string and from watch kit app extension you will get ' YourApp WatchKit Extension ' string somewhere. 如果它被iOS应用程序调用,那么你将获得' UIKit '字符串,并且从手表套件应用扩展程序中,您将获得' YourApp WatchKit Extension '字符串。 You can also refer this SO answer for parsing this string and compare it - https://stackoverflow.com/a/9603733/602997 你也可以参考这个SO答案来解析这个字符串并进行比较 - https://stackoverflow.com/a/9603733/602997

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 确定 UNNotificationResponse 是来自手表扩展程序还是 iOS 应用程序 - Determine if UNNotificationResponse is from watch extension or iOS app iOS WatchKit-启动手表应用“ SPErrorInvalidBundleNoGizmoBinaryMessage”时出错 - iOS WatchKit - Error launching watch app “SPErrorInvalidBundleNoGizmoBinaryMessage” WatchKit扩展:如何从主机iOS应用读取数据? - WatchKit extension: how to read data from host iOS app? 如何从父级ios应用程序调用watchkit扩展中的方法 - How to call the method in watchkit extension from parent ios app iOS / WatchKit:手表套件应用程序(Swift)是否可以使用iOS代码(目标C)? - iOS / WatchKit: Is there a way for the watch kit app(swift) to use the iOS code (Objective C)? 如何通过存储“回复”块在iOS应用和WatchKit扩展之间进行通信? - How to communicate between an iOS app and the WatchKit extension by storing the “reply” block? 如何使用Swift检测我的代码是否在WatchKit或iOS上运行? - How to detect if my code is running on WatchKit or iOS using Swift? 具有WatchKit扩展名的父级应用程序是否为iOS 8及更高版本? - Does parent app with WatchKit extension be iOS 8 and later? 在iOS App和WatchKit Extension之间共享Plist - Share Plist between iOS App and WatchKit Extension 通过WatchKit扩展检测iOS应用是否在前台 - Detect if iOS app is in foreground from WatchKit extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM