I know I can check if a URL has been used to open my application, like so:
- (BOOL)application:(UIApplication*)application
openURL:(NSURL*)url
sourceApplication:(NSString*)sourceApplication
annotation:(id)annotation'
... but how do I check if the user has only returned to the app (and update my interface accordingly), without a URL being used? The use case is when logging in to Facebook or Twitter via Safari, and then user just goes back to the app instead.
Use applicationDidBecomeActive or applicationWillEnterForeground
Related
On Apple documentation of Universal links it is stated that:
If you receive an invalid URL in an activity object, it’s important to fail gracefully. To handle an unsupported URL, you can call openURL: on the shared application object to open the link in Safari.
BUT, if we look at UIApplicationDelegate's - application:continueUserActivity:restorationHandler:
documentation we see, that this method can return NO if the app can't handle received link:
Return Value:
YES to indicate that your app handled the activity or NO to let iOS know that your app did not handle the activity.
I've tried returning NO and it works very well - it opens the link in safari. So why Apple suggests using openURL: instead of returning NO? As for me, it looks better to return NO and let iOS decide what to do next.
I'm implementing an iOS app that handles a custom protocol.
Writing the method application(openURL:sourceApplication:annotation:) in my AppDelegate was easy but I'm left with a problem: I want that - once the user have done with the request - my app move to the background and send the user back to the caller sourceApplication (e.g. a browser, a QRCode reader, or any another app).
This is just like the difference between "tel:" and "telprompt:" url calls: in the former case the phone app remains active, in the latter case, after the call, the user is send back to the sourceApplication.
In order to let my app handle my custom protocol like "telprompt:" does, the only way I can think about is terminate the app once the user action is completed... but this is against iOS Human Interface Guidelines (they say "Don’t Quit Programmatically") and my app can be rejected by Apple.
On Android it is easy: you respond to an Intent with an Activity and when you call finish() on that activity the user is back to his previous app/browser/whatever.
Anyone knows a good way to achieve this on iOS?
Just to clarify:
my app don't call openUrl, it responds to openUrl requests from browser, QRCode reader, other apps;
I don't have to make phone calls;
when I handle a request I ask the user for some data, contact a server, and that's it: the interaction is finished and it would be very nice to drive the user back to previous app without let him use the home button.
I believe you should call openUrl when you are done, with the source app url in param.
That's what facebook does when you use the "connect with facebook" API.
I'm building an iOS app that has an in-app web browser.
One feature that Tweetbot includes is the ability to detect if a link will exit Tweetbot when opened. Here's a screenshot of what happens when you tap an App Store link:
How would I go about detecting if the application is about to close as the result of opening a link?
Update: I suppose this question isn't being clear enough: I'm aware of the UIWebViewDelegate methods, and that [[UIApplication sharedApplication] openURL:url] bits.
What I'm trying to figure out isn't how to open a URL or how to know if a web view is opening a URL - it's about how do I evaluate a URL to determine if it will result in leaving the application without opening the URL first?
Am I watching for itms:// URLs? Is there a clean and simple way to know whether a given URL will exit the application for more than just itms:// URLs?
I can't know exactly what another app is doing but most likely they implement the UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType:.
Check to see if the type is a UIWebViewNavigationTypeLinkClicked then inspect the URL of the request. If the URLs scheme is a custom scheme then another app is going to be launched.
In addition to #rmaddy:
In an UIWebView you're only solution of detecting and intercepting links are webView:shouldStartLoadingWithRequest:navigaitonType: and check if navigationType is UIWebViewNavigationTypeLinkClicked just as #rmaddy said.
What you might think of and seek to find is this, but it doesn't intercept links in the UIWebView:
// In your code:
[[UIApplication sharedApplication] openURL:myURL];
// In your App Delegate:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
{
// Intercept links from openURL and links from other components like UITextView - present custom alert and on the alert delegate either cancel or redirect to the link
}
I am allowing user to call from my app. Is there any way to resume my app back after the call ends?
In know "telprompt" url and "UIWebView" but I don't want to use those because they both pop up to confirm call.
I am looking something automatic like "tel" URL, but I want it to return to my app after the call.
No. Even if you registered a custom url scheme for your app (say, myappname://), the Phone app will not invoke it after the call ends.
How programmatically restart an iPhone app in iOS?
I find this way http://writeitstudios.com/david/?p=54
But may be something simple.
The only way I know to do this is not ideal, but it works.
First, your app has to opt out of background execution (multitasking) The app has to quit when exited, not run as a background task. This is done with the plist key UIApplicationExitsOnSuspend.
Second, your app needs to register a custom URL scheme that can be used to launch the app.
Third, you need a web page hosted somewhere that when loaded will redirect to your app's custom URL scheme.
Forth, the user needs an active Internet connection.
To exit and restart, call UIApplication openURL on your hosted redirecting web page. Your app will exit and safari will launch and load your page. The page will redirect Safari to your custom URL scheme, prompting Safari to internally call openURL, causing iOS to launch your app.
my post that you linked to is referring to a Cocoa Application, not the iOS. On the iOS, you can quit an application (but Apple doesn't like this) by using exit(0); but I don't recommend that. You cannot restart iPhone apps though.
Unless you're developing for jailbroken devices, Apple won't even allow you to programatically terminate your app. So restarting the device is out of the question.
Your AppDelegate instance has a method
(void)applicationDidBecomeActive:(UIApplication *)application
{
}
In here, you can put logic to figure out if the app should restart, or continue doing whatever it was doing. For example you can have a BOOL variable appMustRestart that is false at first but gets triggered as true whenever something happens in your app that you'd like the next time to be a fresh relaunch.
if (appMustRestart)
{
[self resetVars]; // call a method that resets all your vars to initial settings
// INSERT CODE HERE TO TRANSFER FOCUS TO INITIAL VIEWCONTROLLER
}