I have been searching through the forum regarding how to check whether there is internet or not in my ipad app. I just created a simple webview project with other view controllers and I need to display a UIAlert message when the internet is not available. In my case it is displaying the message when I run the app. When I run the app with internet and then deactivate the internet, it does not show the UIAlert message, that is if I switch between the views, it does not any more show the no internet connection.
I have followed this way of implementation in my project: (sorry my mistake this is the link I followed) http://mozymac.com/forums/f54/how-check-if-there-internet-connection-iphone-os-devices-595/ [This is the new edited question]
Apart from that I went through some of the previous questions in Stackoverflow forum like for ex: How to check for an active Internet connection on iOS or OSX?
But everybody has their own version. If any one has a much more updated method for ios5, xcode 4.2.1 of how to accomplish this then would be helpful for me.
Thanks
Is there a reason why you want to check for internet connection before actually trying to load a request in the UIWebView?
Best practice is to just start loading, and use your UIWebViewDelegate/NURLConnectionDelegate to inspect the NSError to see what is wrong. In case of network failure you will receive an error with a domain equal to NSURLErrorDomain. The error code will indicate what the problem is, see the NSError codes enum.
And only after the first error start your reachability to see when the internet connection becomes available again. Or easier, just let the user retry.
Using the Reachability code will actually cause some overhead. It takes time to check if the internet is available, which you could just have used to set up the actual connection as well.
Example
Since you are using a UIWebView you should implement the following delegate method to be notified of errors.
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
if (![[error domain] isEqualToString:NSURLErrorDomain]) {
// not a nsurl error, take other appropriate action
return;
}
NSInteger code = [error code];
// show appropriate error to user, based on code
}
In this delegate method you should do whatever is needed to achieve what you want. You could retry the request yourself, show a message to the user or start listening for reachability changes using the code from the Reachability example provided by Apple.
Apple has one, it's called Reachability. Here's the link to it.
http://developer.apple.com/library/ios/ipad/#samplecode/Reachability/Introduction/Intro.html
Best way to check internet connection is Reachibility application
link
Or else
+ (BOOL)isNetworkAvailable
{
CFNetDiagnosticRef diag;
diag = CFNetDiagnosticCreateWithURL (NULL, (CFURLRef)[NSURL URLWithString:#"www.apple.com"]);
CFNetDiagnosticStatus status;
status = CFNetDiagnosticCopyNetworkStatusPassively (diag, NULL);
CFRelease (diag);
if ( status == kCFNetDiagnosticConnectionUp )
{
//NSLog (#"Connection is up");
return YES;
} else {
NSLog (#"Connection is down");
return NO;
}
}
Related
I'm building a small Instagram client for personal use on Mac OSX. I'm currently using gtm-oauth2 to obtain an oauth2 token from Instagram. I'm following the guide provided with the source to obtain this token. I've got it 90% working. The webView loads with the authentication details, and I can enter my account and the permissions screen comes up asking if I would like to grant my application access. The issue I'm running into is that after authenticating, regardless of whether I "Allow" or "Cancel", the application crashes with no stack trace or additional information. The only info I get regarding the exception is "Thread 1: EXC_BAD_ACCESS (code = 1, address=0x4c1)" and it appears that the thread is doing something to do with the WebCore::ResourceLoader, but it's a bunch of ASM so I've got no idea where this call is actually occurring. Perhaps I'm not calling the windowController properly? I've got my code included below.
- (void)signIntoInstagram {
NSURL *tokenURL =[NSURL URLWithString:kTOKENIURl];
// Set up the OAuth request
GTMOAuth2Authentication *auth = [GTMOAuth2Authentication
authenticationWithServiceProvider:#"Instagram"
tokenURL:tokenURL
redirectURI:kREDIRECTURI
clientID:KCLIENTID
clientSecret:KCLIENTSERCRET
];
// Specify the appropriate scope string, if any, according to the service's API documentation
auth.scope = #"basic";
NSURL *authURL = [NSURL URLWithString:KAUTHURL];
// Display the authentication view
GTMOAuth2WindowController *windowController;
windowController = [GTMOAuth2WindowController controllerWithAuthentication:auth
authorizationURL:authURL
keychainItemName:kKeychainItemName
resourceBundle:nil];
// optional: display some html briefly before the sign-in page loads
NSString *html = #"<html><body><div align=center>Loading sign-in page...</div></body></html>";
[windowController setInitialHTMLString:html];
[windowController signInSheetModalForWindow:_window
delegate:self
finishedSelector:#selector(windowController:finishedWithAuth:error:)];}
If I insert a breakpoint within the windowController:finishedWithAuth:error: method, the application is reaching it. However, it still crashes after I run through, which to me seems like some sort of asynchronous operation causing the error. Hopefully I'm just missing something simple here; I can't imagine there is a major flaw in Google's OAuth project.
I found this on a Google group after having the same problem https://groups.google.com/forum/#!msg/gtm-oauth/N6jlOpL9k5g/n4TdrTJyxzcJ . There is also an issue logged for it https://code.google.com/p/gtm-oauth/issues/detail?id=11
Basically I commented out line 331 of GTMOAuth2WindowController.m and it worked. You can also add your vote to the issue and maybe Google will fix it.
I am adding Dropbox support to my iOS application. Using the official Dropbox API and the tutorials online here I have gotten to the point where Dropbox needs to be authenticated. The code below is what is given to Authenticate when a button is pressed:
//MainViewController.m
....
#implementation CryptoMainViewController
.....
#pragma mark - Dropbox
- (void)didPressLink {
if (![[DBSession sharedSession] isLinked]) {
[[DBSession sharedSession] link];
}
}
But no matter how I change the code, where I put it or what button I link it to, nothing will happen. Using breakpoints I've found that the method does in-fact get triggered. I've even put it in an IBAction, but this gives the same result. What am I doing wrong? How can I get my app to authenticate the end-user?
And, once authenticated, How can I save an NSString to the user's Dropbox?
If this is just totally wrong, then where can I go to find resources on how to do this properly?
The whole tutorial, all of the documentation, api, etc. is available here.
I had the same problem; the reason was that I hadn't set the shared Dropbox session, e.g.
DBSession* dbSession = [[[DBSession alloc] initWithAppKey: #"your_app_key"
appSecret: #"your_app_secret"
root: kDBRootAppFolder] autorelease];
[DBSession setSharedSession: dbSession];
Once that was called the link worked fine.
this answer may be late but im guessing that you already linked your app before and want to do so again. The only way you can have the process of linking taking place again is if you run the following code:
[[DBSession sharedSession] unlinkAll];
You can place it in your viewDidLoad. When you then call didPressLink: the app should open up dropbox app(if available), safari or an in app window asking for your permission to access your dropbox. If this does not happen then the problem is somewhere else. Hope this helps
Does your view implement the <DBLoginControllerDelegate> ?
If so, link Dropbox like so:
DBLoginController* controller = [[DBLoginController new] autorelease];
controller.delegate = self;
[controller presentFromController:self];
I have a mac app that writes camera data to a file then uploads that file on an [NSTimer] to a server. However, after the camera runs for a while it closes itself and I see this in the log: * QTCaptureDeviceInput warning: The device "FaceTime HD Camera (Built-in)" was closed while still being used in a capture session. Make sure that the session is not running before closing any devices it is using.
No other apps are open that can use the camera in the test case and the object for the device is not being released by me. I have gone so far as to call retain on it to prevent this, to no avail.
I looked at Apple's docs and nowhere that I found did it talk about the camera being shut off by the system, so what could be causing this. In the meantime I have some code that periodically checks if it is open and if it isn't calls open on it.
One thing I forgot I am using this notification and it is being fired: QTCaptureDeviceWasDisconnectedNotification
Well, it took a while but I found the issue. When you you are pulling image data from a running QTCaptureDevice you need to work with individual frames via the following delegate method. If you fail to properly release the buffers on each run the camera will eventually close by itself. Please note the code below is CORRECt; I attached it to help anyone else reading this question. Hope this saves someone the time it costed me...
- (void) captureOutput:(QTCaptureOutput *)captureOutput didOutputVideoFrame:(CVImageBufferRef)videoFrame withSampleBuffer:(QTSampleBuffer *)sampleBuffer fromConnection:(QTCaptureConnection *)connection {
CVBufferRef bufferToBeReleased;
CVBufferRetain(videoFrame);
#synchronized (self) {
imageBufferToRelease = imageIWanted;
imageIWanted = videoFrame;
}
CVBufferRelease(bufferToBeReleased);
}
I have read
this question and found its answer to be useful to check an internet connection for the iPhone. I implemented the check in my viewDidLoad message.
However my App does load Data from an XML file from a server (within viewWillAppear). In this case the check has to be done before the app tries to load the data from the internet source. My problem here is that the network check requires some seconds but the app does proceed with the code.
This results in delaying the check. So there is a valid internet connection, but the bool variable that stores this information is set too late.
Code in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
The selected message is implemented in the same way like in the example in the link above. Additionally it sets some bool-properties so I can access the connection state within the app.
Code in viewWillAppear
if(internetActive == TRUE)
{
NSLog(#"connected");
//Code to load XML stuff
}
else
{
NSLog(#"not connected");
}
The console shows me "not connected". But a few seconds later the debug logs within "checkNetworkStatus" show me that the internet connection is available and the bool "internetActive" would be set.
I assume that the network check delivers his report about the connection state too late for my purpose. Is there a way to delay the app until the connection is established? Or would this lead to crashing the app cause the view isn't displayed within a few seconds?
Are there any other possibilities to ensure the connection?
My second question is how to ensure a specific internet address is available? I the link above there is mentioned
hostReachable = [[Reachability reachabilityWithHostName: #"www.apple.com"] retain];
[hostReachable startNotifier];
to detect the connection to a host, but I want to go in detail and try to detect if the host deserves a specific address or not. But entering the full address path (e.g. "localaddress.mylan.de/lan/xml/") instead of the host leads to a not reachable connection state for the host. Entering the mentioned address in the browser address bar shows me the correct result. I guess I have to check it otherwise but how?
Thanks for reading :)
After getting more experiences with IOS development my first question is solved with three steps.
First step is to start all necessary notifiers once. This can be done by starting them in viewDidLoad of the first loaded ViewController.
Check current connection state by evaluating property named currentReachabilityStatus of reachability-objects. This state can be "not connected" even if a connection is available. This happens if the check is done within the ViewController who is starting the notifier. Reason for this is the time it takes while the notifier recognizes the connection. If currentReachabilityStatus property is evaluated as connected then data loading processes can be started.
Evaluating the connection state within the method whose selector is added during adding the notifier. This method will start every time the connection state changes. First call will be directly after starting the notifier. If connection state is evaluated as connected then data loading processes can be started.
The second question stays unresolved up to now. May be there is no way other than sending a request to the target and waiting for a response or a timeout.
Did you try dispatch_async & dispatch_sync(dispatch_get_main_queue( reload data here ) ?
Sorry to bug twice so quickly, but since people were so kind in their informative responces, I figured it couldnt hurt to ask another question.
The same program i tried to make it rather swanky and have a main screen which allows you to click on a button which leads to a limited options screen. This lets you switch the music on or off. Or at least it should do.
The music running code is in the main file (game.m), under the following:
//Music
[Settings setMusicEnabled:YES];
music = [SPSound soundWithContentsOfFile:#"music.caf"];
channel = [[music createChannel] retain];
channel.loop = YES;
channel.volume = 0.25;
if([Settings musicEnabled]){
[channel play];
}
I apologize for the strange format, but it is Sparrow framework. basically, the Settings file contains the class methods I am trying to use. If the methods cause YES, the music is on. If it is No, then the music is off.
settings.m
static BOOL isMusicEnabled;
#implementation Settings
+ (BOOL)musicEnabled
{
return isMusicEnabled;
}
+ (void)setMusicEnabled:(BOOL)value
{
isMusicEnabled = value;
NSLog(#"SME? %i", isMusicEnabled);
}
#end
Now, the options file is working and i tested that section. The program is reading that isMusicEnabled is getting a new value, thus musicEnabled is being altered as well, so there should be a change and the music should be switched off.
However, nothing happens. I have tried to use debugger, but I am not very good at it and I dont understand a lot of the information i am given. I do understand that the problem is sending the message from Settings file to the main/Game file.
I would appriciate anyone's help who could enlighten me as to how this could be solved.
I'm not familiar with Sparrow Framework, but let me make a guess anyway.
[channel play]; starts playing the music in background until the channel is asked to stop playing.
Changing the isMusicEnabled does not trigger any code to stop the currently playing music. When you change the value in Settings, you should inform the channel to stop (most probably by somehow accessing the channel and calling [channel stop].
There's another problem - isMusicEnabled is just a variable in memory, your program will not remember its state between restarts. And Settings are usually supposed to be remembered.
To summarize I see two problems: persisting settings between restarts first and informing about change of settings second. To remember settings I suggest you look into NSUserDefaults class. To inform the channel to stop playing you have couple of options - depending on you skills. Easiest is to simply access the channel variable from within the setMusicEnabled and call stop. Another option would be to use notifications, but for a beginner programmer that is more complicated (look for NSNotificationCenter if interested).