Pretend to write a preface

Background: Large SDKS with a long history, objective-C of course. Of course, there is no problem with this, and it serves tens of millions of users every day, but unfortunately Apple released a StoreKit2, it is normal that Apple dad should release a lot of apis every year. Unfortunately, it only supports Swift writing, and I expect this API will only become the norm, so swiftization is urgent.

Result: Midnight writing

Tips: The example code of this article has been uploaded to Github and is attached in (below).

Compilation environment: Xcode13.0 Swift5.5 ARM64

Please let me know in the comments section.

What does our SDK look like?

The SDK, also known as the Framework, is basically the same, but the main differences are:

  • The Headers directory contains external files, which may include oral files, version documents, and other tools for quick interconnection with others.

  • The Modules directory contains the module.modulemap file, which includes one of the most important external headers such as yourproductName.h.

Why are these two files created?

  1. The Headers file is basically an internal decision about which files to release, set to Public, so that outsiders can see them. (PS: In fact, it is not completely exposed our code, and not so messy, convenient for others to connect)

  2. The Modules file is used to access the SDK and is related to the Target name. (PS: This is very relevant to our remix)

Two, let the SDK run first

| No mixed SDK looks like this:

.// File structure├─ PrefixHeader.pch Public Header File, Precompiled ├ ─ ─ SWSDK. H initial header file - Public ├ ─ ─ TestOC. H foreign practical header file - Public ├ ─ ─ TestOC. M ├ ─ ─ TestOC2. H within a functional file - Private └ ─ ─ TestOC2. M/ / modulesmap file
framework module SWSDK {
  umbrella header "SWSDK.h"
 
  export *
  module * { export*}}Copy the code

Here are our two OC classes, each with its own division of labor. Please remember their names.

/ / TestOC. M file
#import "TestOC.h"
#import "TestOC2.h"
@implementation TestOC
 
+ (void) run  {
	NSLog(@"%s",__func__);
    [TestOC2 run];
}
 
 
/ / TestOC2. M file
#import "TestOC2.h"
 
@implementation TestOC2
+ (void)run {
    NSLog(@"%s",__func__);
}
@end
Copy the code

At this point we are still not running, we need to modify swsdK.h to access open files

  • Modulemap is the entry point for external projects into the SDK, and the swsdK. h file as the entry point is currently empty.

  • Testc needs to be imported to SWSDK.h, and testC needs to be set to Public and available to the Public.

  • Testtesttestis set to Public, Build Phases → Headers, and testtesttestis dragged from Project to Public, as shown in the following figure.

#import <Foundation/Foundation.h>
// In this header, you should import all the public headers of your framework using statements like #import <SWSDK/PublicHeader.h>
// In this header, you should import all the common headers of the framework with statements like #import 
      
#import <SWSDK/TestOC.h>
/ /! Project version number for SWSDK.
FOUNDATION_EXPORT double SWSDKVersionNumber;
 
/ /! Project version string for SWSDK.
FOUNDATION_EXPORT const unsigned char SWSDKVersionString[]
Copy the code

We can continue in the appdelegate.m file:

#import "AppDelegate.h"
#import <SWSDK/SWSDK.h>
@interface AppDelegate(a)
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [TestOC run];
    return YES;
}
/ / output
2021- 11- 04 16:58:42.893981+0800 TestDemo[834:85483] +[TestOC run]
2021- 11- 04 16:58:42.894028+0800 TestDemo[834:85483] +[TestOC2 run]
Copy the code

| here, within the SDK OC part run successfully, butDon't reachOur purpose:Mix with Swift internally and finally use OC calls in external projects.

Skip here for the next post