95

I have a Swift project that uses a ObjC dynamic framework, the framework had to be linked with and embedded into my project. The project runs OK in devices, when submitted to App Store, the error occurred during validation: Found an unexpected Mach-O header code: 0x72613c21

Below are the validation logs:

2015-10-12 02:32:33 +0000 [MT] Beginning distribution assistant for archive: MusicFans, task: Validate
2015-10-12 02:32:33 +0000 [MT] Automatically selecting the only availaable distribution method <IDEDistributionMethodiOSAppStoreValidation: 0x7f851c1d96c0>
2015-10-12 02:32:34 +0000 [MT] [OPTIONAL] Didn't find archived user entitlements for <DVTFilePath:0x7f851b42db10:'/Users/panzhansheng/Library/Developer/Xcode/Archives/2015-10-11/MusicFans 11-10-15 上午8.29.xcarchive/Products/Applications/MusicFans.app/Frameworks/libswiftDispatch.dylib'>: Error Domain=NSCocoaErrorDomain Code=4 "Item at "/Users/panzhansheng/Library/Developer/Xcode/Archives/2015-10-11/MusicFans 11-10-15 上午8.29.xcarchive/Products/Applications/MusicFans.app/Frameworks/libswiftDispatch.dylib" did not contain a "archived-expanded-entitlements.xcent" resource." UserInfo={NSLocalizedDescription=Item at "/Users/panzhansheng/Library/Developer/Xcode/Archives/2015-10-11/MusicFans 11-10-15 上午8.29.xcarchive/Products/Applications/MusicFans.app/Frameworks/libswiftDispatch.dylib" did not contain a "archived-expanded-entitlements.xcent" resource.}
2015-10-12 02:32:34 +0000 [MT] [OPTIONAL] Didn't find archived user entitlements for <DVTFilePath:0x7f8529a08050:'/Users/panzhansheng/Library/Developer/Xcode/Archives/2015-10-11/MusicFans 11-10-15 上午8.29.xcarchive/Products/Applications/MusicFans.app/wavpack.framework'>: Error Domain=NSCocoaErrorDomain Code=4 "Item at "/Users/panzhansheng/Library/Developer/Xcode/Archives/2015-10-11/MusicFans 11-10-15 上午8.29.xcarchive/Products/Applications/MusicFans.app/wavpack.framework" did not contain a "archived-expanded-entitlements.xcent" resource." UserInfo={NSLocalizedDescription=Item at "/Users/panzhansheng/Library/Developer/Xcode/Archives/2015-10-11/MusicFans 11-10-15 上午8.29.xcarchive/Products/Applications/MusicFans.app/wavpack.framework" did not contain a "archived-expanded-entitlements.xcent" resource.}
2015-10-12 02:32:34 +0000 [MT] [OPTIONAL] Didn't find archived user entitlements for <DVTFilePath:0x7f850da13de0:'/Users/panzhansheng/Library/Developer/Xcode/Archives/2015-10-11/MusicFans 11-10-15 上午8.29.xcarchive/Products/Applications/MusicFans.app/FLAC.framework'>: Error Domain=NSCocoaErrorDomain Code=4 "Item at "/Users/panzhansheng/Library/Developer/Xcode/Archives/2015-10-11/MusicFans 11-10-15 上午8.29.xcarchive/Products/Applications/MusicFans.app/FLAC.framework" did not contain a "archived-expanded-entitlements.xcent" resource." UserInfo={NSLocalizedDescription=Item at "/Users/panzhansheng/Library/Developer/Xcode/Archives/2015-10-11/MusicFans 11-10-15 上午8.29.xcarchive/Products/Applications/MusicFans.app/FLAC.framework" did not contain a "archived-expanded-entitlements.xcent" resource.}
2015-10-12 02:33:07 +0000 [MT] Canceled distribution assistant

If I remove the dynamic framework from the project’s Embedded frameworks section, then it can pass the validation, but crashes when run in devices due to the missing image of my dynamic framework, any idea? BTW, dynamic framework has bitcode disabled, so my project also disables bitcode, and is not codesigned.

mokagio
  • 16,391
  • 3
  • 51
  • 58
