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 authored and mircoservices committed Mar 1, 2017
1 parent 64cdace commit 9ef33c6
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 @@ -23,6 +23,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.

## Contributing

The src folder contains ECMAScript 2015 source files.
Expand Down
29 changes: 29 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export default (context) => {

let bridgingHeaderPath;
let bridgingHeaderContent;
let swift2objHeaderPath;
let swift2objcHeaderContent;
let projectName;
let projectPath;
let pluginsPath;
Expand Down Expand Up @@ -81,6 +83,21 @@ export default (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
// ProductModuleName-Swift.h import.
swift2objcHeaderContent = [ '//',
'// Use this file to import your projects\'s generated swift-header to expose Swift to Objective-C.',
'//',
'#import "' + projectName.replace(' ', '_') + '-Swift.h"' ];
fs.writeFileSync(swift2objHeaderPath, swift2objcHeaderContent.join('\n'), { encoding: 'utf-8', flag: 'w' });
xcodeProject.addHeaderFile('Swift2Objc-Header.h');
}

buildConfigs = xcodeProject.pbxXCBuildConfigurationSection();

const bridgingHeaderProperty = '"$(PROJECT_DIR)/$(PROJECT_NAME)' + bridgingHeaderPath.split(projectPath)[1] + '"';
Expand Down Expand Up @@ -186,3 +203,15 @@ const getBridgingHeaderPath = (context, projectPath, iosPlatformVersion) => {

return bridgingHeaderPath;
};

const getSwift2ObjcHeaderPath = (context, projectPath, iosPlatformVersion) => {
const semver = context.requireCordovaModule('semver');
let 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 9ef33c6

Please sign in to comment.