Writing Swift applications with Worklight 6.2 and below

This article is for customers running Worklight 6.2 and below. For newer versions of Worklight/MobileFirst, see 6.3, 7.0 or 7.1.

The Worklight native iOS SDK is written in Objective-C. However, Swift is backward compatible with Objective-C, and allows you to use Objective-C APIs in your Swift project.

To do so:

  1. Create a Swift project.
  2. Follow the steps to add the Worklight SDK to a native iOS application (see 6.2 documentation, or 6.1).
  3. Create a new file in your XCode project called MyBridgingHeader.h.
  4. Inside this file, #include all the Objective-C headers needed for your application. Those could be your own Objective-C classes, or classes provided by a third party library, such as Worklight.

    To see the list of Objective-C headers provided by Worklight, look under the WorklightAPI/include folder of your native application.

    For example, in a Worklight 6.2 project, your bridging header may look like this:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    #import "AbstractAcquisitionError.h"
    #import "AbstractGeoAreaTrigger.h"
    #import "AbstractGeoDwellTrigger.h"
    #import "AbstractPosition.h"
    #import "AbstractTrigger.h"
    #import "AbstractWifiAreaTrigger.h"
    #import "AbstractWifiDwellTrigger.h"
    #import "AbstractWifiFilterTrigger.h"
    #import "AcquisitionCallback.h"
    #import "AcquisitionFailureCallback.h"
    #import "BaseChallengeHandler.h"
    #import "BaseDeviceAuthChallengeHandler.h"
    #import "ChallengeHandler.h"
    #import "JSONStore.h"
    #import "JSONStoreAddOptions.h"
    #import "JSONStoreCollection.h"
    #import "JSONStoreOpenOptions.h"
    #import "JSONStoreQueryOptions.h"
    #import "JSONStoreQueryPart.h"
    #import "OCLogger.h"
    #import "WLAcquisitionFailureCallbacksConfiguration.h"
    #import "WLAcquisitionPolicy.h"
    #import "WLAnalytics.h"
    #import "WLArea.h"
    #import "WLCallbackFactory.h"
    #import "WLChallengeHandler.h"
    #import "WLCircle.h"
    #import "WLClient.h"
    #import "WLConfidenceLevel.h"
    #import "WLCookieExtractor.h"
    #import "WLCoordinate.h"
    #import "WLDelegate.h"
    #import "WLDevice.h"
    #import "WLDeviceAuthManager.h"
    #import "WLDeviceContext.h"
    #import "WLEventSourceListener.h"
    #import "WLEventTransmissionPolicy.h"
    #import "WLFailResponse.h"
    #import "WLGeoAcquisitionPolicy.h"
    #import "WLGeoCallback.h"
    #import "WLGeoDwellInsideTrigger.h"
    #import "WLGeoDwellOutsideTrigger.h"
    #import "WLGeoEnterTrigger.h"
    #import "WLGeoError.h"
    #import "WLGeoExitTrigger.h"
    #import "WLGeoFailureCallback.h"
    #import "WLGeoPosition.h"
    #import "WLGeoPositionChangeTrigger.h"
    #import "WLGeoTrigger.h"
    #import "WLGeoUtils.h"
    #import "WLLocationServicesConfiguration.h"
    #import "WLOnReadyToSubscribeListener.h"
    #import "WLPolygon.h"
    #import "WLProcedureInvocationData.h"
    #import "WLProcedureInvocationResult.h"
    #import "WLPush.h"
    #import "WLPushOptions.h"
    #import "WLResponse.h"
    #import "WLResponseListener.h"
    #import "WLSecurityUtils.h"
    #import "WLSimpleDataSharing.h"
    #import "WLTriggerCallback.h"
    #import "WLTriggersConfiguration.h"
    #import "WLTrusteer.h"
    #import "WLWifiAccessPoint.h"
    #import "WLWifiAccessPointFilter.h"
    #import "WLWifiAcquisitionCallback.h"
    #import "WLWifiAcquisitionPolicy.h"
    #import "WLWifiConnectTrigger.h"
    #import "WLWifiConnectedCallback.h"
    #import "WLWifiDisconnectTrigger.h"
    #import "WLWifiDwellInsideTrigger.h"
    #import "WLWifiDwellOutsideTrigger.h"
    #import "WLWifiEnterTrigger.h"
    #import "WLWifiError.h"
    #import "WLWifiExitTrigger.h"
    #import "WLWifiFailureCallback.h"
    #import "WLWifiLocation.h"
    #import "WLWifiTrigger.h"
    #import "WLWifiVisibleAccessPointsChangeTrigger.h"
    
    This file should also include any other Objective-C headers you need to use.
  5. Now, you need to tell XCode that MyBridgingHeader.h is the Swift bridging header to use.
    Under Build Settings > Swift Compiler > Objective-C Bridging Header, set the path to your bridging header, such as $(SRCROOT)/MyApp/MyBridgingHeader.h.

All the MobileFirst classes are now available from any of your Swift files.
XCode provides code autocompletion converted to the Swift style.

missing_alt
Last modified on May 01, 2016
Share this post: