5

Any ideas why a build would succeed for simulator but not a device?

I created a Cocoa framework and was able to add it to a new single-page application project, and call the methods defined in the framework. The project built as long as I had a simulator selected as the build target and not a device. I have not tried running it, only building. I'm using Swift 3 and XCode 8.1.

I get Use of unresolved identifier errors.

JBaczuk
  • 13,886
  • 10
  • 58
  • 86

2 Answers2

4

you should make a fat Library: for make a fat library do these step by step :

1 - Build yourFreamework target for iOS simulator and extract framework from products folder on your desktop.

2 - Rename the framework to yourFrameworkName-sim.framework so that it is distinguishable later.

3 - Repeat the steps 1 and 2 for iOS device. You can select ‘Generic iOS Device’. Don’t forget to rename the framework to yourFrameworkName-dev.framework.

4 - Use the following command to combine both binaries into a single fat binary file (Make sure you are on desktop while running this command).

$lipo -create ./yourFrameworkName-sim.framework/yourFrameworkName ./yourFrameworkName-dev.framework/yourFrameworkName -output ./yourFrameworkName

5 - Copy yourFramework binary file created in above step and replace it with the binary in yourFrameworkName-dev.framework folder.

6 - Open ‘Info.plist’ file contained in the same folder.

7 - Add ‘iPhoneSimulator’ string in ‘CFBundleSupportedPlatforms’ array.

8 - The final plist file would look like this:

enter image description here

9 - From folder :

yourFrameworkName-sim.framework/Modules/yourFrameworkName.swiftmodule/

copy ‘x86_64.swiftdoc’ and ‘x86_64.swiftmodule’ and paste them to

yourFrameworkName-dev.framework/Modules/yourFrameworkName.swiftmodule/

10 - By following above steps you have converted yourFrameworkName-dev.framework from device only to a universal fat framework. Rename it to yourFrameworkName.framework.

11 - Include this framework via ‘Embeded Binaries’ option in Xcode. Import the module in your file and you would be able to compile it successfully.

mohsen
  • 4,698
  • 1
  • 33
  • 54
  • i have got an error at step 4 that says: error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: can't move temporary file: ./ARFrameW to file: ./ARFrameW.lipo (Is a directory) how can i fix it or what i did wrong to get this error. Thanks in advance – R.AlAli Jan 16 '20 at 07:58
  • How to prepare for Objective C framework because it won't produce .swiftmodule – Chowdhury Md Rajib Sarwar Jul 08 '20 at 09:00
  • I don't think this is valid anymore, see: https://stackoverflow.com/a/65315026/1754401 – tagy22 May 11 '21 at 15:10
3

It sounds like you built the framework for a simulator and not for a device. When the linker is trying to link the application for a device, it doesn't find the framework built for that device.

Two of the ways to do it are as follows.

1) When building the framework, set the active scheme appropriate for the device (upper left area in Xcode). Then, before building the application for the device, go to Build Settings for the app and add the framework's location to Framework Search Paths. Make sure you pick the right binary! For example, when building for the iOS simulator, a debug binary of the framework is going to be in a directory called Build/Products/Debug-iphonesimulator.

With this approach you also need to add the framework to the Copy Files build phase of your app, specifying Destination as Frameworks.

2) Embed the framework into the application, make it a dependency of the app, and set up the application to link with the framework in the app's Build Phases. See

https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPFrameworks/Tasks/CreatingFrameworks.html

about embedding a framework and for other useful framework-related info. A convenient way of accomplishing this is to go to the General tab of your application target and add the framework in the Embedded Binaries section.

Alternatively, if you create your framework after creating the application, you can ask Xcode to embed the framework into the app.

Anatoli P
  • 4,791
  • 1
  • 18
  • 22
  • Ok, so how do you "build a framework for a device" and not for a simulator? – JBaczuk Nov 10 '16 at 05:20
  • @JBaczuk answer is, just build your framework while device is selected. – alicanbatur Feb 26 '17 at 19:45
  • If you're are using custom framework embed in project App store will fail to validate your app. To avoid validation fail case you need to add RunScript for working both simulator and device. refer this -> http://ikennd.ac/blog/2015/02/stripping-unwanted-architectures-from-dynamic-libraries-in-xcode/ – Ashokkumar Adichill Aug 24 '18 at 06:42