0

I'm creating a custom framework. Previously in the framework we would download an image and use that to display in a button:

NSString *path = [NSString stringWithFormat: @"https://s3.amazonaws.com/assets/home.png"];
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *homeButton = [[UIImage alloc] initWithData:data];
self.rewardsCenterButton = [[UIBarButtonItem alloc] initWithImage:[homeButton imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                            style:UIBarButtonItemStylePlain
                                            target:self
                                            action: @selector(backToRewardsCenter:)];

However, in reviewing this we determined that this isn't good to have as a synchronous call so I'd like to add this PNG to the framework itself.

I've been able to do this by adding the image and ensuring it's included in the copy bundle resources Build Phase. With this set the image gets added in the universal framework output:

enter image description here

However, when I attempt to add this in code, it doesn't seem to show up. Also, when I add the framework in a project, I don't see the image being included, just the headers:

enter image description here

Here's what I've tried so far:

NSString* path = [NSString stringWithFormat:@"%@/TheoremReachSDK.framework/home.png", [[NSBundle mainBundle] bundlePath]];
    UIImage *homeButton = [[UIImage alloc] initWithContentsOfFile:path];

And:

NSBundle *bundle = [NSBundle bundleForClass:[TheoremReach class]];
    NSString *path = [bundle pathForResource:@"home" ofType:@"png"];
    UIImage *homeButton = [[UIImage alloc] initWithContentsOfFile:path];

And:

UIImage *homeButton = [UIImage imageNamed:@"home.png"];

But none of those display anything. Any idea what I need to do to get the image to display?

4
  • what bundle ID did you give to your framework? Commented Apr 5, 2017 at 16:28
  • Not sure - how would I see that? I'm not sure I set that... Commented Apr 5, 2017 at 17:01
  • look in your bundle's Info.plist file Commented Apr 5, 2017 at 17:02
  • In the framework it's set to $(PRODUCT_BUNDLE_IDENTIFIER). Commented Apr 5, 2017 at 17:04

1 Answer 1

1

I'm guessing NSBundle isn't finding the framework via the call to:

[NSBundle bundleForClass:[TheoremReach class]]

Try giving your framework an explicit bundle ID, e.g.: com.theoremreach.sdk, clean your projects and then rebuild.

You can then use code like this to fetch and display your image:

NSString *bundleIdentifier = @"com.theoremreach.sdk";
NSBundle *bundle = [NSBundle bundleWithIdentifier:bundleIdentifier];
if(bundle != nil) {
    NSString *path = [bundle pathForResource:@"home" ofType:@"png"];
    UIImage *homeButtonImage = [[UIImage alloc] initWithContentsOfFile:path];
    if(homeButtonImage != nil) {
        self.rewardsCenterButton = [[UIBarButtonItem alloc] initWithImage:[homeButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                                            style:UIBarButtonItemStylePlain
                                           target:self
                                           action: @selector(backToRewardsCenter:)];
    } else {
        NSLog(@"couldn't find home button image in bundle");
    }
} else {
    NSLog(@"could not find bundle with identifier %@", bundleIdentifier);
}
Sign up to request clarification or add additional context in comments.

6 Comments

This definitely seems like it should be working, but I continually see that it couldn't find the bundle with the identifier com.theoremreach.sdk despite cleaning, building and completely restarting Xcode :(
Look in the Info.plist file in framework within your compiled app (i.e. from the Products folder of your app project), does the bundle ID appear there?
Doesn't appear to. But I'm also using sublime text after cd'ing into the .app and most of the file seems encoded. I don't see any of the framework's stuff in this plist
If you double click the Info.plist file, it should open in the Xcode editor. My current thinking is that the bundle ID you think you applied didn't actually make it into the framework embedded in the finished product.
so it isn't an embedded framework, it's just a framework - perhaps that's the problem?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.