Open iTunes links to apps in UIWebView if app is already installed - iphone

If I open this url in Safari iOS would check if I have this app installed. If it is - the app will be launched otherwise I will be redirected to the AppStore app.
Is it possible to make UIWebView to do the same thing?
The key thing is that system checks weather the app installed and has CFBundleURLTypes registered to respond.
EDIT:
Sorry guys I was wrong with identifying the problem. There is actually another link that redirects to the direct (local) link that is registered for the app. I catch this now in
-(BOOL) webView:(UIWebView *)inWeb
shouldStartLoadWithRequest:(NSURLRequest *)inRequest
navigationType:(UIWebViewNavigationType)inType
and call
[[UIApplication sharedApplication] openURL:url]

If UIApplication openURL does what you want (open the app page in App Store or open the app if it's installed; I guess it will do so) you can "steal" the url opening event in the UIWebView in the following way:
// myWebView will be your initialized web view
[myWebView setDelegate:self];
// implement this method in the owning class
-(BOOL) webView:(UIWebView *)inWeb
shouldStartLoadWithRequest:(NSURLRequest *)inRequest
navigationType:(UIWebViewNavigationType)inType
{
if (inType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
This will open all links clicked in the web view in Safari / App Store / default handling app. If you want to open only iTunes links this way, you might want to implement some URL parsing in this method and redirect only those links. If you return YES, the default handler will be called i.e. the link will be opened in the web view.

The behavior you describe isn't how it currently works on iOS 5.1
clicking on iTMS link a second time will still launch App Store.
This may help you with detecting if a app already installed
https://stackoverflow.com/a/5746603/515359

Related

Reverse back to app from safari [duplicate]

This question already has answers here:
Return to app from safari
(4 answers)
Closed 9 years ago.
In my application I am calling an url in safari on a button click,I am using the following code
In safari I am using pay pal payment.Is there any chance to reverse back to my app after donate button click.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
NO That is not possible in case of websites that you dont have control. Instead, you can open the link in your webview.
If you have developed website, you can redirect it to your app using custom URL. For details refer this tutorial
For your case i.e. Paypal, it is not possible
If you are trying to navigate from Safari to your app, you would need to have control of the web page that you send the user to. Create a button on the webpage that links to the custom URL scheme of your application. Then when the user clicks on the button, your app will open on the device.
Instead of using [[UIApplication sharedApplication] openURL:] method, you can open your website into website inside your viewcontroller, with back button.Thus you can go back into your app from safari..
Follow this snippet,
`NSURL *url = [NSURL URLWithString:<YOUR_URL>];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
webViewBrowser.delegate = self;
[webViewBrowser loadRequest:request];`
Enjoy Programming!!

iPhone: After UIWebView opens external browser, then returning to app opens it a second time

I have a UIWebView which loads an HTML page which has some links to the App Store (<a herf="http://itunes/bla/bla/bla"> type of links).
Clicking a link, opens external Safari, then opens external App Store app and successfully goes to correct page.
If you then click Home button and return to springboard and click on app again, it will briefly open to the the UIWebView that was displayed and then jump to external Safari and external App Store app again.
If you go back to app one more time it stays in the app.
So what is happening is that returning to app after opening external browser link from UIWebView HTML page, will then jump back to the same link a second time.
Anybody have any ideas what might be causing this?
Am using Xcode 4.2.1 and it happens in simulator as well as on an actual device (iPad 1 with iOS 4.3).
EDIT - SOLUTION:
Ok, here is what I had to do to solve the problem (Thanks to Ben's response below who got me looking at the right areas):
This was my original method:
- (BOOL)webView:(UIWebView *)_webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (didLoad) { //Do not jump to URL when view first loads
return YES;
}
[[UIApplication sharedApplication] openURL:[request URL]];
return YES;
}
And this is my fixed method that does not keep jumping to URL:
- (BOOL)webView:(UIWebView *)_webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (didShowURL) { //Do not jump to URL when returning to app
didShowURL = 0;
return NO;
}
if (didLoad) { //Do not jump to URL when view first loads
return YES;
}
didShowURL = 1;
[[UIApplication sharedApplication] openURL:[request URL]];
return YES;
}
I would suggest using the itms-apps:// protocol for your link. This will send the user directly to the application in the App Store without the need for a redirect.
NSURL *appStoreLink = [NSURL URLWithString:#"itms-apps://itunes.apple.com/us/app/instagram/id389801252?mt=8"];
[[UIApplication sharedApplication] openURL:appStoreLink];
Your webview is being reloaded to its last page (the app store page) and is automatically redirecting the user away again. You just need to make sure that when the app is reloaded it is not trying to load the app store link again.
Depending on your app setup you could use
viewWillAppear
And reload the original HTML page with the links every time the view is brought to the front.

UIWebView: open some links in Safari, some within the view

My app features content that (for text formatting reasons) is presented in an UIWebView. Within the content there are links, some of which should open their target in mobile Safari, while others should navigate within the content.
So far, I've catched the link requests using a UIWebView delegate. In my implementation of
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
I'd check the requests URL using lastPathComponent or pathComponents for known elements to determine whether to open the link externally or within the view.
However, I just found out said methods are only available since iOS 4.0, which would render the app useless on iPad. Plus I have the feeling I'm using a dirty solution here.
Is there another way to somehow "mark" the links within my content in a way that makes them easy to distinguish later, when processing the request in the delegate method?
Thanks alot!!
You could covert the URL request into a string, and do a compare for a subdirectory on your website, such as in URLs that only start with "http://www.sample.com/myapp/myappswebcontent/", against the initial substring of your URL. Anything else, send to Safari.
You should set a policy delegate of web view:
For instance in the controller, that contains a web view
[webView setPolicyDelegate:self];
and then override a decidePolicyForNavigation method (this is just an example):
- (void)webView:(WebView *)sender decidePolicyForNavigationAction: (NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id <WebPolicyDecisionListener>)listener
{
if ([[actionInformation objectForKey:WebActionNavigationTypeKey] intValue] == WebNavigationTypeLinkClicked) {
[listener ignore];
[[NSWorkspace sharedWorkspace] openURL:[request URL]];
}
else
[listener use];
}
you can distinguish there kind of link and ignore or use the listener. If you ignore it, you can open the link in safari, if you use it, the link will open in your webview.
HTH

How can I jump to the App Store from a UIWebView link in my iPhone app?

I want to take a user OUT of my app and into the App Store when they click on a link within a UIWebView in my app. But when I click on the link, nothing happens (sim AND device both). How can I jump to the App Store from a UIWebView link in my iPhone app?
BY THE WAY:
The link in my UIWebView html is as currently follows (minus the self-promotion)
Download Now
Hmm. Perhaps you’re right. In that case, simply use this function of the UIWebViewDelegate:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
Parse the NSURLRequest to see if it’s an iTunes link and if it is, do this:
[[UIApplication sharedApplication] openURL:someURL];
use code:
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:#"http://itunes.apple.com/xxxxx"]];

opening an URL in safari while iPhone button is clicked

I want to add a button to my iPhone application which will open an URL in safari on iPhone when clicked (my application will terminate right?)
Thanks.
Raihan
You need to use - (BOOL)openURL:(NSURL *)url in UIApplication.
UIApplication is a singleton, so your code would look something like this:
NSURL *url;
[[UIApplication sharedApplication] openURL:url];
Your app will close if you are simply opening in Safari.
You can, however, use a WebView control to embed the web browser in your app. This way your app will not terminate.