跳至主要内容

Build a Universal Framework in iOS using Swift

原文链接

I have searched a lot for solid resources which would help me build a custom Universal Framework on iOS. I have tried implementing multiple techniques but they didn’t help me complete the task. Seeking and reading deeply a lot helped me understand more about it and eventually I build a Universal framework.

I want to share how I built this framework in an easy way,

Create Cocoa Touch Framework:

Open Xcode, File → New → Project. Under iOS, select Framework & Library and choose Cocoa Touch Framework click “Next”

Image for post
Image : Choosing Template for New Project

Enter your framework name and click “Next”.

Image for post
Image : Name the Framework, Choose scripting Language

And choose your project location and create the project.

Select your Project, Choose Target → Project Name → Select Build Settings Tab. Always set “Enable Bitcode” to “Yes”, before building a Framework.

Build your required feature and functionality in the project. You can maintain multiple storyboards if required. The Framework project looks mostly like a normal SPA Project, the only difference is that you can only Compile/Build your Framework you can’t actually run them in the same project.

If the project is build only in “Simulator”, it would generate architectures which would let the framework to run specifically on simulators not on Devices. Or if the project is build in “Device”, it would generate architectures which would let the framework to run only on devices.

So build the project on both “Simulator” and “Device”.

The framework would be generated in the Product folder.

Image for post
Image : Framework generation

The Framework would be generated in the Products Directory of the Project, which has two seperate Folders of unique Framework which would run on Simulator and Device independently.

Device Framework: It contains architectures which will only build for Physical Devices.

Device Framework — runs/executes only on physical Devices

Simulator Framework: It contains architecture which iswill build only for Simulators.

Image for post
Simulator Framework — runs/executes only on Simulator

From a developer standpoint, developer would want to get a Universal framework, which runs on both simulator and Device.

Most of the Popular Framework are built as Universal Framework like Mixpanel, Fabric etc.,

I wrote a “Run Script” which generates the Universal Framework.

Select Project Target → Edit Schema → Archieve → Post-actions → Press “+” → New Run Script Action.

UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal# Make sure the output directory existsmkdir -p "${UNIVERSAL_OUTPUTFOLDER}"# Next, work out if we're in SIM or DEVICExcodebuild -target "${PROJECT_NAME}" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean buildxcodebuild -target "${PROJECT_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos  BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build# Step 2. Copy the framework structure (from iphoneos build) to the universal foldercp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/"# Step 3. Copy Swift modules from iphonesimulator build (if it exists) to the copied framework directoryBUILD_PRODUCTS="${SYMROOT}/../../../../Products"cp -R "${BUILD_PRODUCTS}/Debug-iphonesimulator/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule/." "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule"# Step 4. Create universal binary file using lipo and place the combined executable in the copied framework directorylipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_PRODUCTS}/Debug-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}"# Step 5. Convenience step to copy the framework to the project's directorycp -R "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework" "${PROJECT_DIR}"# Step 6. Convenience step to open the project's directory in Finder
open "${PROJECT_DIR}"
fi

This run script would be executed after the Archive is completed. And the Universal Framework would be generated and opened in project directory itself.

Executing/Running the Framework:

(i) Create a new project:

File → New → Project. Select iOS and choose “Single View Application” and click “Next”.

Image for post
Image : Choose Template

And set options like Product Name, Team, etc. and create the Project.

Image for post
Image : Name the project, Select language

(ii) Set the Framework Dependencies:

Copy/Drag the Universal Framework to this project. While coping the framework in Project Explorer, check “Copy items if needed”.

Select the Project, Choose Target → Project Name → Select General → Scroll to “Embedded Binaries”. Press “+” and Add the framework.

Image for post
Image : Add the Framework in Embedded Binaries

If there are multiple Framework with same name in “Linked Frameworks and Libraries”, delete other ones, and just keep only one.

Nice. Now the universal framework will run on both simulators and Devices. But still there is a problem.

(iii) Remove Unused Architectures:

Let’s assume an application uses our custom universal framework on production, we we need to remove unused architectures. Because Apple doesn’t allow the application with unused architectures to the App Store.

Select the Project, Choose Target → Project Name → Select Build Phases → Press “+” → New Run Script Phase → Name the Script as “Remove Unused Architectures Script”.

Always this script should be placed below “Embed Frameworks”.

To achieve this, I wrote Custom Run Scripts,

