This article from Baidu:
Http://zhidao.baidu.com/link?url= Brivwtqupb9cdh33khhowx8dp2vluys20wifjcwyjaxsv0vfyyrff8u5elkqaoeolqdz5mwwj0ibo7fq6zoj9d-cgaxgpjhzzqe5irjeftg &qq-pf-to=pcqq.discussion
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"];
}
How to implement an app that opens iOS via a URL hyperlink