Uncategorized

Here are some experiences for brothers who plan to write a framework to support IOS, so that you can take time to study.

The solution I molded is as follows:

  1. The framework should be written in Objective-C: – The reason for choosing objective-C is because
    it has not changed anything anymore, running stably. Currently every year Apple launchs a new version of Swift, so if you use swift to write, it is very painful, every time you have to update for the whole project is using a new swift. This is also why you see FireBase or FacebookCore using Objective-C to write frameworks for them. Although I know a lot of people who don’t necessarily know Objective-C, the syntax is harder. But life, you want to be a Framework, you are also high, meaning you should know both languages.

2. When working as a framework, you have to support both simulators and devices. So you have to have scripts to build for them. Specifically, xcframework.

3. You should create a project demo separate from the project framework. In doing so, the test is more standard, sometimes it is easier to detect errors.

I will proceed to create a framework for you to understand.

Open Xcode to create a new Framework as pictured.

Name it CodeToanBug(or as you like).

Adjust the support version as low as possible, for example, I choose 9.0:

Create a workspace using File -> New -> Workspace. Name it CodeToanBug or arbitrary.

Then turn off the open project, but still open workspace. Then drag the CodeToanBug file.xcodeproj to the Workspace as pictured:

After drag and drop

The goal is to create an additional demo project in the Workspace.

Continue to select the CodeToanBug target and then go to the Organization to add codetoanbug to the copyright identity, and class Prefix CTB (abbreviated codetoanbug) so that it adds the prefix itself when creating a new file.

Now add 1 function plus 2 numbers to the framework features. Add a file named CTBMath.h as pictured:

Write the function in the .h file:

<Foundation oundation.h="">#import

NS_ASSUME_NONNULL_BEGIN

@interface CTBMath : NSObject

- (int)addTwoNumbers:(int)number1 secondnumber:(int)number2;

@end

NS_ASSUME_NONNULL_END</Foundation>

In the .m:

#import "CTBMath.h"

@implementation CTBMath

Add two numbers
@param number1 number1 need add
@param number2 number2 need add
- (int)addTwoNumbers:(int)number1 secondnumber:(int)number2 {
    return number1 + number2;
}
@end

Then click the CTBMath.h file and select as picture:

Or if you know how to drag and drop, go to the CodeToanBug target and select Build phases to add public files so that anyone who uses this framework can read:

OK that’s the framework.

The next step is how to build for device and simulator.

Create a universal framework as follows:

Select File -> New target -> Other -> Aggregate:

Name it CodeToanBugFramework or arbitrary.

Now that it appears to add this target, you select it and select Build Phases and select New Run Script Phase:

Next add this code to:


# Universal Script

set -e

FRAMEWORK_NAME="CodeToanBug"
IOS_SCHEME_NAME="CodeToanBug"

if [ -d "${SRCROOT}/build" ] ; then
rm -rf "${SRCROOT}/build"
Fi

SIMULATOR_ARCHIVE_PATH="${SRCROOT}/build/${FRAMEWORK_NAME}-iphonesimulator.xcarchive"
DEVICE_ARCHIVE_PATH="${SRCROOT}/build/${FRAMEWORK_NAME}-iphoneos.xcarchive"

OUTPUT_DIR="${SRCROOT}/framework_out_universal/"

# Simulator xcarchieve
xcodebuild archive 
  -scheme ${IOS_SCHEME_NAME} 
  -archivePath ${SIMULATOR_ARCHIVE_PATH} 
  -configuration Release 
  -sdk iphonesimulator 
  SKIP_INSTALL=NO

# Device xcarchieve
xcodebuild archive 
  -scheme ${IOS_SCHEME_NAME} 
  -archivePath ${DEVICE_ARCHIVE_PATH} 
  -sdk iphoneos 
  -configuration Release 
  SKIP_INSTALL=NO

# Clean up old output directory
rm -rf "${OUTPUT_DIR}"

# Create xcframwork combine of all frameworks
xcodebuild -create-xcframework 
  -framework ${SIMULATOR_ARCHIVE_PATH}/Products/Library/Frameworks/${FRAMEWORK_NAME}.framework 
  -framework ${DEVICE_ARCHIVE_PATH}/Products/Library/Frameworks/${FRAMEWORK_NAME}.framework 
  -output ${OUTPUT_DIR}/${FRAMEWORK_NAME}.xcframework

# Delete the most recent build.
if [ -d "${SRCROOT}/build" ] ; then
rm -rf "${SRCROOT}/build"
Fi

Select as pictured, and then click Run:

Run successfully, open the code folder and you will see this:

This is the xcframework that you can build for both.

Create a demo project in this Workspace by File -> New -> Target -> App as pictured:

Named DemoSwift. Remember to select Language as swift.

Then edit the code in the ViewController file.swift as follows:

import UIKit
import CodeToanBug

ViewController class: UIViewController {
    let math = CTBMath()

override func viewDidLoad() {
        super.viewDidLoad()
        print(math.addTwoNumbers(12, secondnumber: 12))
    }
}

But if you want it to understand, you must add CodeToanBug.framework as pictured:

If you build the app that it is up to 24 is already running.

Now conduct a test with an independent project.

Create a new standalone project called TestFramework using swift. Then open the folder and framework_out_universal proceed to drag and drop CodeToanBug.xcframework as pictured(remember to select copy if need):

And edit Embed & Sign(otherwise it will not build the real machine – error Reason: image not found):

Then again the code is as follows:

If running an app that turns out to 25, the Framework already works.

Here is the code you can download refer to:

https://github.com/codetoanbug/CodeToanBug-IOS

If you have any suggestions, please comment below. Thank you for reading.

User Avatar

Code Toàn Bug

Code nhiều bug nhiều!

Leave a Reply

Your email address will not be published. Required fields are marked *