pzs7602
  • 1,233
  • 1
  • 10
  • 9
  • Did you use CocoaPods? – Volodymyr Oct 25 '15 at 07:15
  • 1
    I got the Mach-O error when using XCode 12.4, and the Facebook iOS SDK (via Swift Package Manager). After hours of tearing my hair, I simply had to select Facebook iOS SDK 9.3 instead of the latest version. Before figuring this out I've fiddled with setting generating bitcode=no, arm64-compilation options, build environment variables, etc, so don't go down that rabbit hole. The Mach-O error displayed when building and attempting upload to AppStore, and it first seemed to have something to do with my iOS Widget Extension (appex a.k.a. plugin) which wasn't the case. – Fredrik Johansson Nov 18 '21 at 15:55

29 Answers29

82

Check two things for every framework:

  1. The Mach-O type of the framework (in Build Settings of the framework target)

Mach-O

  1. Whether you copy the framework by putting it in Build Phases -> Embed Frameworks (or Copy Bundle Resources as mentioned by a. brooks hollar)

Embed frameworks

If the Mach-O type of the framework is "static library", it should not be put in embed frameworks; if the Mach-O type is "dynamic library", it should be put in embed frameworks.

In my case it was SocketRocket I added manually which has both a static library target and a dynamic library target with the same framework name. After deleting the static library target and only embed the dynamic library framework, the problem disappears.


Note:

If you don't have access to the source project, you can manually check if it's a dynamic or static framework using this approach: https://stackoverflow.com/a/32591983/308315

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
Cosyn
  • 4,404
  • 1
  • 33
  • 26
25

I don't know why but somehow pods appeared to become an embedded binary. So I had to remove it, pod install once more, and the issue disappeared.

enter image description here

On Xcode 11 you need to change it to Do Not Embed in the Frameworks, Libraries, and Embedded Content.

Jonathan.
  • 53,997
  • 54
  • 186
  • 290
Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
23

Double check Build Phases -> Copy Bundle Resources for a framework or other binary that shoundn't be there.

In my case, it was a 3rd party library (Parse) that I had added.

  • In my case it was a file which was copied to the project. I removed this and the error disappeared. I then re-added the file and it seems to be fine now. – Davie Brown Mar 31 '16 at 19:51
  • 2
    If you click on the "Show log" button, and then look at the "verbose" logfile, you may see the name of the offending framework. At least it was there for me. – Stan James Jul 11 '16 at 19:25
  • +1 for this answer, basically in { App Target } -> Build Phases -> Copy Bundle Resources I deleted anything related to CocoaPods in there - made this issue go away – Henry Jun 11 '20 at 12:53
23

In my case, I have a framework manually dragged into my project, and it is listed in General->Embedded Binaries, which cause error Found an unexpected Mach-O header code: 0x72613c21. After i moved it from Embedded Binaries to Linked Frameworks and Libraries, problem solved.

Ke Yang
  • 881
  • 1
  • 12
  • 21
16

If you (potentially but not necessarily) used FirebaseSDK (especially Analytics) and get Mach-O error while running, archiving and uploading, try this:

Add this as a build post-actions script.

rm -rf "${TARGET_BUILD_DIR}/${TARGET_NAME}.app/Frameworks/FirebaseAnalytics.framework"
rm -rf "${TARGET_BUILD_DIR}/${TARGET_NAME}.app/Frameworks/GoogleAppMeasurement.framework"
rm -rf "${TARGET_BUILD_DIR}/${TARGET_NAME}.app/Frameworks/FIRAnalyticsConnector.framework"

if you added widgets and/or other plugin extensions add these lines as well:

rm -rf "${TARGET_BUILD_DIR}/${TARGET_NAME}.app/PlugIns/FirebaseAnalytics.framework"
rm -rf "${TARGET_BUILD_DIR}/${TARGET_NAME}.app/PlugIns/GoogleAppMeasurement.framework"
rm -rf "${TARGET_BUILD_DIR}/${TARGET_NAME}.app/PlugIns/FIRAnalyticsConnector.framework"

enter image description here

Brief explanation: The below quote is from the relevant discussion under https://github.com/firebase/firebase-ios-sdk/issues/6472 explains the reason behind this problem.

... so right now the firebase framework artifacts are being copied into the /Frameworks folder of the ipa and are embedded. The frameworks themselves are static frameworks.... If you delete them from the xcarchive manually, everything works as expected.

