Reliably opening App Store links from a UIWebView - iphone

I have an app that will display web pages within a UIWebView. The pages it shows may contain links to other iPhone apps on the App Store.
In a normal browser on a desktop computer, clicking these App Store links would take me through a number of URL redirects and eventually end up opening iTunes and taking me to the page for that App.
Is there a way to ensure that when an App Store link is clicked inside my UIWebView that the App Store app on the iPhone will open and show the app?
What I've been seeing in my tests is that there are several types of links that can result in an App Store page, those being:
phobos links - phobos.apple.com/etc etc etc
itunes.com/app/appname links
referral / affiliate links
any that I don't know of
When I open any of these links in a desktop browser they will work and eventually open iTunes.
When I open any of these links from within the iPhone the UIWebView goes through a number of redirects and eventually one of two things will happen:
The redirects don't work properly and I end up with a page did fail to load method call
The redirects work and the iTunes app is opened, a search for the app name is done, and then I get a message from iTunes explaining it can't connect to the store.
The only time I've been able to get the App Store app to open is by using a direct iTunes link to the app without any referral or redirects.
Obviously for referral or affiliate links, I do not want to strip out the referral ID or affiliate ID. I shouldn't deprive them of a referral if it was their link that is clicked.
So any help would be greatly appreciated.
Thanks.

I found this Technical Q&A from Apple that answers my question:
The basic gist is this:
phobos.apple.com links constructed properly will redirect directly to the App Store app.
itunes.apple.com links must be converted into phobos links.
referral/affiliate links must be traversed using NSURLConnection and the final resulting URL will be a phobos link that can be used.
Thanks for your help guys.

On my tests, I only got phobos.apple.com links to automatically redirect to the AppStore (without any Safari redirect).

Addition to developer documentation, I think they should have the case when the redirectResponse is nil.
It took me some time to figure out what was wrong.
// Save the most recent URL in case multiple redirects occur
// "iTunesURL" is an NSURL property in your class declaration
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response {
if (response) {
self.iTunesURL = [response URL];
}
else {
self.iTunesURL = [request URL];
}
return request;
}

If you have not tested this yet on an actual device, I can tell you that the iPhone Simulator has problems with redirecting these links to the App Store (probably because the Simulator doesn't have it). Running you application on the device will yield different behaviors in this specific area, so make sure you're testing it there.

I've been trying to do the same thing. I wanted to place a link to the full version of my app in the free version. I just confirmed that the method used in the document works. ONLY on the actual device. Never trust the simulator!
Add the stuff in the document, and call it like this :
NSString *testLink = #"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=284417350&mt=8";
self.iTunesLink = [NSURL URLWithString:testLink];
[self openReferralURL:iTunesLink];

Related

launching the facebook app to a group from iOS

What I am looking to do is launch the facebook iOS App from my iOS App. I can do that with fb://URL but what I need to do is start it off in a facebook group page. It seems like all I can do is launch the app to the users news feed. I have no problem launching safari from my app direct to the group using the URL you normally would use (https://www.facebook.com/groups/group_name) but I would rather have people end up in the iOS app not safari.
I expect I might have have more luck using the facebook API inside my program but think its will be less maintenance in the long run if I let my users just use the official app for being a part of our group.
You are already aware of the existence of the fb:// url scheme, which is the way to go.
calling it goes like this :
NSURL *theURL = [NSURL URLWithString:#"fb://<insert function here>"];
[[UIApplication sharedApplication] openURL:theURL];
In the place of <insert function here>, you could use the following ways to open the facebook app straight to a group of your choice (note that you must use the group id rather than its name):
fb://group/(fbid)/members
fb://group/(initWithGroupId:)/members
fb://groups
The last one opens to the groups overview (which you probably do not want), only added that for completeness.
Akosma has an overview of a great deal of app url schemes for ios here

How to get/view/read the current URL is in Safari?

Did lots of searching, just came up with the "how to open a URL in Safari..."
Looking to find how to get/view/read what the current URL is when Safari is opened.
Edit:
My app opens a URL with Safari as follows:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:myURL]];
Supposidly, after some validation from website, the ending URL has some attached info.
So I'm trying to get that final URL from Safari.
This is not possible, you do not have access to any outside app. However how most of these authentication / validation techniques work is (like facebook / dropbox login etc.) is that they use some kind of callback back to the app.
You can register an specific url scheme to your app which can have parameters, this way you can get the info in the url the you need. The website will need to support callbacks though or you would need to be able to access the website to provide it yourself.
On how to work with URL schemes you can check out this tutorial:
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-working-with-url-schemes/
You cannot view this information, as the other posters have said. What you need to do is to use your own website to match the user of the app to the user on the website.
For example, you can send a random number to your website when the user is sent there buy.php?id=123445. Then request (periodically and/or with a button press) via the iPhone the result from your website ex. check.php?id=123445.
This also could be done with cookies and php sessions.

