From adb1fc737d999220d7b4c90d9c79b2116b43b180 Mon Sep 17 00:00:00 2001 From: Marian Hello Date: Fri, 17 Jun 2016 18:07:58 +0200 Subject: [PATCH] enable imports of swift code into objc #9 --- README.md | 13 +++++++++++++ src/add-swift-support.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/README.md b/README.md index c603029..66f52fa 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,19 @@ As an example you can have a look at this [plugin](https://github.com/akofman/co If the `cordova-plugin-add-swift-support` plugin is already installed to your project, then you can add your own Swift plugin as usual, its prefixed Bridging-Header will be automatically found and merged. +## Importing Swift into ObjectiveC code + +Because **ProductModuleName** of application using some Swift Cordova plugin is different than **ProductModuleName** of the +plugin itself, you need to modify your ***.m*** files imports to: + +``` +#import "Swift2Objc-Header.h" +``` + +\* instead of ~~#import "ProductModuleName-Swift.h"~~ + +This header will be created by this plugin and automatically registered with xcode project. + ## License Apache-2.0 © [Alexis Kofman](http://twitter.com/alexiskofman) diff --git a/src/add-swift-support.js b/src/add-swift-support.js index f09cf0e..2e756b0 100644 --- a/src/add-swift-support.js +++ b/src/add-swift-support.js @@ -33,6 +33,8 @@ module.exports = function(context) { var bridgingHeaderPath; var bridgingHeaderContent; + var swift2objHeaderPath; + var swift2objcHeaderContent; var projectName; var projectPath; var pluginsPath; @@ -73,6 +75,21 @@ module.exports = function(context) { xcodeProject.addHeaderFile('Bridging-Header.h'); } + swift2objHeaderPath = getSwift2ObjcHeaderPath(context, projectPath, iosPlatformVersion); + + try{ + fs.statSync(swift2objHeaderPath); + } catch(err) { + // If the bridging header doesn't exist, we create it with the minimum + // Cordova/CDV.h import. + swift2objcHeaderContent = [ '//', + '// Use this file to import your target\'s public headers that you would like to expose to Swift.', + '//', + '#import "' + projectName.replace(' ', '_') + '-Swift.h"' ]; + fs.writeFileSync(swift2objHeaderPath, swift2objcHeaderContent.join('\n'), { encoding: 'utf-8', flag: 'w' }); + xcodeProject.addHeaderFile('Swift2Objc-Header.h'); + } + var bridgingHeaderProperty = '"$(PROJECT_DIR)/$(PROJECT_NAME)' + bridgingHeaderPath.split(projectPath)[1] + '"'; if(xcodeProject.getBuildProperty('SWIFT_OBJC_BRIDGING_HEADER') !== bridgingHeaderProperty) { xcodeProject.updateBuildProperty('SWIFT_OBJC_BRIDGING_HEADER', bridgingHeaderProperty); @@ -146,3 +163,15 @@ function getBridgingHeaderPath(context, projectPath, iosPlatformVersion) { return bridgingHeaderPath; } + +function getSwift2ObjcHeaderPath(context, projectPath, iosPlatformVersion) { + var semver = context.requireCordovaModule('semver'); + var headerPath; + if(semver.lt(iosPlatformVersion, '4.0.0')) { + headerPath = path.posix.join(projectPath, 'Plugins', 'Swift2Objc-Header.h'); + } else { + headerPath = path.posix.join(projectPath, 'Swift2Objc-Header.h'); + } + + return headerPath; +}