Share image resources between app and today widget - ios

Can I share resources (images that are compiled into the application) between my main iOS app and my today extension widget? Say I create a new application. Then add a today extension project to the solution. And then I create a UIImageView. I want to reference an image from a shared pool of images rather than having 2 of every image, one added to main app and another added to widget app.
imageView.Image = UIImage.FromBundle("IMG_3799.JPG");
The IMG_3799.JPG is inside the main application in the References folder of that project. I'd prefer the same image asset to be referenced by the widget. Is that possible? Currently, i'm making a 2nd copy of IMG_3799.JPG and putting it into the widget.

Out of box, you can't.
The closest thing to that would be to create an app group that contains the main app and the extension and that has access to the common folder. However you can't place any file there during deployment, just during runtime, so it may or may not be a workaround for what you require, but that is the only way.

I'm afraid this might be late. Following is what I did with Xcode 12.4
First, go into your Assets.xcassets file in Xcode then select the image you want to be shared.
Second, click "Hide or show the Inspectors" in the top right corner of Xcode.
Thir, click "Show the file inspector"
Then you should see Target Membership where you can simply click the checkbox to have the images to be used in main app, extensions, etc.
I found this method for Wigetkit from 17:10 of this youtube video: https://www.youtube.com/watch?v=ZzBBDrFNXmk

I came across this answer while looking for a way to share images between my main app and my iMessage extension. As it turns out, just by adding the image files to both targets I was able to use one single copy of my images in both places.
In the project navigator, I added the images to a folder (not .xcassets) under my main project (not the extension) and just ensured that the Target Membership was set to both the main app and extension.

Related

Material icons library is not detected on Xcode

I am trying to use Material Icons library on my swift application on Xcode but I cannot make that the library will be detected by the project.
The steps that I have followed:
From here I have copied the link to use git repository.
On terminal, I have go through the project folder and use the link that I have copied before.
Xcode project does not detect it so I drag it from the folder to the project but I cannot use it.
Am I doing something in the wrong way?
Thanks in advance!
As #EricAya pointed I was following the wrong steps. I saw that there were steps to use Material icons for iOS but as it says in the documentation:
Material icons also work well within iOS apps. In both the material icons library and git repository, these icons are packaged up in Xcode imagesets which will work easily with Xcode Asset Catalogs (xcassets).
I guessed that it could also be installed with git repository but I could not get it. Finally I used the following steps:
Go to the official webpage of Material icons and click on the icon that you would like to use.
You will see on the right corner of the webpage two options to download the icon (SVG or PNG). I have chosen PNG.
Unzip the .zip package.
Open ios folder and drag the .imageset folder to your .xcassets file in your project of Xcode.
You can create both an imageView or button to set it and use the following code to create the image:
UIImage(named:"ic_close")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
To set it to your UIImageView you can use yourImage.image and for your button yourButton.setImage().
Do not forget to change the tint property of your imageView or button. I spent some hours thinking that it was not working and the problem was that the tint property was white (sometimes simpler things make waste your time easily).

Its just impossible to add an action extension icon

I've been literally trying for two weeks to get an icon for an action extension to appear, but absolutely nothing works.
I've seen past questions such as this iOS 8 Action Extension icon sizes and this Why isn't my iOS8 Application Action Extension App Icon showing up? but they are of no use.
I've created an Xcode template project purely to try every combination of things mentioned in the other questions, still without avail.
1) The first thing that's mentioned is that the action extension should have its own asset catalog, ok here it is:
2) Secondly that it should contain an app icon image set, here that is:
3) Thirdly people say they had to add its target membership to the extension and to the app. Here that is too (in all of these things, I've tried variations such the membership being just the extension, just the app, both)
4) Fourthly, people mention they had to add it to the compile sources, here that is, as well as it being copied in the bundle resources:
5) Some say it also needs to be copied in the container app's bundle, here that is, and for good measure its been added as a compile resource of that too:
6) One person says it was necessary to add the container apps assets to the extension bundle, I can't see how that would work as the icons in the container app are colored and not matching the requirements of the extension icons, but what the hell, might as well try everything right, and while we're at it lets add assets everywhere:
7) The asset icon itself is important, colored icons are not displayed, but I tried it anyway. A simple black and white image is described as the white parts appearing as grey but I've tried that anyway. Trying unsupported icons was just desperation and experimentation, of course I'm actually using an icon with a transparent background. When posted here it appears on this webpage as all white of course, but the non-black parts are actually fully transparent and have an alpha of 0:
But despite trying many combinations of all the various steps above, this is what it looks like on the device:
This is using iOS 10.n, latest 10.7 (it hasn't worked with any beta) and Xcode 8.6. If I download an app from the app store that uses action extensions then their icons are appearing, so there's no issue with the 10 beta. The issue is either something building the app with Xcode beta, or some step I've missed out - if that is the case, then what?
Finally found out the solution - in order to get it to work its additionally necessary to set the Asset Catalog compiler options of Build Settings section of the extension to specify the name of the app icon set within the asset catalog:
How to set an action icon really is the most appallingly badly documented aspect of iOS, totally shockingly awful.

Where should I put background and button image files? Assets or just drag into the app files?

I am making an app but I'm not quite sure where should I put the image files? Just drag and copy inside the app or in assests? What's the difference, I'm not quite sure.
I'd strongly recommend using the xcassets, which you can do by dragging your files onto it when you have it open in Xcode. Xcode handles the rest, copying them into your project. Doing this also allows you do call UIImage(imageNamed:), using the title you give to each Image Set. You also don't have to worry about specific naming of the different size classes, as you drag them to the appropriate section there.

Share extension custom UI. Button has not got image

I am using share extension with custom UI, So I configured my view controllers in storyboards, and add image (and tried add background image) to the button. So in storyboard I see that image. But when I run extension I don't see image on the button.
So the issue is because seems I don't add my images to the new target. So if you create a new target, and extension it's our new target we should be sure that all needed resources like images, strings are added to it.
So just use target membership to checkmark needed files or add it via xcode project "Build Phases" tab.
So it's also connected to other classes you want to use. Sometimes can be a pain to add all of them because some classes import other classes and etc.

Resources located in a separate bundle visible in IB, but not in the runtime

I have an application, where most of image resources are stored in the separate resource bundle (there is a valid reason to do that). This resource bundle is added to the project along with main application bundle and while I design my NIB in the Interface Builder all those images are visible inside the IB (i.e. they appear in the drop list for my UIImageView and they are properly displayed in my UI). However when I run my app in the simulator all those images are missing. Is there a way to tell UIKit to look for those images inside this additional resource bundle in addition to the main application bundle?
I can load those images manually in viewDidLoad no problem, but I'm looking for a way to use Interface Builder to make it easier to change design/skin of those NIBs.
Update: While image files from that bundle are available right in the drop list in the IB, they won't load in runtime. However if I prefix their names with a bundle name in IB like this:
Then they will load properly in the runtime, but IB will display them as big blue question marks.
Now the question is - how to have best of the both worlds and have images displayed properly both in IB (design-time) and runtime.
I ran into the same problem, and it's likely due to the way that Xcode is compiling your images when assembling your bundle target. Since bundle targets aren't fully supported on iOS, you have to create them as Mac OS X targets. That leaves a problematic build setting in place, namely COMBINE_HIDPI_ARTWORK, which will be set to YES. This results in your normal and #2x variants being combined into a single tiff file in your bundle. The resulting file is supported on Mac OS but not iOS.
(Thanks to this answer for the tip).
To fix this, select your bundle-creating project in the File Navigator, select your bundle target, switch to the Build Settings tab, and search for the aforementioned setting. Then either select the whole row and tap Delete or just set it to NO.
At that point you should be able to use your images in both IB and code without doing anything special (other than making sure the .xib and image are in the same bundle).

Resources