Renaming an iOS Framework after it's been built - ios

I've built a framework from modified open source C++ code for use in another development sdk. Apps are not able to use this sdk if they have another sdk that also depends on this framework. How can I re-namespace this framework after its already been built (changing MyFramework.xcframework -> MyNewFramework.xcframework).
After renaming all of the references I could find, I was still getting a linker error on install: dyld: Library not loaded: #rpath/MyFramework.framework/MyFramework Referenced from: /private/var/containers/Bundle/Application/[app] Reason: image not found

This requires updating the name of the framework everywhere:
Open MyFramework.framework directory (if using an XCFramework, this will require opening the .xcframework directory and repeating these steps for the .frameworks for both the x86_64 and arm64 architectures.)
Open the Info.plist and change the Bundle name and Executable file to MyNewFramework. You will also want to update the bundle id
Open Modules/module.modulemap. Change the uses of MyFramework to MyNewFramework:
framework module MyNewFramework {
umbrella header "MyNewFramework.h"
export *
module * { export * }
}
Open the Headers directory and for each and every header file in there, you'll need to change all of the imports of other local header files: #import <MyFramework/Something.h> -> #import <MyNewFramework/Something.h> (I would suggest a global find and replace for #import <MyFramework/).
Change the file in the Headers directory MyFramework.h to MyNewFramework.h
Change the name of the executable found in the framework directory from MyFramework to MyNewFramework
Once navigated to the MyFramework.framework directory, run the command: otool -l MyNewFramework | grep rpath. It should echo something like this: name #rpath/MyFramework.framework/MyFramework as one of the options. Copy this path.
Using the command copied from step 6, Replace the instances of MyFramework with MyNewFramework run this command: (changing #rpath/MyNewFramework.framework/MyNewFramework if different from what you'd copied)
install_name_tool -id #rpath/MyNewFramework.framework/MyNewFramework MyNewFramework
Confirm that renaming the rpath was successful by running otool -l MyNewFramework again and checking that the path has been updated to MyNewFramework.
Rename the name of the entire framework from MyFramework.framework to MyNewFramework.framework
If using an XCFramework, navigate outside of the architectures to the Info.plist found directly in the .xcframework directory. Under AvailableLibraries in both Item 0 and Item 1 change LibraryPath from MyFramework.framework to MyNewFramework.framework.
If using an XCFramework, don't forget to rename the name of the outermost directory from MyFramework.xcframework to MyNewFramework.xcframework.
To ensure there aren't any hanging references, delete derived data (rm -rf ~/Library/Developer/Xcode/DerivedData/). Make sure to tear down your dependencies and re-integrate them with the renamed one. (For cocoapods, this involves updating your Podfile or podspec with the new framework name, then running pod deintegrate && pod install.)
Whew! What a job! Go fix yourself a nice cup of something.
Note: verify that this doesn't cause any issues while building and exporting your app. If so, consider disabling Bitcode if that's a viable option for you.

Related

What's the equivalent of "development pods" under Carthage?

The teams developing frameworks for our iOS app are migrating from Cocoapods to Carthage.
Under Cocoapods, I could set up dependencies as "development pods". For example, instead of having the main app download a specific version of an xyzzy dependency, I could set up xyzzy as a development pod and point it to my local directory where I had checked out xyzzy from its Git repo. While I was working in the main app's project, any edits I'd do to xyzzy's files would be made in that directory. This let me build and test changes immediately, and when I was ready to check them in, Git would find them in the xyzzy project's directory.
Under Carthage I haven't found a way to do this. I see http://allocinit.io/ios/debugging-carthage-dependencies/ which explains how to create symbolic links so that I can see the dependency source files to make debugging easier, but any edits I make to them are under the main application's Carthage/Builds directory.
How do I set up the equivalent of development pods under Carthage?
I believe Carthage doesn't have something similar to "development pods" yet.
But you could simulate "development pods" just following these steps:
Steps:
Add the .xcodeproj to your workspace
Remove all the dependencies you have in your project of the framework you added in step 1. (probably you may need to remove it from Build Phases -> Run Script -> Input Files too )
Go to General tab of the target you want to run, add the framework under Linked Frameworks and Libraries (it is going to take the one added from the .xcoproj)
(optional) you may need to run carthage bootstrap in the framework's repo you want to add locally.
That's it.
After that you will be able to run your project and update framework's code in the same workspace.
This works just as well as development pods for me, as of Xcode 8.3.3 and Carthage 0.24.0:
In app path, rm -rf Carthage
Point at the appropriate branch or tag in Cartfile
carthage update --use-submodules (generates .gitmodules and clones repo into Carthage/Checkouts)
In Xcode under project -> Build Phases -> Run Script, comment out the line that ends with carthage update --cache-builds if present.
Change to the General tab and remove the lib from Embedded Binaries
Right-click project, Add Files to app..., add lib from Carthage/Checkouts
Under project -> General, re-add the library, choosing the one you added in the previous step.
App should now build with the local lib. Make sure that your .gitignore has Carthage/{Build,Checkouts} and .gitmodules.
This answer is a summary of a successful implementation of the solution introduced here.
A cleaner solution is using local paths for dependencies in Cartfile.
Environment
Xcode 10.1
macOS 10.13.6
Step 1. Symbolic linking
1.1 Change $(SRCROOT_MAIN)/Carthage/Checkouts/$(DEVELOPING_FRAMEWORK_NAME) directory to a symbolic link pointing to source root directory of your developing framework $(SRCROOT_DEVELOPING_FRAMEWORK), where $(SRCROOT_MAIN) is source root directory of your main app. Backup existing directories before this change.
This linking enables version-controlled changes in your developing framework.
Syntax when using ln utility,
$ ln -s "$SRCROOT_DEVELOPING_FRAMEWORK" "$SRCROOT_MAIN/Carthage/Checkouts/$DEVELOPING_FRAMEWORK_NAME"
1.2 Change $(SRCROOT_DEVELOPING_FRAMEWORK)/Carthage/Build directory in your framework to a symbolic link pointing to $(SRCROOT_MAIN)/Carthage/Build directory. Backup existing directories before this change.
This linking enables access to all frameworks built by Carthage from both your developing framework and your main app.
Syntax when using ln utility,
$ ln -s "$SRCROOT_MAIN/Carthage/Build" "$SRCROOT_DEVELOPING_FRAMEWORK/Carthage/Build"
Step 2. Framework Replacement

2.1 Remove your developing framework in Xcode > YOUR_MAIN_APP > General > Linked Frameworks and Libraries (that is, the one located in $(SRCROOT_MAIN)/Carthage/Build/iOS).
2.2 Add $(DEVELOPING_FRAMEWORK_NAME).xcodeproj (found in directory pointed by $(SRCROOT_MAIN)/Carthage/Checkouts/$(DEVELOPING_FRAMEWORK_NAME) symbolic link) into your main app
2.3 Build the developing framework product for device and simulator
2.4 Add the new developing framework auto-detected by Xcode in Xcode > YOUR_MAIN_APP > General > Linked Frameworks and Libraries.
2.5 Add $(DEVELOPING_FRAMEWORK_NAME).framework as a target dependency by adding $(DEVELOPING_FRAMEWORK_NAME).framework in Xcode > YOUR_MAIN_APP > Build Phases > Target Dependencies.
2.6 Copy $(BUILT_PRODUCTS_DIR)/$(DEVELOPING_FRAMEWORK_NAME).framework to $(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/$(DEVELOPING_FRAMEWORK_NAME).framework by adding a new input file $(BUILT_PRODUCTS_DIR)/$(DEVELOPING_FRAMEWORK_NAME).framework and a new output file $(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/$(DEVELOPING_FRAMEWORK_NAME).framework in Xcode > YOUR_MAIN_APP > Build Phases > Run Script of Carthage Embed Framework.
Reference
Debugging Carthage Dependencies
https://allocinit.io/ios/debugging-carthage-dependencies/
Build Setting Reference
https://developer.apple.com/library/archive/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html

How to embed framework without copying in project

There was a similar question, but the answer is no
I use appodeal-ios-sdk-mobile-adapter. I want to add them to your projects without copying each.
in the Link Binary with Libraries framework added
But I get:
ld: framework not found GoogleMobileAds
clang: error: linker command failed with exit code 1 (use -v to see invocation)
In order to add the framework to your project without copying proeta folder, drag the folder using the (Create groups), and add the path where you have a set framework -> "Build Setting -> Library Search Paths and Framework Search Paths
/Users/username/Framework/AppodealAdapters/**
Well you can open the system directory where all the frameworks are present
that is in the system folder where all the frameworks are installed
Hit Shift+Command+G
and use the below address
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks
Once in the directory copy the framework which you want to use. Take care to not mess around other frameworks in this directory.
Once the frameworks are copied into this directory you can add them to your project just like you include a regular system defined framework.
Try it out it works for me. I am using XCode 7.3.

Using CMake to copy .frameworks to iOS app bundle

A library I'm using recently switched to distribution as a .framework.
In my existing CMake file, I've been successful at getting it to link with my iOS app, but am getting:
dyld: Library not loaded: #rpath/Pizza.framework/Pizza
Referenced from: /var/mobile/Containers/Bundle/Application/D71ED298-C287-4B2F-8AFA-710A14C06D75/pizzashop.app/pizza
Reason: image not found
when I install it from Xcode. If I manually add it to my xcode project, in the "embedded binaries" section then I'm good (see image below)
So I've concluded that the problem is getting the .framework into my app bundle. I've come across this question and looked at the linked QT example, but I'm still trying to orient myself here as I'm finding the syntax a bit opaque.
Is CMake's BundleUtilities what I want to use here? In looking at the BundleUtilities example I'm a bit lost:
set(APPS ...) # paths to executables
set(DIRS ...) # directories to search for prerequisites
INSTALL(CODE "
include(BundleUtilities)
fixup_bundle(\"${APPS}\" \"\" \"${DIRS}\")
" COMPONENT Runtime)
Is this OSX-specific or can I apply it similarly to iOS?
We didn't end up finding an ideal solution, and ended up doing things a bit more manually than preferred:
We added a custom command we run after the build is complete, but before it is packaged (see CMake's add_custom_command).
The custom command does the following:
creates a Frameworks directory under our app bundle folder (make sure it's somewhere where it will get copied in your packaging process).
we use cp -aH to copy all frameworks into this Frameworks directory
we then re-sign each framework in this directory using:
codesign --force --verbose Computers.framework --sign "$2"
Add the Frameworks directory to your search paths:
set_target_properties(${EXE_NAME} PROPERTIES
XCODE_ATTRIBUTE_LD_RUNPATH_SEARCH_PATHS "#executable_path/Frameworks")

How to attach library to ios project?

I'm developing a command line tool app that uses AppList library. But when i trying to run in on my device, the error occured:
dyld: Library not loaded: /usr/lib/libapplist.dylib
Referenced from: /usr/bin/testApp
Reason: image not found
Is it possible to attach libapplist.dylib to project package?
All dylibs have an installation path inside mach-o header. That path is placed in your application import section so that linker could find the dylib. When your process is launched dyld searches that path for the dylib.
You can check that path like this:
otool -D libapplist.dylib
That's what dyld is telling you - it couldn't find the dylib. You have two options:
Place your dylib where it needs to be
Change installation path.
If it's your dylib you can change it inside xcode project settings of your dylib - search for installation directory. When you rebuild dylib and application linking it they will contain the new path.
If you can't recompile the dylib then you need to change it manually. Here is how you can do it:
install_name_tool -id #executable_path/libapplist.dylib libapplist.dylib
#executable_path tells the linker to search for dylib where application executable is located.
Its simple go to build phases and link framework and library
go to build phases in app settings
add the library which you want

Validation Error: The bundle contains disallowed file 'Frameworks'

I'm having the same issue as this guy, this guy, and this guy (nota bene, I'm actually not sure if they're all guys, per se).
They all ended up finding their own solutions, but none of them apply to my issue. I'm using Xcode 6.1 in my iOS 8 app with an included extension. The app and the extension both rely on an included framework. When I try to submit the app to the Store, the validation warning I get is "ERROR ITMS-9000: Invalid bundle. The bundle at 'xxxxx.appex' contains disallowed file 'Frameworks'".
I can't even find a file called Frameworks. The shared framework is supposed to be saved at /Library/Frameworks, which is Apple's recommended save location. The project also uses Cocoapods, which strikes me as the only other possible culprit, since it has references in its configuration files to $FRAMEWORK_PATH (though the build folder doesn't include a file or folder with that name).
OK for future viewers here's the fix:
When you create your own iOS framework (I'm using Xcode 6.1) when you build it the final product contains a 'Frameworks' folder in the framework bundle itself. i.e. MyFramework.framework/Frameworks. This happens even if you don't specify a copy files/embed frameworks build phase.
What you have to do is to go into you framework bundle, find the empty frameworks folder and delete it. This should not affect your app's functionality in any way. Then build your app and check that the embedded framework doesn't have a Frameworks folder as planned.
Your archive should now not contain the offending folder and the error should be gone!
I changed build settings > Packaging > Define modules set to YES in my extension and watch app target. Works fine for me.
In my case the solution was the following :
Try to create the script there 'problematic target' -> Build Phases' then click on + and select New Run Script Phase, the run script should go after all others. Insert there :
cd "${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/"
if [[ -d "Frameworks" ]]; then
rm -fr Frameworks
fi
Then clean the project and try to create an archive once again. These answer was provided in the following issue :
https://github.com/CocoaPods/CocoaPods/issues/4203
I hope this help you.
Continuing to work with this, I noted that my Share Extension had an "Embed Frameworks" Build Phase, with the Destination set to the "Frameworks" directory. I changed it to "Shared Frameworks" and the error has gone away.
However, another error remains: "... contains disallowed nested bundles". I thought this was a sort of umbrella error warning as a result of the original. I'll open another question for that one.

Resources