Swift: Open app in Safari

Source: Internet
Author: User

What happens before you open it, first look at this diagram:

I just have a simple mock-up here. When you enter a special "url", Safari pops up a hint asking if you want to continue to open the app. If you confirm at this time. Then the app will open.

Let's open up and see what kind of app this app is. Look at the picture:

It is a tableview of grey often. Then clicking on a line will jump to a place that claims to be detailed. There is nothing in fact. There is only one place to look, and that is the title of this page. Remember this title, the back of the function of the show is all on him.

That's what it looks like when you jump to the detail page:

Yes, just one title, then nothing else.

Let's move on to the above. As mentioned above, entering a special "URL" in Safari will open the app after confirm the popup box for Safari. This is a primary function. Because this is obviously not enough. What we need is for the user to complete the Confirm popup to open the Detail page later. Step-by-step.

1. Select your project in the xcode6+ version, then select Infotab. Look at the picture:

A total of three steps from left to right is the place to set the URL scheme. Figure:

At the bottom of the info page there is a URL Types, click on the inside Set URL scheme for deeplink, of course, here you can set any URL scheme as you want.

Here, save it and run your app. The app runs up and then presses CMD+SHIFT+H to the app interface. Then open Safari, enter deeplink://in the Address bar, then click the Go button. Next, a comfirm pop-up box pops up. Click Open and your app will open. Figure:

1. Enter the URL scheme you defined in the Address bar.

2. Click the Go button to eject the Confirm box

When you click the Open button, your app will open.

But.. We're not just doing that. What URL scheme can offer us is not just that. We're going to open the detail page.

url With scheme, you can have a bunch of things in the back. For example: http://baidu.com. You can have baidu.com after you have http://. We can also use this. Add a bunch of numbers behind the deeplink://, which can represent a lot of numbers. Here we use this number to represent the product number of the product for which a detail page needs to be displayed. These are very simple, but how to achieve it???

func  application (application:   uiapplication , OpenURL url:  Nsurl , sourceapplication:  Span class= "S3" > String ?, annotation:  Anyobject ?)->  Bool , this method is in the Appdelegate class. The implementation is not provided by default and we need to add it ourselves.

When this method executes, add a piece of code:println(URL. Absolutestring), which can be printed out to open the URL of the app. Of course we are, for example, I want to include the product's detailed number is: "deeplink://1234567890". So the entire string is printed here: "deeplink://1234567890". Because, our URL scheme is immutable. Then we can intercept the specific number of the product.

var itemcode = (URL.  Absolutestring! as nsstring). Substringfromindex(one)

This will allow you to get the product number. Then call the window's Rootviewcontroller (it should be stated that our Rootviewcontroller is uinavigationcontroller). Just assign the product number to the controller on the Product detail page, and let the controller use this product number to get the details of the product from the backend of the server. Of course, in the later implementations, the content that gets the information from the backend is not included. These features are also easy to add.

we add a product number attribute to the Detailviewcontroller: var productdetail: String!. This property is then assigned to the controller's title, which is displayed on the page.

    Override Func Viewdidload () {        super.viewdidload ()        Self.view.backgroundColor = Uicolor.whitecolor ()               self.title = Self.productdetail    }

after the concrete presentation. and see how it says: func  application ( application:  uiapplication , OpenURL url:  nsurl , sourceapplication:  String ?, annotation:   anyobject ?)   Bool How to handle a page jump, Do not deal with the words just opened the Uinavigationcontroller Rootviewcontroller.

This kind of processing is actually very simple. Uinavigationcontroller originally already is rootviewcontroller, as long as push the initial good detailviewcontroller on Bob. Very simple. Code:

        If let window = Self.window, let Rootcontroller = window.rootviewcontroller {//            Self.window?. Rootviewcontroller            println ("window is cool")            var navcontroller = Rootcontroller as! Uinavigationcontroller             var detailcontroller = Detailviewcontroller () detailcontroller.productdetail = ItemCode Navcontroller.pushviewcontroller (Detailcontroller, animated:true)        }

So it's done. Open Safari and enter "deeplink://1234567890" to see how it works:

So far so good! However, there is one more bug. Repeat the experiment and you'll see. Think about how to solve it, it is after class homework.

Welcome Dabigatran to learn from each other and make progress together. QQ Group: 58099570 | To be kind, reprint please indicate the source!

Swift: Open app in Safari

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.