Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an instance of UIView from Watch Extension in watch OS 2?

In watch OS 1, I was able to create an instance of a custom view in WatchExtension. I'm not talking about showing it, I mean just creating an instance of the view and then create an UIImage with its content.

Now, in watch OS 2, I can't access to the UIView from WatchExtension, even when I have imported the UIKit framework.

Is there any way to be able to create an instance of UIView from WatchExtension?

like image 396
Reynaldo Aguilar Avatar asked Sep 17 '15 15:09

Reynaldo Aguilar


People also ask

What is a WatchKit extension?

The WatchKit framework provides infrastructure for creating watchOS apps, including an extension delegate that manages background tasks, extended runtime sessions, and Siri intents. The framework also performs other support tasks, such as accessing information about the user's Apple Watch.

What is an Apple Watch extension?

WatchKit extension. An extension that contains your watchOS app's code. In Xcode 14 and later, you can produce watchOS apps with a single watchOS app target for code, assets, extensions, and localizations. These single-target watchOS apps can run on watchOS 7 and later.

How do you create an app for Apple Watch?

To build a watch-only app, start a new project in Xcode, select the Watch App template, and click Next. Provide a name, and choose the interface, life cycle, and language for your watch app. Select the options to include a notification scene template or unit and user interface tests, and click Next.


1 Answers

It seems it's not possible on watchOS2 unfortunately. While watchOS1 used the iOS platform SDK, watchOS2 is a separate platform.

migration docs:

In watchOS 2, you can share code, but not frameworks, between your iOS app and Watch app. Because the apps run on separate platforms with different architectures...

Also in WatchKit in depth 1 wwdc video they mention this:

The WatchKit extension for watchOS 1 is something you have created already, there is a target in your project, but it uses the iOS platform in SDK.

Here are the available system technologies you can use on watchOS2.

I did the same thing on watchOS1 like you, used a UIView and snapshotted it into a UIView. Besides doing this on the phone and sending it back to the watch (in which you loose the benefit of watchOS2 that the extension runs on the watch and doesn't need to communicate with the phone), or getting it as an image from a server, I don't see any other way of doing it.

edit: there is Core Graphics available so we are able to do basic drawings. See here: http://develop.watch/develop-for-watchos-2-iii-drawing/

like image 142
izerik Avatar answered Nov 15 '22 03:11

izerik