iOS and Android - Bridge One

May 28, 2019
ios android comparison

Bridge

I am looking for a way to construct a bridge for Android developer to walk into iOS and an iOS developer to walk into Android by not using Cross-platform frameworks.

In these adventures, I am going to study the similarities that will make the transition quick and some differences that one needs to be careful. I am not against Cross-platform development, I love it, but in some instances, it requires a developer to jump into the native platform to achieve certain things, it might help a Cross-platform developer as well in such cases.

View Controllers

Binding of view to the view controller is explicit(we specify the layout the view controller has to be bound to) in case of Android while it is implicit in case of iOS.

Android View Controller - Activity [QuizActivity.kt]

class QuizActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_quiz)
    }
    
}

iOS View Controller - UIViewController [QuizController.swift]

class QuizController: UIViewController {

}

Layout Design

Android’s Constraint Layout and iOS Auto Layout are quite similar. Once you have an understanding of one, you can quickly work your way in the other. While the IDE associated with Android gives the flexibility to use visual design tool to place components in the screen as well as edit the raw XML file, the iOS on the other hand, as far as I have tinkered, doesn’t offer the freedom to edit the layout file manually.

Android Layout - Constraint Layout [activity_quiz.xml]

iOS Layout - Auto Layout [Quiz.storyboard]

Dependency Management

The dependency management for Android is inbuilt with the default build system(Gradle), so it is straightforward to add the necessary dependencies to the project without any manual bootstrap process. While in case of iOS developers have to go through a bootstrap process to set up a dependency manager to add the required dependencies to the project. There are a few options available for iOS developers to choose from like CocoaPods, Swift Package Manager and Carthage. In this example, I have used CocoaPods.

Android Dependency Management - Gradle [build.gradle]

dependencies {
    //dependencies for GeoQuiz
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.21"
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    
    //dependencies for unit testing
    testImplementation 'junit:junit:4.12'
    
    //dependencies for ui testing
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

iOS Dependency Management - Podfile [Podfile]

target 'GeoQuiz' do
  use_frameworks!

  # dependencies for GeoQuiz
  pod 'Toast-Swift', '~> 5.0.0'
  
  
  #dependencies for unit testing
  target 'GeoQuizTests' do
    inherit! :search_paths
  end

  #dependencies for ui testing
  target 'GeoQuizUITests' do
    inherit! :search_paths
  end

end


#1. Close Xcode and execute the command given below to create the Podfile.
pod init

#2. Open Podfile and add the required dependencies to it.

#3. Execute the command given below to install the dependencies added to the Podfile.
pod install

#4. Open the project workspace created with the following extension *.xcworkspace

View Listeners

For Android, I attach the click listeners on the call of the onCreate() method, while in iOS I have to make the attachments from the layout editor in Xcode(Quiz.storyboard, Hold Control and Drag to the appropriate ViewController to bind a view to a method tagged inside the ViewController). In Android case, something similar to iOS can be achieved using DataBinding.

Android Bind View To Action - Activity [QuizActivity.kt]

class QuizActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        //..
        true_button.setOnClickListener {
            Toast.makeText(this, R.string.correct_toast, Toast.LENGTH_SHORT).show()
        }

        false_button.setOnClickListener {
            Toast.makeText(this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show()
        }
    }
}

iOS Bind View To Action - UIViewController [QuizController.swift]

class QuizController: UIViewController {

    @IBAction func trueButtonListener(sender: UIButton){
        self.view.makeToast(getString(key: "correct"))
    }
    
    @IBAction func falseButtonListener(sender: UIButton){
        self.view.makeToast(getString(key: "incorrect"))
    }
    
    private func getString(key: String) -> String {
        return NSLocalizedString(key,comment: "Read localized value")
    }
    
}

Managing Strings

Almost all developers are not a fan of encoding text within the code unless they have no other option. In this aspect, both iOS and Android offer a way to separate strings into individual files which will come in handy when the App needs to be localized for a specific target audience. Android text used in the App can be separated into individual XML files. While in iOS at least as far as I have used there are two levels of separation, where one set is statically bound to the UI elements as seen in Quiz.strings with some bizarre key-value pair. The other relates to text that needs to be shown dynamically in the UI based on user action as seen in Localizable.strings.

Android Strings - [strings.xml]

<resources>
    <string name="question_text">Canberra is the capital of Australia.</string>
    <string name="true_button">True</string>
    <string name="false_button">False</string>
    <string name="correct_toast">Correct!</string>
    <string name="incorrect_toast">Incorrect!</string>
</resources>

iOS Strings - [Quiz.strings,Localizable.strings]

"PfF-kH-laH.text" = "Canberra is the capital of Australia.";
"3GG-nO-zSm.normalTitle" = "TRUE";
"pDy-Th-fph.normalTitle" = "FALSE";


"correct" = "Correct!";
"incorrect" = "Incorrect!";

I also to bring to readers notice that I don’t claim to know everything before I ventured in this adventure, In case If I am not correct about certain things kindly bring it to my notice and I’ll correct it, and it will benefit other readers as well. As we learn, we make mistakes; we fix them, and we grow.

I am modifying the code samples from this book for illustrative purposes. I would also recommend this book for those of you who are new to the Android world.

comments powered by Disqus