Coder Social home page Coder Social logo

vevaywu / swiftcrossplatformframework Goto Github PK

View Code? Open in Web Editor NEW

This project forked from amine2233/swiftcrossplatformframework

0.0 0.0 0.0 38 KB

Tutorial to create cross platform framework for Swift compatible with Carthage and SwiftPM

License: MIT License

Swift 33.48% Objective-C 66.52%

swiftcrossplatformframework's Introduction

Swift Cross Platform Framework

swift cross platform framework

Table of contents


Introduction

The purpose of this tutorial is to create a Swift Cross-Platform framework from the same sources for macOS, iOS, watchOS and tvOS platforms and use them in a project via Carthage. It will be also ready for SwiftPM.

Use XcodeTool

You can follow this tutorial or just use XcodeTool and look at the section: Create a new component cross-platform

Create an empty project

  1. Open Xcode
  2. Select Create a new Xcode project

image

  1. Select Cross platform
  2. Click on Empty
  3. Click on Next

image

  1. Enter the project name (for this tutorial we use Template)
  2. Click on Next

image

You should have this screen image

Add targets

We must add a target for each platform

Target iOS

  1. On the section PROJECT click on (+)

image

  1. Select iOS
  2. Select Cocoa Touch Framework
  3. Click Next

image

  1. Enter the project name Template iOS (replace Template by your framework name). Don't forget iOS it's important!
  2. Click on Next

image

You should have this screen image

Target watchOS

  1. On the section PROJECT click on (+)

image

  1. Select watchOS
  2. Select Watch Framework
  3. Click Next

image

  1. Enter the project name Template watchOS (replace Template by your framework name). Don't forget watchOS it's important!
  2. Click on Next

image

You should have this screen image

Target tvOS

  1. On the section PROJECT click on (+)

image

  1. Select tvOS
  2. Select TV Framework
  3. Click Next

image

  1. Enter the project name Template tvOS (replace Template by your framework name). Don't forget tvOS it's important!
  2. Click on Next

image

You should have this screen image

Target macOS

  1. On the section PROJECT click on (+)

image

  1. Select macOS
  2. Select Cocoa Framework
  3. Click Next

image

  1. Enter the project name Template macOS (replace Template by your framework name). Don't forget macOS it's important!
  2. Click on Next

image

You should have this screen image

Add "Sources" folder

  1. Rigth click on Template and select Add Files to Template
  2. Click on New Folder
  3. Enter Sources and click on Create and Add

image

Rename "info.plist"

Info.plist for iOS

  1. Open folder Template iOS
  2. Rename info.plist to info-iOS.plist

image

Info.plist for watchOS

  1. Open folder Template watchOS
  2. Rename info.plist to info-watchOS.plist

image

Info.plist for tvOS

  1. Open folder Template tvOS
  2. Rename info.plist to info-tvOS.plist

image

Info.plist for macOS

  1. Open folder Template macOS
  2. Rename info.plist to info-macOS.plist

image

Move all plist on "Sources" folder

Select all on the folder

image

And move them to the "Sources" folder

image

On the project remove plists

image

Rigth click on the Sources folder and click on Add Files to "Template"...

image

Select all plist and click on Add. Be careful don't check a target.

image

Update "Build Settings"

Set the product name

  1. On section PROJECT select Template
  2. Selection section Build Settings
  3. In search enter product name
  4. For the property Product Name enter $(PROJECT_NAME)

image

Apply Product Name for all targets

  1. Select all targets
  2. Click on Product Name
  3. Click on key Delete. All targets will have the product name Template.

image

Update packaging

For iOS target

  1. Select Template iOS target
  2. Set property info.plist File to Sources/info-iOS.plist
  3. Set property Product Bundle Identifier to com.<your company>.$(PROJECT_NAME)

image

For watchOS target

  1. Select Template watchOS target
  2. Set property info.plist File to Sources/info-watchOS.plist
  3. Set property Product Bundle Identifier to com.<your company>.$(PROJECT_NAME)

image

For tvOS target

  1. Select Template tvOS target
  2. Set property info.plist File to Sources/info-tvOS.plist
  3. Set property Product Bundle Identifier to com.<your company>.$(PROJECT_NAME)

image

For macOS target

  1. Select Template macOS target
  2. Set property info.plist File to Sources/info-macOS.plist
  3. Set property Product Bundle Identifier to com.<your company>.$(PROJECT_NAME)

image

One header file for all targets

Change header file

  1. Select Template iOS.h
  2. Change #import <UIKit/UIKit.h> by #import <Foundation/Foundation.h>
  3. Remove iOS

image

Move header file to "Sources"

  1. Open Finder and Rename Template iOS.h to Template.h
  2. Move Template.h to folder Sources
  3. Remove folders Template iOS, Template macOS, Template tvOS and Template watchOS

image

image

image

Update project

  1. Remove folders Template iOS, Template macOS, Template tvOS and Template watchOS
  2. Add Template.h in Sources folder
  3. Select Template.h
  4. In Target Membership check Template iOS, Template watchOS, Template tvOS and Template macOS
  5. For each target select Public

image

image

image

Add class for all platforms

  1. Click on Sources
  2. Click on New File...

image

  1. Select macOS
  2. Select Swift File
  3. Click Next button

image

  1. Enter the name of your class
  2. Select all targets
  3. Click Create button

image

Your first class cross platform: image

Configure targets for Carthage

  1. Click on Template iOS option Manage Schemes...
  2. For all targets check Shared

image

image

Configure project for SwiftPM

  1. Select the project Template
  2. Rigth click and select New File...

image

  1. Select macOS
  2. Select Swift File
  3. Click Next button

image

  1. Enter Package
  2. Uncheck all targets
  3. Click on Create button
  4. On the Package.swift enter:
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription

let package = Package(
    name: "Template",
    products: [
        // Products define the executables and libraries produced by a package, and make them visible to other packages.
        .library(
            name: "Template",
            targets: ["Template"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "Template",
            dependencies: [])
    ]
)

image

image

swiftcrossplatformframework's People

Contributors

tofplay avatar roudique avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.