iOS: Open a registered open URL from within the same app - ios

I am running into a situation which I am not sure is possible from technical/design point of view. Please advise.
Here is what I need:
I have an open URL registered for my native iOS app. I expect a request ID to be passed along with it and once hit I open that request.
From within my native iOS app, I need to open a web page in the webview. This page has few buttons in it.
A tap on the button in the webview should open the request inside my app. So, I want to trigger the registered open URL in step #1.
Web page data is dynamic and will change on the fly.
Is this a feasible design. Shall I consider something else.
Any advise/pointers will be appreciated.

This should be possible. Here's a though:
1: Implement the UIWebview delegate method shouldStartLoadWithRequest.
2: when you intercept that your webview is attempting to access the url yourapp://blah-blah-blah you can return NO instead dip into your appdelegate can manually call the function handleOpenURL.
I've seen something similar in the past with supporting oAuth (I believe it was with instagram) within one of our apps. We basically loaded up the login page in a UIWebview and then when we detected the the post login redirect we parsed the oAuth token from the url and called it good.
Good luck

Related

Set your app to detect and open when a specific link is clicked - Xcode, Swft 4

I'm currently working on an iOS app, Swift 4, that automatically launches on click of a link to block users from accidentally navigating to that link.
How would I set this up?
For example, YouTube opens up youtube.com links.
Sorry if this is a vague question, if more info is required feel free to ask ^^
If you create a custom URL protocol like myapp://somepath (where myapp is the protocol) then you just register that custom protocol, and when the user clicks such a link it’ll automatically open your app. You can’t intercept a general purpose URL protocol like HTTP or YouTube.
(At least not without OS support. That's how YouTube and the app store are able to open HTTP links.)
I haven't done anything like this so far but it seems like you should take a look at Apple URL Scheme and Universal Links:
https://developer.apple.com/library/content/featuredarticles/iPhoneURLScheme_Reference/Introduction/Introduction.html
https://coderwall.com/p/mtjaeq/ios-custom-url-scheme
https://developer.apple.com/library/content/documentation/General/Conceptual/AppSearch/UniversalLinks.html
https://www.raywenderlich.com/128948/universal-links-make-connection?utm_source=raywenderlich.com%20Weekly&utm_campaign=95f29cf4fc-raywenderlich_com_Weekly5_31_2016&utm_medium=email&utm_term=0_83b6edc87f-95f29cf4fc-415182745

iOS WKWebView Handle File Download

I am facing the following problem:
In a web interface, file downloads are triggered with an anchor tag, like this:
<a href="/bla/blabla" download>..</a>
While Safari browser can handle this request and open a dialogue to handle the file, WKWebView treats this just as an ordinary link and does nothing with it. I want to be able to get the file handler dialogue that is normally there when using Safari.
Right now there are 2 problems and I do not see an opening there yet:
I can not detect a click on the element as it is treated just as a normal link. And I can not rely on the URL parameters to detect if it is a file since that is not constantly true.
Even if the URL is defined as a one leading to a file, I can not pass it to Safari since it does not share session info and cookies with my app's WKWebView.
Therefore, I would like to know if there is any opening in handling files in iOS WKWebView. Thank you.

Options for presenting website when website/app supports Universal Links in iOS 9

In iOS 9 I can register my website (FooSite) to link to my app via Universal Link. Now let's say a user in another app (BarApp) encounters an HTTP link to my app (FooApp).
Does BarApp have the option to present FooSite instead of allowing iOS to resolve the universal link and launch FooApp?
Do the options change between UIWebView, WKWebView and Safari View Controller?
To answer your first question, BarApp should not. I haven't tried to prevent this, but I can't see how, short of not firing the URI at all, BarApp could launch FooSite rather than FooApp.
To answer your second question, SFSafariViewController is extremely limited in what the developer is allowed to access. You can set an initial URL, know when the initial URL loads, and know when the user has tapped the dismiss button. That's it. No inspecting the current URL, no intervening in requests, etc.

iOS - Return to Safari from Native App without Opening New Tab

I am having trouble figuring out how to switch to Safari from a native app in iOS 7+. I've used UIApplication.sharedApplication.openURL(), but that opens a new tab. I would like to return the user to the current page he/she was viewing before without opening a new tab. I found this SO post, but it is a few years old, so I was hoping things have changed since then.
Here is the workflow I am envisioning:
User taps on a link on an HTML page on Safari to open/install my app
User performs an action on my app
After the user is done performing the action, my app opens Safari automatically, and the user is back on the page where he/she left off
Google has somehow done this with their Google Maps app. If you search for an address on google.com on Safari, you can tap on the map that appears in the search results, and it will open the Maps app. At the top of the Maps app will be a "Return to Safari" bar that you can tap. Once you tap it, you are returned to Safari without loading another tab. I can't seem to find anything regarding how Google did this. If I can replicate that behavior in my app, that would work just fine.
Any help would be greatly appreciated!
There is a way to accomplish what you want using standard iOS APIs. No need to use external components.
You control your webpage and your app, so you know the exact URL that has the link to your app.
These are the steps:
1) In your app, define a custom URL scheme. In this case let's assume you use the scheme myawesomeapp://. You can do this in your Xcode project by going to the Info section of your target. See below
2) In your web page you need to handle the two scenarios: app installed / not installed. It is just a matter of detecting if an app responds to the scheme myawesomeapp://.
To detect from your webpage if your app is not installed please refer to this post
I will explain the case where your app is already installed.
Let's say the webpage that contains the link is:
http://www.mywebsite.com/mypage.html#mytag
The link you provide in your webpage should pass some parameters to the app and one of these should be the URL that you want the app to return. Following with the example the link could be:
myawesomeapp://?action=my_action_1&sourceurl=http%3A%2F%2Fwww.mywebsite.com%2Fmypage.html%23mytag
Note that the URL you pass as a parameter inside the scheme has to be URL encoded or it won't work properly.
3) In your app delegate you need to implement the method:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
In this method, parse the URL, decode the query items and pass the sourceURL to the view controller responsible of handling the action prior to calling it. In this case I set a public property in the ViewController that will store the URL.
#property (nonatomic, strong) NSURL *sourceURL;
4) In the view controller when the user finishes the interaction, you just call:
[[UIApplication sharedApplication] openURL:self.sourceURL];
Because self.sourceURL contains the URL of your webpage, Safari will be launched with the URL. However, because that page is already opened, iOS detects this and re-opens that page.
I have a sample project in my Github page that implements all this.
And to finalize, after you install the sample project in your iPhone, open this stack overflow post from mobile Safari and open my awesome app
Once the app is opened, click on the button and you will return to this stack overflow post.
The behaviour you described is exactly what FB's AppLinks is designed for, and you'll get the same behaviour with all iOS apps that support it (which is quite a lot) out of box!
By the way Google Maps uses the same component: you can see it if you open Google Maps from let's say Fantastical.app!

Can app open browser, get user to login and receive file?

I'm trying to write an iOS Objective-C app to login to an old website. Note I don't have control of the website so I have no way of changing the login.
The procedure is:
Open browser at specific URL.
User enters their username and password.
Website returns a file called something like bad_link.smith.
App reads file to get key to use for API.
Is it possible to do this in iOS - open a URL from an app and have the file returned?
You could use UIWebView Delegates for this
Implement this https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIWebViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UIWebViewDelegate/webView:shouldStartLoadWithRequest:navigationType: delegate method and get the URL the user is going to and if the URL matches the URL you want to download then you can get the URL by the NSURLRequest and download it seperately. :) Hope I helped you.
What other thing you could do is make the code native and without the browser and simulating as a browser in the code. Can be used if no CAPTCHA is there. Do tell if you need more info on this.

Resources