ios disallow cellular for the whole app - iphone

Hi I have read some posts and see that I can use "reachability" sample from Apple to check if the current device is run on Wi-Fi or cellular and decide if a connection can be fired. However I have a lots of webviews and HTTP calls in my app on different classes, so I am afraid I have to call the check in every single method which will start a connection. I would like to ask if there is any way to check the status of the network and disallow all traffic on cellular? Thanks a lot.

You are looking for the ReachableViaWiFi network status, which is broadcast via NSNotification. You can setup that up in your code like this:
#property (nonatomic, assign) NetworkStatus currentNetStatus;
...
- (void) startListeningForWifi{
Reachability* hostReach = [Reachability reachabilityWithHostName:#"hostName"];
[hostReach startNotifier];
// reachability set up
// Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the
// method "reachabilityChanged" will be called.
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
}
- (void)reachabilityChanged: (NSNotification* )note{
Reachability *curReach = [note object];
self.currentNetStatus = [curReach currentReachabilityStatus];
}
Then it's easy to check the currentNetStatus before you make a network call. If that is not equal to ReachableViaWiFi then you are not on wifi.

Related

Pusher is disconnect in some time and result in no real time messaging in iOS Application

I am creating a chat application using Pusher library. connection is made correctly but when I chat for about near about 10 minutes connection fail and vacated in debug console of pushe app account and result in no real time messaging any more. I have implemented delegate method but delegate method does not call when connection vacated.
For creating pusher client:
client = [PTPusher pusherWithKey:#"XXXXXXXXXXXXXX" delegate:self encrypted:NO];
client.reconnectAutomatically = YES;
client.reconnectDelay = 30;
[client connect];
Delegate Methods:
(void)pusher:(PTPusher *)client1 connectionDidDisconnect:(PTPusherConnection *)connection
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
if ([reachability currentReachabilityStatus] == NotReachable) {
// there is no point in trying to reconnect at this point
client1.reconnectAutomatically = NO;
// start observing the reachability status to see when we come back online
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:reachability];
[reachability startNotifier];
}
}
(void)reachabilityChanged:(NSNotification *)note
{
Reachability *reachability = note.object;
if ([reachability currentReachabilityStatus] != NotReachable) {
// we seem to have some kind of network reachability, so try again
//= <# get the pusher instance #>
PTPusher *pusher = [PTPusher pusherWithKey:#"XXXXXXXXXXXXXXXX" delegate:self encrypted:NO];
[pusher connect];
// we can stop observing reachability changes now
// [[NSNotificationCenter defaultCenter] removeObserver:self];
// [reachability stopNotifier];
// re-enable auto-reconnect
pusher.reconnectAutomatically = YES;
}
}

is Reachability is update in realtime (reachabilityWithHostName)? [duplicate]

i read the https://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
my Question is if the www.apple.com is become not active Suddenly..this code will alert me?
or just if my connection fail's?
That's absolutely right.
It will fire off a kReachabilityChangedNotification notification that tells you the new reachability state.
You get the new reachability state something like this :
- (void)reachabilityChanged:(NSNotification *)notification {
Reachability *reachability = notification.object;
if (NotReachable == reachability.currentReachabilityStatus)
NSLog(#"No longer reachable");
}

how to implement Network Reachebility status in multiple files?

I am using readability class to check network availability and it works fine with this code. But in my app I am having approx 25 view that needs to check the network. I need to know that do I have to write pieces of code in every file ? or is there any way to write it once ?
In the code there is 3 methods that I have to implement to check the network status.
any good suggestions ?
Thanks...
You can use this sample application for your requirement. Provided by APPLE.
In this they have kept it under Application Delegate to be available to all the classes.
Hope it helps.
here goes, import the header in the app delegate file.
in applicationDidFinishLaunching add
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(reachabilityChanged:)
name: kReachabilityChangedNotification object: nil];
this means the appDelegate will be informed each time the reachability is changed. you need to implement the following:
add
-(void)reachabilityChanged: (NSNotification* )note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus netStatus = [curReach currentReachabilityStatus];
if(curReach == hostReach)
{
if (netStatus == NotReachable)
{
//no conn
}
else if (netStatus !=NotReachable)
{
//has conn
}
}
}
this is just an overview and you wont learn by just copying and pasting right, the reachability class can give you all you want to know, even they type of connection... so keep playing.
Make a static method in Reachability class like
-(BOOL)isInternetReachable{
//your code to check the internet connectivity
// return yes or no
}
the you can use as simple as
if([Reachability isInternetReachable])
{
///// do ur stuff
}
else{
////show some error message
}

iphone Reachability issues

My app requires an internet connection, so in the ApplicationDelegate, on applicationDidFinishLaunching I am running the following:
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
hostReach = [[Reachability reachabilityWithHostName: #"www.apple.com"] retain];
[hostReach startNotifer];
[self updateInterfaceWithReachability: hostReach];
But for some reason, this seems to be firing two times, since what gets logged is the following:
2010-02-04 14:25:48.004 myApp[201:207] Reachability Flag Status: -- ------- networkStatusForFlags
2010-02-04 14:25:48.240 myApp[201:207] STATUS: Access Not Available
2010-02-04 14:25:48.499 myApp[201:207] Reachability Flag Status: -- ------- networkStatusForFlags
2010-02-04 14:25:48.517 myApp[201:207] STATUS: Access Not Available
Which is good that it's working, but I have an alert message to notify the user that there is no connection, and it pops up twice...
Why is the reachability notifier firing two times?
No idea why it's popping up twice. Have you tried commenting out the call to startNotifier? It might do an initial check in the reachabilityWithHostName: method.
This might be a case where a workaround is the most appropriate fix, e.g.:
static bool userNotifiedOfReachability = NO;
...
- (void)updateInterfaceWithReachability:(Reachbility *)reachability {
if (!userNotifiedOfReachability) {
// Notify User
...
userNotifiedOfReachability = YES;
}
}
That would be appropriate for a "please try launching the app again later" message.

iPhone sdk pass messages between view controllers

I was wondering what is the best practice for an application flow in iPhone development.
How do you pass messages between ViewControllers?
Do you use singletons? pass it between views or do you have a main controller for the application that manage the flow?
Thanks.
I use NSNotificationCenter, which is fantastic for this kind of work. Think of it as an easy way to broadcast messages.
Each ViewController you want to receive the message informs the default NSNotificationCenter that it wants to listen for your message, and when you send it, the delegate in every attached listener is run. For example,
ViewController.m
NSNotificationCenter *note = [NSNotificationCenter defaultCenter];
[note addObserver:self selector:#selector(eventDidFire:) name:#"ILikeTurtlesEvent" object:nil];
/* ... */
- (void) eventDidFire:(NSNotification *)note {
id obj = [note object];
NSLog(#"First one got %#", obj);
}
ViewControllerB.m
NSNotificationCenter *note = [NSNotificationCenter defaultCenter];
[note addObserver:self selector:#selector(awesomeSauce:) name:#"ILikeTurtlesEvent" object:nil];
[note postNotificationName:#"ILikeTurtlesEvent" object:#"StackOverflow"];
/* ... */
- (void) awesomeSauce:(NSNotification *)note {
id obj = [note object];
NSLog(#"Second one got %#", obj);
}
Would produce (in either order depending on which ViewController registers first):
First one got StackOverflow
Second one got StackOverflow
The NSNotification class is a bit heavyweight, but fits the usage you describe. The way it works is that your various NSViewControllers register with an NSNotificationCenter to receive the events they're interested in
Cocoa Touch handles the routing, including providing a singleton-like "default notification center" for you. See Apple's notification guide with more info.