iOS Question iOS version: Check Internet connection + Know connection type (WiFi/Cell)

Sergio Haurat

Active Member
Licensed User
Longtime User
SOLUTION HERE

I am attaching the .h, .xml and .a files in a Reachability.zip file, also the XCode source code in a Reachability source.zip

I asked ChatGPT how to get the same Android Code Snippet but in B4I and it says:

Steps to Use Reachability in B4I

1. **Download Reachability in Objective-C**:
- Visit the repository: [Reachability on GitHub](https://github.com/tonymillion/Reachability).
- Download the files [Reachability.h](https://github.com/tonymillion/Reachability/blob/master/Reachability.h) and [Reachability.m](https://github.com/tonymillion/Reachability/blob/master/Reachability.m).

2. **Create the Static Library (.a)**:
- To compile the `Reachability.h` and `Reachability.m` files into a static library, you need access to Xcode. If you don't have access to a Mac, you can ask someone who has a Mac to create this file.

Create the Static Library in Xcode

1. **Create a new Xcode project**:
- Open Xcode and create a new project of type "Cocoa Touch Static Library".

2. **Add Reachability to your project**:
- Drag and drop the `Reachability.h` and `Reachability.m` files into your Xcode project.

3. **Configure Architectures**:
- Make sure to configure your project to compile for all necessary architectures (armv7, arm64, x86_64, etc.).

4. **Compile the library**:
- Compile the project. This will generate a `.a` file in your project's products folder.

Create the XML File

The XML file describes the library interface that B4I uses to interact with the Objective-C code. Here is a detailed XML file that includes the main functions of Reachability.

1. **Create the Reachability.xml file**:
- Open a text editor and create a new file named `Reachability.xml`.

2. **Define the XML structure**:
- Here is an example of how the XML file for Reachability should look.
XML:
<library>
    <name>Reachability</name>
    <version>1.0</version>
    <dependsOn>iCore</dependsOn>
    <shortname>Reachability</shortname>
    <author>Tony Million</author>
    <copyright>© 2024 YourName</copyright>
    <wrapper>objc</wrapper>
    <functions>
        <function name="reachabilityWithHostname" returntype="NSObject*">
            <param name="hostname" type="NSString*"/>
            <description>Checks the reachability of a given hostname.</description>
        </function>
        <function name="reachabilityWithAddress" returntype="NSObject*">
            <param name="hostAddress" type="void*"/>
            <description>Checks the reachability of a given IP address.</description>
        </function>
        <function name="reachabilityForInternetConnection" returntype="NSObject*">
            <description>Checks the reachability of the internet connection.</description>
        </function>
        <function name="reachabilityForLocalWiFi" returntype="NSObject*">
            <description>Checks the reachability of the local WiFi connection.</description>
        </function>
        <function name="startNotifier" returntype="BOOL" method="reachability">
            <description>Starts listening for reachability notifications on the current run loop.</description>
        </function>
        <function name="stopNotifier" returntype="void" method="reachability">
            <description>Stops listening for reachability notifications on the current run loop.</description>
        </function>
        <function name="currentReachabilityStatus" returntype="int" method="reachability">
            <description>Returns the current reachability status.</description>
        </function>
        <function name="connectionRequired" returntype="BOOL" method="reachability">
            <description>Returns whether a connection is required.</description>
        </function>
    </functions>
</library>

3. **Save the file**:
- Save this file in the same folder where your `.a` file will be.

Configure in B4I

1. **Add files to your B4I project**:
- Place the `.a` file (e.g., `libReachability.a`) and the `Reachability.xml` file in a folder within your B4I project.

2. **Modify your B4I code to use the library**:
- Ensure that your B4I code imports and uses the Reachability library correctly.

Example of B4I Project

Here is an example of how to use Reachability in your B4I project:
B4X:
#AdditionalLib: Reachability

Sub Process_Globals
    Type tpeCheckInternet(blnIsOnline As Boolean, strType As String)
End Sub

Public Sub CheckInternet As tpeCheckInternet
    Dim ci As tpeCheckInternet
    ci.Initialize
    ci.strType = "NONE"
    ci.blnIsOnline = False

    Dim no As NativeObject
    no = no.Initialize("Reachability").RunMethod("reachabilityForInternetConnection", Null)
    Dim status As Int = no.RunMethod("currentReachabilityStatus", Null).As (Int)

    If status <> 0 Then
        ci.blnIsOnline = True
        If status = 2 Then ' ReachableViaWiFi
            ci.strType = "WIFI"
        ElseIf status = 1 Then ' ReachableViaWWAN
            ci.strType = "CELL"
        End If
    End If

    Return ci
End Sub

By following these steps, you should be able to integrate and use Reachability in your B4I application. If you need more details on any specific step, feel free to ask.



I can't find a way to resolve this error:
b4i_main.h:2:9: fatal error: 'Reachability.h' file not found
#import "Reachability.h"
^~~~~~~~~~~~~~~~
1 error generated.
 

Attachments

  • Reachability.zip
    19.5 KB · Views: 75
  • Reachability source code.zip
    56 KB · Views: 81
Last edited:
Top