Luke Smith
Luke Smith

Reputation: 692

Global functions in an iOS application

I need to be able to check for internet connectivity / available updates within multiple views. What would be the best way to implement methods / functions (which carry out this function) that are accessible in numerous views?

Whether I can have a separate class that include these methods, that I use as and when needed? If that's at all possible.

Upvotes: 2

Views: 261

Answers (6)

lukaswelte
lukaswelte

Reputation: 3001

There are no global methods provided by Apples Libraries and Frameworks you could directly use.

Internet connection check

To check the internet connection you can use the Reachability class from the Reachability example from Apple like mentioned here

Reachability *reach = [Reachability reachabilityWithHostname:@"google.com"];
 if ([reach isReachable]) {
     //Internet Available...
}
else
{
      //Internet Not Available
}

Or use one of the other Open-Source Frameworks:

Little github search:

Available App Updates Check

To check for available App updates you could use one of the available Open-Source Frameworks. Like:

Upvotes: 0

Sam Baumgarten
Sam Baumgarten

Reputation: 2259

Here is an ARC compatible version of Reachability: https://github.com/tonymillion/Reachability. In each view you want to check for internet in, you can add this to the viewDidLoad method: // allocate a reachability object

Reachability* reach = [Reachability reachabilityWithHostname:@"http://thewebsiteyouareusing.com"];

// set the blocks 
reach.reachableBlock = ^(Reachability*reach)
{
    // It works
};

reach.unreachableBlock = ^(Reachability*reach)
{
    // Uh oh, it doesn't work
};

[reach startNotifier];

Upvotes: 0

Abizern
Abizern

Reputation: 150785

I don't know what you are doing for your internet connectivity, but if you use something like the AFNetworking library, then you set up singleton's for endpoints and use those for network operations. It also includes it's own reachability functionality that you can use to see whether a network connection is possible as well as block based methods for handing changes in the connectivity state (just as important as checking whether a network connection exists)

If you'd rather roll your own solutions. Have a look at NPReachability. It's an extension to the Apple's Reachability class that provides KVO, blocks, and ARC support.

Upvotes: 1

morningstar
morningstar

Reputation: 9162

There are many ways possible. The singleton is a good idea, if your functions need to store any data. If not, you can just make a class that has only static methods (+ prefix). Then import that class from any file that needs it.

Upvotes: 0

Prateek Prem
Prateek Prem

Reputation: 1546

You will have to download Reachability.h amd .m file add add it in your project. and use this line :

Reachability *reach = [Reachability reachabilityWithHostname:@"google.com"];
 if ([reach isReachable]) {
     //Internet Available...
}
else
{
      //Internet Not Available
}

Upvotes: 1

kailoon
kailoon

Reputation: 2069

One way is to add a variable to your app delegate. You can then access it anywhere using:

MyAppDelegate *app = (MyAppDelegate *)[UIApplication shared].delegate;
if (app.myInternetVariable) {
    // etc...
}

Another way is to use a singleton to store all your application data.

Upvotes: -1

Related Questions