Skip to content

Commit

Permalink
enable imports of swift code into objc akofman#9
Browse files Browse the repository at this point in the history
  • Loading branch information
mauron85 committed Jun 17, 2016
1 parent 10cbe2e commit adb1fc7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
29 changes: 29 additions & 0 deletions src/add-swift-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ module.exports = function(context) {

var bridgingHeaderPath;
var bridgingHeaderContent;
var swift2objHeaderPath;
var swift2objcHeaderContent;
var projectName;
var projectPath;
var pluginsPath;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

0 comments on commit adb1fc7

Please sign in to comment.