IOS One app jumps to another app and communicates (e.g. a jumps to B and opens the specified page in B)

Source: Internet
Author: User
Tags lua

Feature implementation: A jumps to B and opens the specified page in b

Steps:

1. First create two projects (Project A, Project B), add the URL Types in the Info.plist file in Project B, as shown in: Where URL idenifier is the bundle ID of Project B, add a command prefix to the URL schemes, I use " PROJECTB ", this name can be taken, run the project B.

2. Add a jump code to project a

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"projectB://"]];
    • 1
    • 1

Here the URL of the command prefix must be consistent with the previous definition, I added this line of code to a button click Method, now click on the button can jump to project B.

3. Now say that the app jumps between the communication, in fact, the same as the value of the transfer. Click on the second button in project A to add code

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"projectB://openBSecondPage"]];
    • 1
    • 1

4. Project B adds a Nsurl attribute in Appdelegate that implements a proxy method that receives the URL passed from project A.

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{    self.url = url; return YES;}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

Then add the code to the first interface in B

- (void) Viewdidload {[Super Viewdidload];Nsurl * url = ((Appdelegate *) [ uiapplication Sharedapplication] .delegate) .URL; if (URL) {//shows the Url,url = Projectb://openbsecondpage obtained from a Host = Openbsecondpage self.label .text = [nsstring stringwithformat:@  "url =%@,host =%@", [url Absolutestring],[url host]; //some operations based on the host of the transmitted URLs if ([[URL host]isequaltostring:@< Span class= "hljs-string" > "Openbsecondpage"]) {//jump to the second interface [ "second" Sender:nil];}}}  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14



In short, some custom actions are made after opening project B from a URL passed from a.

The specific parsing process for B is: Nsurl * url = ((Appdelegate *) [uiapplication sharedapplication].delegate). URL;
;
if (URL) {

Nsarray *arr = [url.host componentsseparatedbystring:@ "&"];

NSLog (@ "%@", arr);

UILabel *lab = [[UILabel alloc]initwithframe:cgrectmake (10, 200, 300, 300)];

Lab.text = [NSString stringwithformat:@ "%@", arr];

Lab.numberoflines = 0;

[Self.view Addsubview:lab];

Take some action based on the host of the URL passed in
if ([Arr.lastobject isequaltostring:@ "Openurl=openbsecondpage"]) {
Jump to the second interface
Secondviewcontroller *sec = [[Secondviewcontroller alloc]init];

[Self.navigationcontroller pushviewcontroller:sec Animated:yes];
}
}

IOS One app jumps to another app and communicates (e.g. a jumps to B and opens the specified page in B)

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.