Apple watch multiple Assets.xcassets folder purpose - ios

What's the difference between the Assets.xcassets in
1) WatchKit App
2) WatchKit Extension
I'm not sure which folder should I place the image in.
I need to access the image (e.g. testImage.png) in both Interface.storyboard which resides in WatchKit App folder and in WatchKit Extension. In the controller I'm setting it using:
let menuIcon: UIImage? = UIImage(named: menu.iconName)
I've tried:
Option 1:
1) to place images in both WatchKit App and WatchKit Extension
Which will mean duplicate images, in two Assets.xcassets folder.
Option 2:
1) to place images in both WatchKit App only and change the target membership of the Assets.xcassets to support both WatchKit App and WatchKit Extension
Which approach is better? or are there any better ways?

This seems to be a holdover from watchOS 1 when the WatchKit Extension was actually running on the iPhone and sending information and resources to the WatchKit App running on the watch.
There are some calls which will work only with the WatchKit App's Assets.xcassets folder such as setImageNamed: or setBackgroundImageNamed:
There are several ways to change the current image of an interface object:
Use the setImageNamed: or setBackgroundImageNamed: methods to assign an image that is already in the Watch app bundle.
Use the setImage:, setImageData:, setBackgroundImage:, or setBackgroundImageData: >methods to transfer image data from your WatchKit extension to your Watch app.
Specifying images by name is more efficient because only the name string must be transferred to your Watch app. watchOS searches your Watch app bundle for an image file with the name you specified. The most efficient way to specify images efficiently is to store them in your Watch app bundle and use the setImageNamed: or setBackgroundImageNamed: as appropriate to configure the corresponding object.
Images created in your WatchKit extension must be transferred to the Watch app before they can be used. For example, using the imageNamed: method in your extension loads the image from your WatchKit extension’s bundle, not from your Watch app’s bundle. You can then call the setImage: method, passing in the image object. The WatchKit extension automatically transfers the image to the Watch app for display. While this has some additional overhead compared to loading images directly from the Watch app bundle, It should not have a significant impact on either performance or battery life.
App Programming Guide for watchOS / Images
Personally I would not place images in both folders as this will increase the size of your App. I tend to place images that will be set by Storyboard in the WatchKit App's folder and all the images that will change programmatically in the Extension.

Related

Dynamic App Icon from Plist Swift

I have searched online on how to change your app icon dynamically and all says you would need put the images under project directory. Is there any ways I can get an image from URL from my plist and show it as my app icon?
According to the documentation, you must declare your alternate icons in your app's Info.plist, which is shipped in your app bundle. Since your app bundle is read-only, you can't add/edit entries in your Info.plist, which means it's impossible to download a new app icon from a server and set it as your app's icon

Using images in the main Xcode project for Watch app

I have a watch application and I will display images in it. Those images are already included in IOS application so I don't want to copy them into watch app as well. How to use images in IOS side (not in asset catalog) in watch app?
With the introduction of watchOS2, WatckKit apps became native apps and not just extensions of their iOS counterpart. Due to this, the iOS and watchOS apps can no longer share a common AppGroup.
The only way to share data between apps is using the WatchConnectivity framework. However, for static images, you shouldn't use the WatchConnectivity framework. If you don't want to add the pictures to the asset catalog, you don't actually need to copy them. All you need to do is add them to your WatckKit app or app extension target, depending on whether you want to use them from Storyboard or code.
Keep in mind that the system will actually copy those images to your Watch target as well, since storage is not shared between the iOS and watchOS apps so you don't actually gain any storage advantages by not adding those images to both apps' asset catalogs.

Is it allowed to let the alternate app icon to be dynamic?

Since iOS 10.3 has been released, Apple added a new feature which allows us to change the app icon dynamically, by using setAlternateIconName(_:completionHandler:) method. So far, as mentioned in the method documentation, we have to mention the name(s) of the alternate app icon(s) in the project .plist file, assigned to CFBundlePrimaryIcon key.
Actually, when working with static icons (icons that have been added directly to the app main bundle) it works as expected without any problems:
My question is:
Is it possible -or is there a workaround- to set the alternate app icon dynamically (for instance: icons that have been downloaded from the web and saved in the app documents directory)?
I don't think its possible.
setAlternateIconName(_:completionHandler:) API looks for the icons inside app bundle and cannot be changed to fetch from sandbox.
Also, most probably Apple reviews the app icons you have bundled for avoiding use of same app icons or icons similar to Apple apps.
Refer: https://www.hackingwithswift.com/example-code/uikit/how-to-change-your-app-icon-dynamically-with-setalternateiconname

Whether we can create an App Extension for WatchKit(WatchOS 2) App similar to that of iOS?

I'm developing an WatchKit App (using WatchOS 2).
I have one query whether we can create an App Entension for WatchKit App similar to that of iOS, so that other WatchKit App(host app) can use this extension for sharing purpose (may be a text or an image or a link).
Thanks!

Getting images in WatchKit app from iOS app

I'd like to show in a WatchKit app the user's avatar image from its paired iOS app. Such image is downloaded by the iOS app querying a REST web service when the user logs in the app. How could I get it from the WatchKit app?
The downloaded avatar is persisted as a .jpeg file in the Documents folder of the iOS app.
The easiest way to solve your issue is to write the avatar image out to the App Group directory instead of the Documents directory in the iOS app. Then you can load the avatar image from the App Group file system directly from the Watch Extension. In order to do this, you'll need to make the following changes.
Step by Step
Add an App Group to your iOS app and Watch Extension
Write the avatar image to the App Group instead of the Documents directory
Read the avatar image out of the App Group in the Watch Extension
Push the avatar image into the WKInterfaceDevice shared image cache
Apply the image to either a WKInterfaceImage or WKInterfaceGroup to be displayed
Hopefully that helps make things a bit more clear.

Resources