Blog | Phodal - A Growth Engineerhttp://www.phodal.com/blog/2016-05-07T15:32:01.275092+00:00BlogCordova插件 / 混合应用插件开发: Swift和Objective-C 混合使用2016-05-07T15:18:39+00:002016-05-07T15:32:01.275092+00:00Phodal Huanghttp://www.phodal.com/blog/author/root/http://www.phodal.com/blog/cordova-plugin-demo-swift-objective-c/在开始Cordova插件的时候遇到了一个有意思的问题,我在GitHub上寻找的一个iOS上的包用的是``Swift`` + ``Objective-C``,于是让我们来看看他们是怎么结合到一起的。
Cordova Swift hello,world
---
我们只需要简单地修改下``plugin.xml``,然后在代码中定义一个``Bridgin-Header.h``文件即可,如下代码所示:
```xml
<platform name="ios">
<info>
open xcode go into settings change `deployment target` to 7.0 or above and add `[Project Name]/Plugins/com.example.hello/Bridging-Header.h` to Objective-c Bridging Header under the Swift Compiler - Code Generation options
</info>
<config-file parent="/*" target="config.xml">
<feature name="Hello">
<param name="ios-package" value="HWPHello"/>
</feature>
</config-file>
<header-file src="src/ios/Bridging-Header.h"></header-file>
<source-file src="src/ios/Hello.swift"></source-file>
</platform>
```
在头文件中,我们引入将要给Swift用的头文件
```
#import <cordova cdv.h="">
```
在同目录下的``Hello.swift``便可直接使用:
```
import Foundation
@objc(HWPHello) class Hello : CDVPlugin {
func greet(command: CDVInvokedUrlCommand) {
var message = command.arguments[0] as String
var pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAsString: "Hello \(message)")
commandDelegate.sendPluginResult(pluginResult, callbackId:command.callbackId)
}
}
```
这时如[https://github.com/edewit/cordova-plugin-hello/tree/swift](https://github.com/edewit/cordova-plugin-hello/tree/swift)中所说,我们还需要一些特别的步骤,后面我们可以用代码来解决。
> go into settings change deployment target to 7.0 or above and add <project name="">/Plugins/com.example.hello/Bridging-Header.h to Objective-c Bridging Header under the Swift Compiler - Code Generation options
> Finally add the following LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Cordova自动混合使用Swift和Objective-C
---
这一点也适用于一些复杂的例子,如我在试验的[https://github.com/phodal/coap-cordova-plugin](https://github.com/phodal/coap-cordova-plugin)
我的xml是这样的:
```xml
<platform name="ios">
<hook src="hooks/add_swift_support.js" type="c"></hook>
<config-file parent="/widget" target="config.xml">
<feature name="Coap">
<param name="ios-package" value="HWCoapPlugin"/>
</feature>
</config-file>
<header-file src="src/ios/GCDAsyncUdpSocket.h"></header-file>
<source-file src="src/ios/GCDAsyncUdpSocket.m"></source-file>
<header-file src="src/ios/CoAP-Bridging-Header.h"></header-file>
<source-file src="src/ios/SCClient.swift"></source-file>
<source-file src="src/ios/SCMessage.swift"></source-file>
<source-file src="src/ios/SCServer.swift"></source-file>
<source-file src="src/ios/Coap.swift"></source-file>
</platform>
```
后面的``Coap.swift``需要调用``GCDAsyncUdpSocket``里面的函数,所以我们需要定义在``CoAP-Bridging-Header.h``中:
```objc
#import <cordova cdv.h="">
#import "GCDAsyncUdpSocket.h"
```
但是要是每次都这样就太麻烦了,于是网上出现了一个名为``add_swift_support.js``的脚本,用来自动化上面那些步骤。
参见:[https://github.com/cowbell/cordova-plugin-geofence](https://github.com/cowbell/cordova-plugin-geofence)
</cordova></project></cordova>