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
- Nsurl * myurl_app_a = [Nsurl urlwithstring:@"testhello://"];
- if ([[[UIApplication sharedapplication] canopenurl:myurl_app_a]) {
- NSLog (@"Canopenurl");
- [[UIApplication sharedapplication] openurl:myurl_app_a];
- }
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
- -(BOOL) Application: (UIApplication *) application Handleopenurl: (nsurl *) URL
- {
- if (!url) { return NO;}
- NSString *urlstring = [url absolutestring];
- NSLog (@"%@", urlstring);
- //[[nsuserdefaults Standarduserdefaults] setobject:urlstring forkey:@ "url"];
- //[[nsuserdefaults standarduserdefaults] synchronize];
- return YES;
- }
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
- Check if the user is configured with AppID
- There is no cfbundleurlschemes field for accurately configuring info
- Can you open it correctly?
- if (!kappid) {
- Uialertview *alertview = [[Uialertview alloc]
- initwithtitle:@"Setup Error"
- message:@"Missing app ID. You cannot run the app until your provide this in the code. "
- delegate:self
- cancelbuttontitle:@"OK"
- Otherbuttontitles:nil,
- NIL];
- [Alertview show];
- [Alertview release];
- } Else {
- //Now check the URL scheme fb[app_id]://authorize are in the. plist and can
- //Be opened, doing a simple check without the local app ID factored in here
- NSString *url = [NSString stringwithformat:@"Fb%@://authorize", kappid];
- BOOL bschemeinplist = NO; //Find out if the sceme are in the plist file.
- nsarray* abundleurltypes = [[NSBundle mainbundle] objectforinfodictionarykey:@"Cfbundleurltypes"];
- if ([Abundleurltypes Iskindofclass:[nsarray class]] &&
- ([abundleurltypes Count] > 0)) {
- nsdictionary* ABundleURLTypes0 = [Abundleurltypes objectatindex:0];
- if ([ABundleURLTypes0 iskindofclass:[nsdictionary class]]) {
- nsarray* abundleurlschemes = [ABundleURLTypes0 objectforkey:@"Cfbundleurlschemes"];
- if ([Abundleurlschemes Iskindofclass:[nsarray class]] &&
- ([abundleurlschemes Count] > 0)) {
- NSString *scheme = [Abundleurlschemes objectatindex:0];
- if ([Scheme iskindofclass:[nsstring class]] &&
- [url Hasprefix:scheme]) {
- Bschemeinplist = YES;
- }
- }
- }
- }
- //Check If the authorization callback would work
- BOOL Bcanopenurl = [[uiapplication sharedapplication] Canopenurl:[nsurl Urlwithstring:url]];
- if (!bschemeinplist | |!bcanopenurl) {
- Uialertview *alertview = [[Uialertview alloc]
- initwithtitle:@"Setup Error"
- message:@"Invalid or missing URL scheme. You cannot run the app until your set up a valid URL scheme in your. plist. "
- delegate:self
- cancelbuttontitle:@"OK"
- Otherbuttontitles:nil,
- NIL];
- [Alertview show];
- [Alertview release];
- }
- }
There is also a section of code that launches other applications:
[CSharp] View plaincopy
- -(Ibaction) openmaps {//Open map
- //Where is Apple on the map anyway?
- nsstring* Addresstext = @ "1 Infinite Loop, Cupertino, CA 95014″;
- //URL encode the spaces
- Addresstext = [Addresstext stringbyaddingpercentescapesusingencoding:nsasciistringencoding];
- nsstring* Urltext = [NSString stringwithformat:@"http://maps.google.com/maps?q=%@", Addresstext];
- //lets throw this text on the log so we can view the URL in the the event we had an issue
- NSLog (Urltext);
- [[UIApplication sharedapplication] Openurl:[nsurl Urlwithstring:urltext]];
- }
- -(Ibaction) Openemail {//open mail
- //Fire off a email to Apple support
- [[UIApplication sharedapplication] openurl:[nsurl urlwithstring:@"Mailto://[email protected]"];
- }
- -(Ibaction) Openphone {// Call
- //Call Google 411
- [[UIApplication sharedapplication] openurl:[nsurl urlwithstring:@"tel://8004664411"];
- }
- -(Ibaction) opensms {//Open SMS
- //Text to Google SMS
- [[UIApplication sharedapplication] openurl:[nsurl urlwithstring:@"sms://466453"];
- }
- -(Ibaction) Openbrowser {//Open Browser
- //Lanuch any IPhone developers fav site
- [[UIApplication sharedapplication] openurl:[nsurl urlwithstring:@"http://itunesconnect.apple.com"];
- }
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