Showing posts with label iOS. Show all posts
Showing posts with label iOS. Show all posts

Importing Swift file to Objective-C project

I think it is very complex when I read Apple's reference. Hence, I decided to write a simple article which illustrates you how to import Swift file into Objective-C project step by step.

First, you should create an Objective-C project.

After project be created. We have some parameters should be set in Build Settings.

  • Find “Defines Module”, and set to “YES”. This setting let Xcode use same framework to import Swift to Objective-C project.



  • Find “Always Embed Swift Standard Libraries”, and set to “YES”.



  • The “Product Module Name” names by your project name. In there, we do not need to revise anything.


  • New or add exist Swift file into the project. Then Xcode pop-ups a dialog, follows image. We just click “Create Bridging Header” to automatically crate bridging header. After that, we can see the bridging header file which be names your product name followed by adding "-Bridging-Header.h".


    Finally, we need to import “MixDemo-Swift.h” to any Object-C .m. That header is created by Xcode, but you cannot see in the project folder. The file names your Product Module Name, which we mentioned above, followed by add “-Swift.h”. You can also search "Objective-C Generated Interface Header Name" under Build Settings.
    #import "MixDemo-Swift.h"
    

    Then, we can use swift object in the Objective-C project directly.
        BarcodeScannerViewController *barcodeViewController = [[BarcodeScannerViewController alloc] init];
        barcodeViewController.delegate = self;
        [self presentViewController:barcodeViewController animated:YES completion:nil];
    

    Reference: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

    App Transport Security

    Platforms: iOS 9.0 and later, OS X v10.11 and later

    Summary:
    l   Secure connections between App and back end
    l   Https exclusively
    l   default strong Internet security in iOS and OS X apps and in app extensions

    Protocol: TLS 1.2 and later

    Certificates:
    l   SHA-2 256 bits
    l   ECC 256 bits
    l   RSA 2048 bits

    Forward secrecy (FS):
    l   TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
    l   TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
    l   TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
    l   TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
    l   TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
    l   TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
    l   TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
    l   TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
    l   TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
    l   TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
    l   TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA




    When the backend server does not follow ATS rules (Eg Using lower level of Transport Layer Securiy, http protocol or self-signed etc. ), it should make a whitelist. It only needs to revise the file of "Info.plist",
    Here are some sample settings:


    l   Allowing Lowered Security
    It can specify protocol, which is lower than TSL v1.2, or which is not supported FS
    <key>NSAppTransportSecurity</key>
    <dict>
            <key>NSExceptionDomains</key>
            <dict>
                    <key>your_server.example.com</key>
                    <dict>
                            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.0</string>
                            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
                            <false/>
                    </dict>
            </dict>
    </dict>


    l   Allowing Http, Self-signed (Insecure connection)
    <key>NSAppTransportSecurity</key>
    <dict>
            <key>NSExceptionDomains</key>
            <dict>
                    <key>your_server.example.com</key>
                    <dict>
                            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
                            <true/>
                    </dict>
            </dict>
    </dict>



    l   Turn off ATS
    <key>NSAppTransportSecurity</key>
    <dict>
     <key>NSAllowsArbitraryLoads</key>
     <true/>
    </dict>


    P.S. If the domain which cannot be controlled by the developer, it needs a parameter of  "NSThirdPartyException". I do not know what is different. But it is work for me.

    Reference: