iOS one app launches another app

Source: Internet
Author: User

Problem Description: You need to be able to launch another iOS app from one iOS app.

Development environment: xcode7.3.1

Keywords: whitelist (lsapplicationqueriesschemes), register your own URL

Demo Source Code Link: HTTP://PAN.BAIDU.COM/S/1BO327SB

How do I start another app from one app??? I iOS rookie (work needs, just start self-learning iOS, now learn to sell, haha) reference on-line several articles, to achieve this function. Now, share the demo.

1. Sign up for your own URL

Create a new project Myappone, add a field to the Info.plist file, and register the Myappone URL.

1.1 In the Information property list, click "+" to add the URL types, add an item, add "URL schemes" in Item, and then add a string in the URL schemes item, " AppDemo1 "(reference), the string is the URL that the current project Myappone itself to register for itself. Register your own URL to complete.

1.2 We have just registered the URL of our own, equivalent to our current project Myappone an identity. The other app uses the interface provided by iOS (which later describes which function is iOS), and invoking this URL will launch our Myappone application.

2. Whitelist Lsapplicationqueriesschemes

After iOS9, you need to add the lsapplicationqueriesschemes (whitelist) in the Info.plist file to legitimately launch the app you want to launch. The whitelist adds the URL of the app you want to launch (this URL is the "register your own URL" explained above).

2.1 Add Lsapplicationqueriesschemes. (The current project is Xcode7.3.1,xcode does not automatically match the lsapplicationqueriesschemes, so you need to enter this field manually and change its properties to "Array"), Add "AppDemo2" to the lsapplicationqueriesschemes.

We assume that there is already a myapptwo, and that its registered URL is AppDemo2. We want to start the Myappone project to launch the Myapptwo this app, you need to add Myapptwo url in whitelist.

3.MyAppOne key code to launch another app

3.1 String AppDemo2 after the "://" is required to add, otherwise you will not be able to open the URL of the app you specified.

3.2 code describes the parameterless URL and the parameter URL. If you want to pass parameters, append these parameters after "://".

1 ////The user presses the button ********* please refer to the code in the viewcontroller.m file 2-(Ibaction) Btn1click: (ID) Sender {3NSLog (@"myappone:you Click the button1111.");4     Charszbuff[ -] = {0};5sprintf (Szbuff,"%s","appdemo2://username=heliang&pwd=20160812");6     ///1. How to pass without parameters7     //nsurl* urllocal = [Nsurl urlwithstring:@ "AppDemo2://"];8     ///2. How to transfer with parameters9nsurl* urllocal =[Nsurl urlwithstring:[nsstring stringwithutf8string:szbuff];Ten      One     //determine whether the URL can be started A     if([[[UIApplication sharedapplication] Canopenurl:urllocal]) -     { - [[uiapplication sharedapplication] OpenURL:urllocal]; theNSLog (@"Launch AppDemo2 success!"); -     } -     Else -     { +NSLog (@"No such URL."); -         ///jump to an app AppStore +nsurl* Urlappstore = [Nsurl urlwithstring:@"https://itunes.apple.com/cn/app/tie-lu12306/id564818797?mt=8"]; A [[UIApplication sharedapplication] openurl:urlappstore]; at     } -}

3.3 So far, we've covered how to start another app2 from one App1.

The steps are as follows :

app2 to register its own URL in the URL schemes of Info.plist

②APP1 Add APP2 registered URL in Info.plist's "whitelist"

③APP1 call [UIApplication sharedapplication] openurl:urllocal]; method to start App2. (You can refer to the demo in the link for further understanding.) )

How do I get the arguments passed over this startup method?

4. Get the parameters passed over

4.1 Rewrite the following application method in APPDELEGATE.M (if the function is not in the file, add the following function manually, this is the override). The arguments passed in, just in the URL. (You can refer to the demo in the link for further understanding.) )

4.2 The letter is better than no book, this article is not authoritative explanation. This example is for everyone to get started with this knowledge point of iOS. Deeper principles and applications, but also need to be slowly pondering. At present, in your own project, to achieve this function, you have succeeded. Congratulations on your success in advance!

1 /////////override the Application method ******** refer to the code in the APPDELEGATE.M file 2-(BOOL) Application: (uiapplication*) Application OpenURL: (nonnull nsurl *) URL options: (nonnull nsdictionary<nsstring *,ID> *) Options3 {4     ////Print the parameters we receive, and the Applicationkey of the application5NSLog (@"AppDemo1 is started, the following are the boot parameters passed by AppDemo2:");6nsstring* strURL =[[NSString alloc] init];7strURL =url.absolutestring;8NSLog (@"strurl:%@", strURL);9     Tennsstring* sourceapplication =Options[uiapplicationopenurloptionssourceapplicationkey]; OneNSLog (@"sourceapplication:%@", sourceapplication); A     returnYES; -}

Reference Links:

http://www.jianshu.com/p/e14df92cb207

http://lysongzi.com/2016/03/13/iOS-%E4%BD%BF%E7%94%A8URL-Scheme%E5%AE%9E%E7%8E%B0app%E4%B9%8B%E9%97%B4%E7%9A% 84%e9%80%9a%e4%bf%a1/

iOS one app launches another app

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.