FRAMEWORK="BetterHQ"FRAMEWORK_EXECUTABLE_PATH="${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/$FRAMEWORK.framework/$FRAMEWORK"EXTRACTED_ARCHS=()for ARCH in $ARCHSdolipo -extract "$ARCH" "$FRAMEWORK_EXECUTABLE_PATH" -o "$FRAMEWORK_EXECUTABLE_PATH-$ARCH"EXTRACTED_ARCHS+=("$FRAMEWORK_EXECUTABLE_PATH-$ARCH")donelipo -o "$FRAMEWORK_EXECUTABLE_PATH-merged" -create "${EXTRACTED_ARCHS[@]}"rm "${EXTRACTED_ARCHS[@]}"
rm "$FRAMEWORK_EXECUTABLE_PATH"
mv "$FRAMEWORK_EXECUTABLE_PATH-merged" "$FRAMEWORK_EXECUTABLE_PATH"

This run script removes the unused Simulator architectures only while pushing the Application to the App Store.

Now the Custom iOS Framework is complete and it remains Universal.

Hope this article is useful for people looking to create custom iOS framework, Please ❤️ to recommend this post to others 😊. Let me know your feedback. :)

评论

此博客中的热门博文

Resolving errSecInternalComponent errors during code signing

原文链接 One code signing issue I commonly see, both here on DevForums and in my Day Job™ with DTS, is that the codesign command fails with errSecInternalComponent. This issue crops up in a wide variety of circumstances and the correct fix depends on the specific problem. This post is my attempt to clarify the potential causes of this error and help folks resolve it. If you have any questions or comments about this, please start a new thread, tagging it with Code Signing so that I see it. Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com" Resolving errSecInternalComponent errors during code signing In some circumstances the codesign command might fail with the error errSecInternalComponent. For example: % codesign -s "Apple Development" "MyTrue" MyTrue: errSecInternalComponent This typically affects folks who are signing code in a nonstandard environm...

iOS:检测使用VPN或Proxy

参考链接: https://www.jianshu.com/p/c3b950dbf86a https://gist.github.com/PramodJoshi/4faad4c91f7dcb4eb9b06be8390c01db http://noodlecode.net/2018/04/check-if-ios-app-is-connected-to-vpn 第一种方法 需要导入框架CFNetwork 然后,这个方法是mrc的:需要添加-fno-objc-arc的flag 代码如下: + ( BOOL )getProxyStatus { NSDictionary *proxySettings = NSMakeCollectable ([( NSDictionary *) CFNetworkCopySystemProxySettings () autorelease]); NSArray *proxies = NSMakeCollectable ([( NSArray *) CFNetworkCopyProxiesForURL (( CFURLRef )[ NSURL URLWithString: @"http://www.google.com" ], ( CFDictionaryRef )proxySettings) autorelease]); NSDictionary *settings = [proxies objectAtIndex: 0 ]; NSLog ( @"host=%@" , [settings objectForKey:( NSString *)kCFProxyHostNameKey]); NSLog ( @"port=%@" , [settings objectForKey:( NSString *)kCFProxyPortNumberKey]); NSLog ( @"type=%@" , [settings objectForKey:( NSString *)kCFProxyTypeKey]); if ([[settings object...

去广告DNS设置,国内ADGuard DNS方案,手机电脑iOS去广告,保护隐私

 原文链接 之前分享过使用mac系统搭建adguard home,这几个月用下来零零散散基本上也被弃用了。主要原因是因为需要保持电脑一直开机。但是我的电脑是笔记本,存在移动各个地域的情况,也就是说只能够屏蔽电脑自身,对于手机而言不太现实。今天偶然发现dnspod推出了高级版的公共解析。dnspod背靠腾讯云,肯定是合法合规的公共解析服务,这个高级版用起来不错。 国内自己搭建解析服务是违法行为,所以这也是为什么使用dnspod的原因。 后台截图 开始使用 首先我们先进入dnspod的公共解析页面,点击开始使用。 专业版公共解析 dnspod会提供几种预设,我们选择「开发者」即可 开发者 然后你就成功的申请到自己个人使用的dns了! 更新拦截规则 我们可以将常见的广告过滤规则加入到dns中。我们在顶部选项卡中选择「拦截规则」。 拦截规则设置 打开adguard adguard 绑定iOS设备 推荐使用描述文件的方式,删除配置时删除描述文件即可。 描述文件 绑定macOS 推荐使用描述文件的方式,删除配置时删除描述文件即可。 描述文件 mac需要在「系统偏好设置」的「网络」中查看是否正在运行。 代理 如果没有运行需要点击「···」来启动服务。 启动服务 绑定路由器 找到自己路由器的DHCP设置,修改dns,然后记得绑定自己的ip。 修改dns 绑定ip 费用 目前有300万次/月的免费额度,但没有超出之后的价格。300万次一个人比较难用完,可以放心使用。 我个人使用iOS设备两台、智能家居、电脑两台,日均请求数大致2万/日。 判断是否搭建成功 可以通过查看日志的方式,日志大概有半小时到一小时的延迟,请耐心等待。