Basically many of the answers here point to the right direction, but the problem remains because the solution is to remove the unwanted artifacts every single time. Currently (Jan 2021) AFAIK there is no other way to solve it.

Rufat Mirza
  • 1,425
  • 14
  • 20
  • 5
    This is what worked for me (even if you're not using firebase). Actually this answer did not help me as it is, I found the solution in this comment precisely: https://github.com/firebase/firebase-ios-sdk/issues/6472#issuecomment-740186615 – aiwis31 Jan 14 '21 at 11:27
  • 3
    This also worked for me, I was using Firebase with SPM and a Widget, Thanks you! – Luca Jan 18 '21 at 20:41
  • 3
    Make sure to select your app from `Provide build settings from [your_app_target]` in the select box. – user482594 Feb 20 '21 at 20:13
  • 2
    Same as @Luca : using Firebase with SPM (without Widgets) and this also worked for me when archiving and uploading to AppSore. Thank you ! – swifthing May 07 '21 at 07:00
14

Scheme->Edit Scheme->Archive->Post Action->Click '+' -> New Run Script Action-> paste below code

LOGFILE="${ARCHIVE_PATH}/static-frameworks.log"
echo "Removing static frameworks from ${WRAPPER_NAME} archive" > $LOGFILE
find "${ARCHIVE_PRODUCTS_PATH}/Applications/${WRAPPER_NAME}" -name '*.framework' -print0 | while IFS= read -r -d '' fm; do
    name=$(basename "${fm}" .framework)
    target="${fm}/${name}"
    echo "Checking: ${fm}" >> $LOGFILE
    if file "${target}" | grep -q "current ar archive"; then
        rm -rf "${fm}"
        echo "Removed static framework: ${fm}" >> $LOGFILE
    fi
done

enter image description here

Raees Madathil
  • 291
  • 1
  • 3
  • 14
10

I had a same issue. I did a couple of things and my issue went away. I believe My issue was related to Cocoapods version but you can try couple of things here.

First clean DerivedData by:

1.Turn off Xcode

2.Go to ./Users/YourFile/Library/Developer/Xcode/DerivedData

3.Delete everything from this folder

4.Run Xcode

5.Build&Clean

Second for some reason, with latest cocoa pods version 0.39, I was getting following warning when I ran pod install

[!] The MY_TARTGET target overrides the EMBEDDED_CONTENT_CONTAINS_SWIFT build setting defined in `Pods/Target Support Files/Pods/Pods.release.xcconfig'. This can lead to problems with the CocoaPods installation

I've downgraded cocoapods by removing all installed cocoapods:

sudo gem uninstall cocoapods

then install older version:

sudo gem install cocoapods -v 0.38.1

This removed the warning and I was able to remove the error, "unexpected Mach-O header code: 0x72613c21"

CodeOverRide
  • 4,431
  • 43
  • 36
8

For me, I had Fabric binaries in my Carthage /usr/local/bin/carthage copy-frameworks run script.

After removing those, it fixed the issue.

I'm using Xcode Version 9.4.1 (9F2000)

Jacob
  • 1,052
  • 8
  • 10
  • Thanks, this works for me. I'm using Xcode `Version 10.3 (10G8)`. However I'm wondering if we still need to copy over `Fabric.framework` and `Crashlytics.framework`. – Hungju Lu Sep 04 '19 at 15:20
  • I can't answer that question. But there is a run script that fabric requires you to add to your build scripts invoking `${PROJECT_DIR}/Carthage/Build/iOS/Fabric.framework/run`. My guess is that this is performing whatever needs to be done. – Jacob Oct 20 '19 at 15:36
8

In XCode 11, click on the project file, select your target, then on General tab expand Frameworks, Libraries, and Embedded Content, change Embed & Sign to Do Not Embed.

Display Name
  • 4,502
  • 2
  • 47
  • 63
  • Works under Xcode Version 11.2.1 (11B500). I have read the logs and changed this for all my third party frameworks. Some "libSwift_XXX" frameworks also were repaired since they did not appear anymore after this change. – Darkwonder Mar 05 '20 at 12:06
  • This results in a runtime error in my case `dyld: Library not loaded` – Adamski Mar 31 '20 at 10:20
  • all my lib are "Do not embed" and still the same error. – firetrap Apr 15 '20 at 22:12
7

Edit: This looks to be fixed in Xcode 8 Beta 3.

I'm currently experiencing this issue with Xcode 8 Beta 2 and Swift 3 with pure-swift Pods (which seems to be causing the issue).

Adding the following to my Podfile has fixed the problem.

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES'] = 'NO'
    end
  end
end

Credit goes to this comment by YuAo on GitHub.

Kilian
  • 2,122
  • 2
  • 24
  • 42
6

Take a look at this Apple documentation Embedding Frameworks in An App go to the section Embedded Static Libraries they explain this error like this:

This is caused by placing a static library in a bundle structure that looks like a framework; this packaging is sometimes referred to by third party framework developers as a static framework. Since the binary in these situations is a static library, apps cannot embed it in the app bundle.

They also provide a way to fixed:

you can solve this error by identifying the static framework and removing it from the Embedded Binaries section.

You accomplish this by following the instructions in Inspecting A Binary's Linkage

andreskwan
  • 1,152
  • 1
  • 13
  • 13
6

2021! UPDATED

Here is the hack for this:

  1. Build the app with the Pods_Runner.framework marked as Embed & Sign

enter image description here

  1. Once it's built. Go to the same place and set the Pods_Runner.framework to Do Not Embed.

enter image description here

  1. Go to Product -> Archive -> Submit the archive and it will all work as expected.

credits: lifenautjoe

Akbar Pulatov
  • 2,955
  • 2
  • 16
  • 33
5

Adding below script into Post-Actions of Archive helped me overcome this issue. It removes static-frameworks which are unrelated.

# https://github.com/firebase/firebase-ios-sdk/issues/6472
LOGFILE="${ARCHIVE_PATH}/static-frameworks.log"
echo "Removing static frameworks from ${WRAPPER_NAME} archive" > $LOGFILE
find "${ARCHIVE_PRODUCTS_PATH}/Applications/${WRAPPER_NAME}" -name '*.framework' -print0 | while IFS= read -r -d '' fm; do
    name=$(basename "${fm}" .framework)
    target="${fm}/${name}"
    echo "Checking: ${fm}" >> $LOGFILE
    if file "${target}" | grep -q "current ar archive"; then
        rm -rf "${fm}"
        echo "Removed static framework: ${fm}" >> $LOGFILE
    fi
done

A comment from Firebase issue: https://github.com/firebase/firebase-ios-sdk/issues/6472#issuecomment-771351512

iamburak
  • 3,508
  • 4
  • 34
  • 65
4

Remove them from Embedded Binaries and add them to Linked Frameworks and Libaries, then Archive again and upload to store.

Nguyễn Anh Tuấn
  • 1,023
  • 1
  • 12
  • 19
  • 1
    You should ensure you don't need the frameworks to be embedded (copied into the Frameworks folder) else you will get a dylib not found exception when you try to run on a device. This can be deceptive because it will work fine on the simulator. – dcrow Jan 09 '19 at 00:43
4

In my case in xcode 11, i resovle issue to set do not embeded in genral -> frameworks,libraies and embedded content enter image description here

3

Check and looking for duplicate frameworks and Libraries in Link Binary With Libraries and Embed Frameworks in Build Phases tab of your project.

Only should be in one side ...

Jirson Tavera
  • 1,303
  • 14
  • 18
2

I ran into this error with a FacebookSDK framework. I removed it from the Embed frameworks list in Build Phases and it solved the issue. Inspect the log and find the framework causing the error as mentioned by others.

Adam Mendoza
  • 5,419
  • 2
  • 25
  • 31
2

I encountered this issue in a very simple Framework project. I only had one target in my project for the framework and it was building fine. I wanted to add some unit tests, so I added two new targets: an iOS Unit Testing Bundle target, and a Single View Application target (to act as a host app for the tests). However, both new targets were throwing this error.

I discovered that the problem was caused by the Mach-O Type build setting on the new targets being set to Static Library. Apparently the new targets were inheriting the Mach-O type from the parent Project. When I set the Mach-O Type to the correct settings ("Bundle" for unit tests and "Executable" for the host app) it all worked! I also had to clean build, delete derived data and reset the simulator to get the new settings to take.

shocking
  • 848
  • 12
  • 16
2

In my case, I selected the copy when installing and worked for me

enter image description here

zeytin
  • 5,545
  • 4
  • 14
  • 38
1

In my case it was caused by change of Developer certificate/team.

I start project with personal Dev Apple ID and by the time I changed it to my work ID. When we try to export .xcarchive then "Match-O header" fail did appear. Nothing described here or at other sites don't change anything.

When I change Team back to my personal ID, error logs shows missing .xcent files for only few frameworks now (most recently added). So I start whole new project, paste all the sources and files, then run cocoapod and everything works as expect.

Pacyjent
  • 409
  • 5
  • 6
1

My way of fixing this was to get back to Fabric/Crashlytics installation via Cocoapods instead of fancy Carthage setup (which appeared buggy).

SoftDesigner
  • 5,640
  • 3
  • 58
  • 47
0

I had this problem with a manually-added PFFacebookUtils framework in a Swift 2.3 non-CocoaPods project. I fixed it by removing said framework from the embedded frameworks section of the "General" page of the build target and linked it in Build Phases -> Link Binary With Libraries

johncederholm
  • 151
  • 4
  • 7
0

Given the amount of time I've poured into finding this problem, it was caused by Cocoapods. I was working on a Workspace that had both iOS apps and a command line tool to kick off some pipelines and stuff. My file looked like this:

target 'AppPipe' do
  platform :osx, '10.14'
  project 'AppPipe/AppPipe.xcodeproj'
  pod 'Yams'

  post_install do |installer|
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
        config.build_settings['MACH_O_TYPE'] = 'staticlib'
      end
    end
  end
end

I thought the pods_install hook was local to the scope I was writing it in, but it wasn't! It actually turned all of my target's configs into staticlibs! This fixed it:

target 'AppPipe' do
  platform :osx, '10.14'
  project 'AppPipe/AppPipe.xcodeproj'
  pod 'Yams'
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
        # Only AppPipe always gets staticlib
        if target.name == 'AppPipe'
          config.build_settings['MACH_O_TYPE'] = 'staticlib'
        end
    end
  end
end
Lucas van Dongen
  • 9,328
  • 7
  • 39
  • 60
0

If you have an Xcode project for a static framework, your test target must have "Bundle" value for MACH_O_TYPE build setting:

enter image description here

Viktor Malyi
  • 2,298
  • 2
  • 23
  • 40
0

When embedding bundles from library targets they may produce executables even with Mach-O Type = Bundle. So we can simply delete these executables from xcarchive (Products/Applications/APP_NAME/YourBundle.bundle/YourBundle)

schmidt9
  • 4,436
  • 1
  • 26
  • 32
0

Use this code as a script build-phase instead of a scheme post-build action. I found out of all the answers, this works the best:

# Fixes binary framework bug, see:
# https://developer.apple.com/documentation/xcode-release-notes/xcode-12_4-release-notes#Swift-Packages
# https://github.com/firebase/firebase-ios-sdk/issues/6472

echo "Removing static frameworks from ${TARGET_NAME}.app"
find "${BUILT_PRODUCTS_DIR}/${TARGET_NAME}.app/" -name '*.framework' -print0 | while IFS= read -r -d '' fm; do
    name=$(basename "${fm}" .framework)
    target="${fm}/${name}"
    echo "Checking: ${fm}"
    if file "${target}" | grep -q "current ar archive"; then
        rm -rf "${fm}"
        echo "Removed static framework: ${fm}"
    fi
done
Ethan Allen
  • 14,425
  • 24
  • 101
  • 194
0

I encountered the same problem days ago, the root cause of this problem is:

** Some static libraries was wrongly embeded in the .app bundle. **

You can search .a or static .framework in the Products/*.app bundle in Xcode, and check out the project carefully, remove redundant static libraries.

Xueshi
  • 124
  • 2
  • 6
-1

One of the possible reason is nested framework.

Inspired by Golang, I recently refactored my project's code from:

App(with all the code in one target)

to ("->" means link with, ABCD means framework)

B -> C
A -> B

App -> A
App -> B
App -> C

It turns out that nested framework is not officially supported by Apple. So I had to flatten the dependency tree to:

App -> D

D -> A
D -> B
D -> C
rpstw
  • 1,582
  • 14
  • 16
-2

I had same issue in Xcode 8 beta 3. I fixed it removing Fabric and Crashlytics from Linked frameworks (Project => Target => General page) (source)

topascz
  • 61
  • 1
  • 3