NSStreamEventErrorOccurred triggered even if the network is pretty strong signal - iphone

I have implemented the NSStream delegate. I have implemented the same as Witap Application
In that, I have implemented handleEvent delegate
- (void) stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode{
switch(eventCode) {
case NSStreamEventOpenCompleted:
{
[tcpServer release];
tcpServer = nil;
break;
}
case NSStreamEventHasBytesAvailable:
{
//done my stuff here
}
case NSStreamEventErrorOccurred:
{
UIAlertView *wifiLostAlert = [[UIAlertView alloc] initWithTitle:#"Wifi
connection error" message:#"" delegate:nil cancelButtonTitle:#"Continue" otherButtonTitles:nil];
[wifiLostAlert show];
[wifiLostAlert release];
wifiLostAlert = nil;
}
}
In my client site, they reported an issue as
"Multiple time when we was either trying to send a message to the
learner or synching devices she got the following message: “Wifi
connection error.
Same happend at 2 different networks and it doesnot recover quickly
even if the network is pretty strong signal."
Unfortunately, I am unable to reproduce this issue in my site and it's working fine in another client site too!!.
Any clue's regarding the issue. Any help on this is appreciated.
Thank you.

I would recommend you supply the client with an updated version that displays more information about what error is actually occurring.
You can get more information about the error using:
NSError* error = [stream streamError];
You can find more information about NSError at NSError Class Reference.
Something like this might work in your case:
NSString* errorMessage = [NSString stringWithFormat:#"%# (Code = %d")",
[error localizedDescription],
[error code]];
Then change your UIAlertView to this:
UIAlertView *wifiLostAlert = [[UIAlertView alloc]
initWithTitle:#"Stream Error"
message:errorMessage
delegate:nil
cancelButtonTitle:#"Continue"
otherButtonTitles:nil];
This won't solve the problem but will give both you and your client more information about the root cause is.
For example, you may find the error is "Connection refused." which would point to a problem not with WiFi signal strength but in the server-side software.
Good luck!

Related

objective-c if NSURL fails display UIAlertView

I have this NSURL like so:
NSURL *url = [NSURL URLWithString:#"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
and it works fine when I am connected to the internet. But if I am not (or if the user has no signal) I would want an UIAlertView to appear. I tried the following code below (with and without the if statement) but my app crashes.
if(!url){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Test Message"
message:#"This is a test"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
What Am I doing wrong?
What I mean by crashing, I mean my app opens and does not get the data from the URL. I do not see any errors, I just get brought to a page with this highlighter:
Thread 6
com.app.root.default-priority
- 0 ucol_getVersion
Next to it is a window with this hightlighted
0x17bbd32: movl 204(%ecx), %edx Thread 6: EXC_BAD_ACCESS (code=2 address=0xcc)
What you are doing only checks if the object url is a NULL pointer or not. If you want to check if a user has internet connection, I would recommend trying the Reachability classes, which can be found easily online. This one is very useful for me.
it is recommended to check the Internet availability if you are developing the app with internet connection requirement by the Apple and they are also providing the .h and .m files namely Reachability.h and Reachability.m so please download that files and import to your projects and there is a parameter named "internetConnectionStatus" and by that values use the below alert
if (internetConnectionStatus == NotReachable)
{
UIAlertView *responseAlert = [[UIAlertView alloc] initWithTitle:#"Network Error"
message:#"This application requires network connectivity."
delegate:self
cancelButtonTitle:#"Exit"
otherButtonTitles:#"Continue", nil];
[responseAlert show];
}
else
{ //do other stuff here
}
you can find the reachability files Here and you can use any Reachability files from github
ThnQ

Issue in loading http://www.google.com url in iOS app

I was using the following code for checking the internet connection in my first iOS app (almost 3 years ago).
NSError *err = nil;
NSStringEncoding encoding;
NSString * connectionstring = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.google.com"] usedEncoding:&encoding error:&err];
if(connectionstring.length ==0)
{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Error Occured" message:#"No Internet Connection." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
// loading another screen
}
The problem is in India and US it is going into the else condition but in Belgium it is going into the if condition and throwing the error. So are there any restrictions for Google in Belgium?
If it is tagged incorrectly please guide me. In my current versions solved this issue by using Rechability classes to check this . Just I wanted to know about it . Thanks in advance.
As a first step, look at the encoding.
You can log the encoding right after the connectionstring is created by inserting
NSLog(#"encoding: %#", [NSString localizedNameOfStringEncoding:encoding]);
into your code.
For further debugging, you could try loading the url into an NSData object first, then inspect the data and finally create a NSString from the data.
Alternative solution: To check internet connection Use Reachability class provided by apple.
Download Apple Sample Here, add Reachability class to your project.
- (BOOL)isConnected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
Instead of using google.com use any local client server test url for internet checking it will also be very fast responsive comparatively of any other way of internet validation.

xcode webview error not working

This web view failed error showing the alert popup even when it is loading the website. I believe I need to delay this method in order for it to work.What will be the best method for doing this?
- (void)webView:(UIWebView *)webViewfail didFailLoadWithError:(NSError *)error
{
if([webViewfail isEqual:webview]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Connection Failed" message:#"Check your Internet connection before refreshing."
delegate:webview
cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
Here is how I am loading the website
- (void)viewDidLoad
{
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.blabla.com"]]];
}
The problem is most likely an error -999 which generally happens when something from the webpage does not load correctly or a user tries to navigate back while the page is still loading. After some research here's what I found and used to keep the NetworkAlert from poping up every time but still popping up when there is no network.
-(void)webView:(UIWebView *)webBlog didFailLoadWithError:(NSError *)error{
if ([error code] != -999) {
NSLog(#"Could not load the dumb webPage");
//show error alert, etc.
[self showNoNetworkAlert];
}else{
NSLog(#"Could not load the dumb web page...just might blame user!");
}
}
- (void) showNoNetworkAlert{
UIAlertView *baseAlert = [[UIAlertView alloc]
initWithTitle:#"No Network" message:#"A network connection is required. Please verify your network settings and try again."
delegate:nil cancelButtonTitle:nil
otherButtonTitles:#"Dismiss", nil];
[baseAlert show];
}
Hope this helps someone...

Core Data - updates to persistent store not working

This has been driving me crazy trying to figure this out...
I have a managed object where I need to set an attribute to flag the record is a favorite
The problem is that I don't see the value saved to the database (I have pulled the database from the simulator and inspected it???)
The following is the code snippet ... Note no error is thrown from the save
// Tell the user we have added to favorites
NSString *yes = #"Y";
[cardMessage setValue:yes forKey:#"favorite"];
NSError *error = nil;
BOOL savedSuccessfully = [managedObjectContext save:&error];
if (!savedSuccessfully)
{
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate.
You should not use this function in a shipping application, although it
may be useful during development.
*/
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Message added to Favorites" delegate:self
cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
You can use SQLite Manager (Browser Plug) to view the data from the Simulator db - fast and easy (Firefox plug).
https://code.google.com/p/sqlite-manager/
Use - (BOOL)hasChanges - in case something unexpected went wrong (no change for example).

Bluetooth warnings Xcode?

I'm trying to learn about using bluetooth connectivity through making a simple peer to peer bluetooth chat app. I'm hitting a bit of a problem as I'm new to exploring GameKit and Bluetooth and am hoping someone can help me. I think it's a formatting issue, but I could be very wrong. Basically when sending messages between both devices I'd like to use the name of the device that the message is coming from in the UIAlertView popup that displays the message. For example, 'Johnny's Iphone says:...'
Here's the bit of my code dealing with the AlertVIew:
- (void) receiveData:(NSData *)data
fromPeer:(NSString *)peer
inSession:(GKSession *)session
context:(void *)context {
//---convert the NSData to NSString---
NSString* str;
str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:(#"Chat Message: %#", peer)
message:str
delegate:self
cancelButtonTitle:#"REPLY"
otherButtonTitles:nil];
[alert show];
[alert release];
}
In the above I'm trying to use 'peer' to set the name but it's not working. I've tried initialising peer as a string and then passing it but this isn't working either. In both cases I get a string of 9 or 10 numbers with whatever message was sent displayed below. Where am I going wrong and what should I be doing?
Use [session displayNameForPeer:peer] which will return you the "display name"