简体   繁体   中英

Watch extension NSBundle URLForResource always nil

In my application i use a static library with resources target, I've connected the resources target to Build Phases -> Copy Bundle Resources and i fetch library resources like this:

 NSUrl *resourcesUrl = [[NSBundle mainBundle] URLForResource:@"MySDKResources" withExtension:@"bundle"]
 NSBundle *bundle = [NSBundle bundleWithURL:resourcesUrl];  
 NSString *path = [bundle pathForResource:filename ofType:fileExtension];

Using watch extension, when application receive func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) delegate I use the same code but this time

  • resourcesUrl is in different folder, one i cannot find on finder
  • bundle is nil
  • path is obviously nil
  • didReceiveMessage delegate call this code NOT on main thread (so I dispatch on main)

In the same session when one of application classes call this code file path return alright but when watch extension delegate call the same code it returns nil

I've tried to add the static library to Target Dependencies and Compile Sources and adding the resources target to Copy Bundle Resources but i get No such file or directory error

Ok, turns out that when you run watch simulator it kills app on iPhone simulator (but not crash it) so it seems like the app running but actually you cannot instantiate bundle or any other app resource. this blog post explain how to run application and watch application together on debug.

TL;DR :

while running code on simulator on Xcode menu bar select Debug -> Attach to Process -> select the process of your iOS app from the list (It'll probably be under Likely Targets)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM