IOS WatchKit - how to determine if your code works in a chat extension or in an application

In WatchKit, you have an application that works on your phone, and a watch application that works as an extension.

If you are creating a library containing a common code that will be used both in the phone application and in the watch extension, is there a way to find out if the code works in the phone application or chat extension?

those.

if ([self isRunningInWatchExtension]) { NSLog(@"this is running on watch"); } else { NSLog(@"this is running on phone app"); } - (BOOL)isRunningInWatchExtension { ??? } 
+6
source share
3 answers

I accomplished this by checking the package id:

 if ([[[NSBundle mainBundle] bundleIdentifier] isEqualToString:kAppBundleIdentifier]) { // Running in main app } else if ([[[NSBundle mainBundle] bundleIdentifier] isEqualToString:kWatchBundleIdentifier]) { // Running in extension } 
+4
source

There are some conventions in the target conventions that may help you,

 #if TARGET_OS_WATCH //do something for watch #else //do something for ios ==> assuming you only support two platforms #endif 
+8
source
  • This can be easy if you call any custom methods in your common framework class. You just need to add additional method parameters to the method. And if you call this method from an iOS application or Watchkit application, add the corresponding key-value pair to the dictionary for the parameters. And compare this to your structure methods.

  • To determine this from init or any other method, you can find out by this code,

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

So, you need to parse this string and get the corresponding target names. If it is called by the iOS application, you will get the string " UIKit ", and from the extension for the watch set you will get the string YourApp WatchKit Extension . You can also reference this SO answer to parse this line and compare it - fooobar.com/questions/70289 / ...

0
source

Source: https://habr.com/ru/post/984457/


All Articles