IOS opens another app in one app and its two apps call each other

Source: Internet
Author: User

Open another app's solution in an iOS app

Recently to implement an application in iOS to launch another application of the function, search for some information, using UIApplication OpenURL: The method can be achieved, now organize and share with you!

Registering a Custom URL protocol

The app that was launched first needs to register a custom URL protocol with the iphone. This is done in the Info.plist file of your project folder (which is the same file you changed the application icon).
Step1. Right-click and select "Add Row" STEP2. Key value Select "URL Types"
Step3. Open the Item 0″, and then add a URL identifier for the key. Can be any value, but it is recommended to use "anti-domain name" (for example, "Com.fcplayer.testHello").
Step4. Add a line under "Item 0".
Step5. Select "URL schemes" as key.
Step6. Enter your URL protocol name (for example "testhello://" should be written "Testhello"). If necessary, you can add multiple protocols here.
The operation is as follows:

Accessing the Custom URL

Launch another app in the main application by accessing a custom URL:

[CSharp] View plaincopy
    1. Nsurl * myurl_app_a = [Nsurl urlwithstring:@"testhello://"];
    2. if ([[[UIApplication sharedapplication] canopenurl:myurl_app_a]) {
    3. NSLog (@"Canopenurl");
    4. [[UIApplication sharedapplication] openurl:myurl_app_a];
    5. }

Custom Processing URLs

Sometimes we need to send parameters to another application in addition to booting, which can also be done through custom URLs, such as:

testhello://
Testhello://com.fcplayer.testhello
testhello://config=1&abar=2

In this case, we have to do the custom processing in the launch application, and implement the message in delegate (Cocos2d added in appdelegate), for example:
-(BOOL) Application: (UIApplication *) Applicationhandleopenurl: (nsurl*) URL {//do something withthe URL here}

In general, we parse the URL from the parameter to display it in the view or store it in the userpreference. The following example stores the URL as a URL variable in user preference or prints it out:

[CSharp] View plaincopy
  1. -(BOOL) Application: (UIApplication *) application Handleopenurl: (nsurl *) URL
  2. {
  3. if (!url) { return NO;}
  4. NSString *urlstring = [url absolutestring];
  5. NSLog (@"%@", urlstring);
  6. //[[nsuserdefaults Standarduserdefaults] setobject:urlstring forkey:@ "url"];
  7. //[[nsuserdefaults standarduserdefaults] synchronize];
  8. return YES;
  9. }

Other

Basically, we've implemented the ability to launch another app in one application, but in order for our code to be stronger, I found another access code on the Internet, as follows:

[CSharp] View plaincopy
  1. Check if the user is configured with AppID
  2. There is no cfbundleurlschemes field for accurately configuring info
  3. Can you open it correctly?
  4. if (!kappid) {
  5. Uialertview *alertview = [[Uialertview alloc]
  6. initwithtitle:@"Setup Error"
  7. message:@"Missing app ID. You cannot run the app until your provide this in the code. "
  8. delegate:self
  9. cancelbuttontitle:@"OK"
  10. Otherbuttontitles:nil,
  11. NIL];
  12. [Alertview show];
  13. [Alertview release];
  14. } Else {
  15. //Now check the URL scheme fb[app_id]://authorize are in the. plist and can
  16. //Be opened, doing a simple check without the local app ID factored in here
  17. NSString *url = [NSString stringwithformat:@"Fb%@://authorize", kappid];
  18. BOOL bschemeinplist = NO;  //Find out if the sceme are in the plist file.
  19. nsarray* abundleurltypes = [[NSBundle mainbundle] objectforinfodictionarykey:@"Cfbundleurltypes"];
  20. if ([Abundleurltypes Iskindofclass:[nsarray class]] &&
  21. ([abundleurltypes Count] > 0)) {
  22. nsdictionary* ABundleURLTypes0 = [Abundleurltypes objectatindex:0];
  23. if ([ABundleURLTypes0 iskindofclass:[nsdictionary class]]) {
  24. nsarray* abundleurlschemes = [ABundleURLTypes0 objectforkey:@"Cfbundleurlschemes"];
  25. if ([Abundleurlschemes Iskindofclass:[nsarray class]] &&
  26. ([abundleurlschemes Count] > 0)) {
  27. NSString *scheme = [Abundleurlschemes objectatindex:0];
  28. if ([Scheme iskindofclass:[nsstring class]] &&
  29. [url Hasprefix:scheme]) {
  30. Bschemeinplist = YES;
  31. }
  32. }
  33. }
  34. }
  35. //Check If the authorization callback would work
  36. BOOL Bcanopenurl = [[uiapplication sharedapplication] Canopenurl:[nsurl Urlwithstring:url]];
  37. if (!bschemeinplist | |!bcanopenurl) {
  38. Uialertview *alertview = [[Uialertview alloc]
  39. initwithtitle:@"Setup Error"
  40. message:@"Invalid or missing URL scheme. You cannot run the app until your set up a valid URL scheme in your. plist. "
  41. delegate:self
  42. cancelbuttontitle:@"OK"
  43. Otherbuttontitles:nil,
  44. NIL];
  45. [Alertview show];
  46. [Alertview release];
  47. }
  48. }

There is also a section of code that launches other applications:

[CSharp] View plaincopy
  1. -(Ibaction) openmaps {//Open map
  2. //Where is Apple on the map anyway?
  3. nsstring* Addresstext = @ "1 Infinite Loop, Cupertino, CA 95014″;
  4. //URL encode the spaces
  5. Addresstext = [Addresstext stringbyaddingpercentescapesusingencoding:nsasciistringencoding];
  6. nsstring* Urltext = [NSString stringwithformat:@"http://maps.google.com/maps?q=%@", Addresstext];
  7. //lets throw this text on the log so we can view the URL in the the event we had an issue
  8. NSLog (Urltext);
  9. [[UIApplication sharedapplication] Openurl:[nsurl Urlwithstring:urltext]];
  10. }
  11. -(Ibaction) Openemail {//open mail
  12. //Fire off a email to Apple support
  13. [[UIApplication sharedapplication] openurl:[nsurl urlwithstring:@"Mailto://[email protected]"];
  14. }
  15. -(Ibaction) Openphone {// Call
  16. //Call Google 411
  17. [[UIApplication sharedapplication] openurl:[nsurl urlwithstring:@"tel://8004664411"];
  18. }
  19. -(Ibaction) opensms {//Open SMS
  20. //Text to Google SMS
  21. [[UIApplication sharedapplication] openurl:[nsurl urlwithstring:@"sms://466453"];
  22. }
  23. -(Ibaction) Openbrowser {//Open Browser
  24. //Lanuch any IPhone developers fav site
  25. [[UIApplication sharedapplication] openurl:[nsurl urlwithstring:@"http://itunesconnect.apple.com"];
  26. }

Reference Link: http://blog.csdn.net/arthurchenjs/article/details/6920631

  iOS Development ~ Two apps call each otherCategory: IOS2013-07-01 20:27 273 people read Comments (1) favorite reports

1, new two projects: Appone,apptwo;

2. Add the following in their attribute list, respectively:

Appown:

Apptwo:

3. Implement the Code separately:

Add code in the Appown viewcontroller.m:

-(void) Touchesbegan: (nsset *) touches withevent: (uievent *) event

{

Nsurl *url = [Nsurlurlwithstring:@ "Apptwo:"];

if ([[[UIApplicationsharedapplication] canopenurl: url])

{

NSLog(@ "Canopenurl");

[[UIApplication sharedapplication] openURL: url];

} Else

{

NSLog(@ "Can not OpenURL");

}

}


Add code in the APPTWN viewcontroller.m:

-(void) Touchesbegan: (nsset *) touches withevent: (uievent *) event

{

NSLog(@ "Touchesbegan");

Nsurl *url = [Nsurlurlwithstring:@ "Appone:"];

if ([[[UIApplicationsharedapplication] canopenurl: url])

{

NSLog(@ "Canopenurl");

[[UIApplication sharedapplication] openURL: url];

} Else

{

NSLog(@ "Can not OpenURL");

}

}


4. Run the next two projects, and then either kill one of the project processes or put them into hibernation, you can open another project through another project.

IOS opens another app in one app and its two apps call each other

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.