本文來自百度:
http://zhidao.baidu.com/link?url=BRIvWtquPb9cDh33Khhowx8dp2VLuYs20wifJcwYJaXSv0vfYYrFF8U5elkqAoEolqDZ5mWWJ0IBO7Fq6Zoj9d-cGaxgpJHzZqe5IrjEftG&qq-pf-to=pcqq.discussion
最近要在IOS中實現一個應用啟動另外一個應用的功能,搜了一些資料,使用UIApplication的openURL:的方法就能實現,現在整理和大家分享一下!
注冊自定義URL協議
首先被啟動的應用需要向iPhone注冊一個自定義URL協議。這是在你的項目文件夾的info.plist文件進行的(就是你改變應用程序圖標的同一個文件)。
Step1. 右鍵,選擇“Add Row”Step2. Key值選擇“URL types”
Step3. 打開“Item 0″,然后為該key增加一個URL identifier。可以是任何值,但建議用“反域名”(例如 “com.fcplayer.testHello”)。
Step4. 在“Item 0”下再加一行。
Step5. 選擇“URL Schemes” 作為Key。
Step6. 輸入你的URL協議名 (例如“testHello://” 應寫做“testHello”)。如果有必要,你可以在這里加入多個協議。
操作截圖如下:
訪問自定義URL
在主應用程序中通過訪問自定義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];
}
自定義處理URL
有些時候我們除了啟動還需向另外一個應用發送參數,這是也可以通過自定義的URL來實現,如:
testHello://
testHello://com.fcplayer.testHello
testHello://config=1&abar=2
這時我們在被啟動應用中就必須進行自定義處理,在delegate中實現該消息(Cocos2d加在AppDelegate中),例如:
- (BOOL)application:(UIApplication *)applicationhandleOpenURL:(NSURL*)url { // Do something withthe url here }
通常,我們會從參數中解析出URL以便在視圖中顯示或者存儲到UserPreference。下面的例子把URL存儲為User Preference的url變量中或者打印出來:
[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;
}
其他
基本上至此我們就已經實現一個應用程序中啟動另外一個應用的功能,但是為了是我們的代碼更加強壯,我在網上又找了一段訪問代碼,如下:
[csharp] view plaincopy
// 檢查用戶是否配置了AppId
// 有沒有准確配置Info的CFBundleURLSchemes字段
// 是不是可以正確打開
if (!kAppId) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Setup Error"
message:@"Missing app ID. You cannot run the app until you provide this in the code."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];
[alertView show];
[alertView release];
} else {
// Now check that the URL scheme fb[app_id]://authorize is in the .plist and can
// be opened, doing a simple check without local app id factored in here
NSString *url = [NSString stringWithFormat:@"fb%@://authorize",kAppId];
BOOL bSchemeInPlist = NO; // find out if the sceme is 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 will 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 you set up a valid URL scheme in your .plist."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];
[alertView show];
[alertView release];
}
}
另外還有一段啟動其他應用的代碼:
[csharp] view plaincopy
-(IBAction)openMaps {//打開地圖
// 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 event we have an issue
NSLog(urlText);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
}
-(IBAction)openEmail {//打開mail
// Fire off an email to apple support
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];
}
-(IBAction)openPhone {//撥打電話
// Call Google 411
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8004664411"]];
}
-(IBAction)openSms {//打開短信
// Text to Google SMS
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://466453"]];
}
-(IBAction)openBrowser {//打開
瀏覽器
// Lanuch any iPhone developers fav site
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"
http://itunesconnect.apple.com"]];
}