iOS: Launch Safari with URL automatically - ios

some basic questions for a very basic app:
For the first time when the app runs, the user is presented with a screen where an ID (number) has to be selected. This ID is then embedded in a URL that is used when launching Safari as the user is pushes a Start button. The ID is stored in the user preferences.
The second time the app is launched, I just want to read out the ID from user preferences and launch Safari, without the user seeing the ID screen or pushing any buttons.
The first scenario I got working by reading out the ID in viewDidLoad etc.
For the second scenario I'm pretty much clueless. Where should that code be placed. Is it technically possible? Would Apple accept this?

Yes, It's possible. And why Apple would not accept this? Just use this code:
[[UIApplication sharedApplication] openURL:url];
url - your composed URL with user ID.

Related

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!

Open iOS app from browser

What I want to do is,
We have having one product info on Website.
That product is available on store.
what we have on website is that, Product info and one button for that product.
I want to take two actions on that button.
When User opens website on iPad or iPhone on Safari (browser) and click on GetProduct button, then following two actions must be taken place.
1. If user is already having product installed on device then directly open the app in device.
2. If user is not having the app on device then link user to the app on store, so he can download from there.
I already handled second condition,
but how to handle the first condition.
If I am already having the app then how to open it on action of button click in browser.
You can achieve what you're asking for by using a URL scheme. This will enable you to call the openUrl: method with your application's url scheme which will then launch your app.
Here's how you setup a custom url scheme:
Open your app's Info.plist and add a row with a key called URL Types.
Expand the URL Types item, and Item 0 under it and you'll see URL Identifier
Enter your app's bundle identifier (e.g. com.myCompany.myApp) as the URL Identifier value.
Add another row to Item 0 and enter URL Schemes.
Expand the URL Schemes and under Item 0 type in the name for your custom scheme (e.g. myScheme).
You should now be able to open your app from Safari by typing myScheme:// in the address bar.
Alternatively, from your app, you can launch the other app like this:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"myScheme://"]];
Note that you can also send parameters to the app you're launching with the url scheme (more on that here).
With iOS9 Apple introduced a way to open installed app from Links. Here is the official link for this : Apple universal links

Invoking Safari on iOS without opening a new website

The situation is like this:
User opens app from a website using a custom urlscheme
User does stuff in the app
User clicks button in the app to return to the website in Safari.
I have tried opening a new tab containing a javascript:window.close() but this does not work on iOS 6.1.
So my question is: Is there a way to open Safari to view the website the user left from? Either with a working new tab that closes itself or a different route?
When you open the app with your custom url scheme, pass the actual page url as an argument.
mycustomUrlScheme://mydomain.com?objectid=1234&callback_url=encoded_url
In your app, handle the url for the content info and keep the page url to open it afterwards. It will make safari open a new tab. But that should be a good start.
As far as i understand you can do it.
user opens mobile safari for example http://www.example.com
user clicks a link that is appscheme://open and the application become active
user taps a button to open safari for example http://www.example.com?q=test
for the third step you can use [[UIApplication sharedApplication]openURL:url]

"Gift App" from inside the app

I noticed that on the latest angry birds update they added a feature to gift your app from inside the app.
Up till now I knew you can gift paid apps from the iTunes itself. Does anybody know what link should I use to access this mechanism from inside the app itself?
Thanks!
Actually, you'll want your URL to start with itms-appss: if you want it to open in the App Store app, where someone would actually gift an app. This feels more natural than Safari popping up.
// example app id for batman arkham city lockdown
#define APP_ID 459850726
NSString *GiftAppURL = [NSString stringWithFormat:#"itms-appss://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/giftSongsWizard?gift=1&salableAdamId=%d&productType=C&pricingParameter=STDQ&mt=8&ign-mscache=1",
APP_ID];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:GiftAppURL]];
APP_ID should obviously be defined to the Apple ID of your app.
Also worth noting, the URL is case sensitive.
If you watch what happens when you click that button, you can see that it initially makes a request to a redirect script on www.angrybirds.com:
http://www.angrybirds.com/redirect.php?device=iphone&product=angrybirds&type=purchasegift
From there you are redirected to a secure url of the form:
https://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/giftSongsWizard?gift=1&salableAdamId=343200656&productType=C&pricingParameter=STDQ
343200656 is the AppleID for Angry Birds.
I have here some step-by-step instructions in how to add a 'Gift This App' button into your app:
Add a button in your XIB and add an action to it.
In your .m add the actions brackets e.g:
-(IBAction)actionName {
}
add this code in and replace APP_ID with the number in the apps web page link for e.g.
itunes.apple.com/au/app/[APPNAME]/id**APP_ID**?mt=8
this is a code e.g:
- (IBAction)actionName
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/giftSongsWizard?gift=1&salableAdamId=**[APP_ID]**&productType=C&pricingParameter=STDQ"]];
}
Hope this helps!

In-App review feature in iPhone App

I have recently seen in some Apps that review and rating (with 5 stars) can be integrated into the app. Does anyone have an idea how this is done? e.g. with a http request?
More specific: Can I create a view in my App with a UITextField and a Button, so that when the user writes his review in the textfield and click send, the review should be posted to the "Customer Reviews" in the App Store? and the rating should also be done inside the App similarly.
Yes, I did this with my own app. I have a rating button that hits the following method on click:
- (IBAction) reviewPressed: (id) source {
NSString *str = #"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=341086403";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
You will need to replace the ID in the URL with your own App ID. For new apps, this presents you with a chick-and-egg problem - you don't have an ID until the App goes live.
You can get your application ID by going in the process of submitting an app but choose to upload the app binary later. You will then get the app ID and thus add it in your code.
I think its late but this is the best solution.
You can use below url to directly send user to write a review page on App Store. Just need to replace application id.
itms-apps://itunes.apple.com/gb/app/id1136613532?action=write-review&mt=8
Example :
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"itms-apps://itunes.apple.com/gb/app/id1136613532?action=write-review&mt=8"]];
Here My Application Id is 1136613532. You can replace it with yours.
Tested in iOS 10.3

Resources