iPhone: fb://... URL opening the Premier Inn app, not Facebook

I don't know a lot about how the iOS handles URLs, but it appears to be doing something strange on my phone...
We have a 'Like' button in our app, that when clicked on is supposed to open the Facebook application (or Safari, if Facebook is not installed). For some reason, when I click it on my phone, it opens the "Premier Inn" app. I have both the Facebook App and Premier Inn app installed on my phone, but not sure why Facebook isn't opening when the following code is executed:
NSURL *fanPageURL = [NSURL URLWithString:#"fb://page/BIG_NUMBER_HERE"];
if (![[UIApplication sharedApplication] openURL: fanPageURL]) { ... }
For obvious reasons, I've substituted the page number for BIG_NUMBER_HERE in the above code.
Can anyone explain why this might be going wrong? Could the Premier Inn app have "registered" itself as a handler of the fb:// urls somehow?
Regards,
Just as you suggest, it sounds like Premier Inn has registered as a handler for "fb". There's no central registry to stop applications from using the same URL scheme, it's really just down to good luck and reasonable namespace choices by developers.
Edit: I can't find a canonical source for this, but the handler application that's called is that which is most recently installed.
try change that page to profile.

How to post images on facebook wall on a single button click from an iPhone app

I am making an iPhone app where in the user wants that the selected image should be posted on facebook wall on a single button click.
I have Actionsheet button for this.
I want that both authentication of user(loginButtonTapped in tutorial below) and posting images to the wall(rateTapped in tutorial below) be carried out on a single button click.
I am using the example on the link below as my reference for posting images on facebook wall
http://www.raywenderlich.com/1626/how-to-post-to-a-users-wall-upload-photos-and-add-a-like-button-from-your-iphone-app
I am a newbie.
What should I do?
How can I proceed?
Please Help and Suggest.
Thanks.
You can use the facebook Graph Api for posting images to wall..
Here is the tutorial for this...
http://www.capturetheconversation.com/technology/iphone-facebook-oauth-2-0-and-the-graph-api-a-tutorial-part-2
Before sharing you will always have to get the Facebook credentials first by either opening the Facebook App (which is called SSO / Single Sign On) or, in case the Facebook App is not installed, by opening the WebView or Safari. Posting stuff to Facebook can be pretty tricky therefore I wrote a simple lib, called BMSocialShare. It is also supporting "single click sharing". You can do things like uploading photos to Facebook easily:
BMFacebookPost *post = [[BMFacebookPost alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
[[BMSocialShare sharedInstance] facebookPublish:post];
or also create a normal post:
BMFacebookPost *post = [[BMFacebookPost alloc]
initWithTitle:#"Simple sharing via Facebook, Email and Twitter for iOS!"
descriptionText:#"Posting to Facebook, Twitter and Email made dead simple on iOS. Simply include BMSocialShare as a framework and you are ready to go."
andHref:#"https://github.com/blockhaus/BMSocialShare"];
[post setImageUrl:#"http://www.blockhausmedien.at/images/logo-new.gif"
withHref:#"http://www.blockhaus-media.com"];
[[BMSocialShare sharedInstance] facebookPublish:post];
I wanted to add on my personal experience on using BMSocialShare created by vinzenzweber here, and my reputation only allows me to answer. It saved me a lot of time and from the trouble of reading Facebook's documentation, for something as simple as sharing an image for my photo app.
Just a couple of pointers on the documentation of it which might not be immediately obvious for someone using the library for the first time.
When inserting the snippet into your Info.plist containing the Facebook app id, you need to affix 'fb' in front of the number app id.
Besides adding the BMSocialShare.framework into the Xcode Project, you need to manually drag the BMSocialShare.bundle and the FBDialog.bundle in the resources folder of the BMSocialShare.framework, into the Copy Bundle Resources section under Build Phrases of your application's target

How can I launch app store from my application

Im trying to implement button which opens app store application from my app. I use this simple line of code, which opens safari but not app store application.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://itunes.apple.com/sk/app/tweetie-2/id333903271?mt=8"]];
I dont know whats wrong, is the URL format correct? I was following this document.
All related questions in stackoverflow are outofdate I suppose.
If you want to bypass Safari, change 'itunes' in the URL to 'phobos'. Note that this will fail in the simulator, but most definitely works on the device.
According to the documentation, iTunes affiliate links have to process several redirects in order to wind up in iTunes. Follow the example to create an NSURLConnection, set yourself as its delegate, and use the string it ends up with to open with UIApplication.