1. 程式人生 > >IOS 在一個應用裏打開另一個應用 及其 兩個應用互相調用

IOS 在一個應用裏打開另一個應用 及其 兩個應用互相調用

程序 get cocos cat mark fin 屬性列表 RKE forkey

在IOS應用中打開另外一個應用的解決方案

最近要在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
  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. }

自定義處理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
  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. }

其他

基本上至此我們就已經實現一個應用程序中啟動另外一個應用的功能,但是為了是我們的代碼更加強壯,我在網上又找了一段訪問代碼,如下:

[csharp] view plaincopy
  1. // 檢查用戶是否配置了AppId
  2. // 有沒有準確配置Info的CFBundleURLSchemes字段
  3. // 是不是可以正確打開
  4. if (!kAppId) {
  5. UIAlertView *alertView = [[UIAlertView alloc]
  6. initWithTitle:@"Setup Error"
  7. message:@"Missing app ID. You cannot run the app until you 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 that the URL scheme fb[app_id]://authorize is in the .plist and can
  16. // be opened, doing a simple check without local app id factored in here
  17. NSString *url = [NSString stringWithFormat:@"fb%@://authorize",kAppId];
  18. BOOL bSchemeInPlist = NO; // find out if the sceme is 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 will 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 you 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. }

另外還有一段啟動其他應用的代碼:

[csharp] view plaincopy
  1. -(IBAction)openMaps {//打開地圖
  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 event we have an issue
  8. NSLog(urlText);
  9. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
  10. }
  11. -(IBAction)openEmail {//打開mail
  12. // Fire off an email to apple support
  13. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://[email protected]"]];
  14. }
  15. -(IBAction)openPhone {//撥打電話
  16. // Call Google 411
  17. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8004664411"]];
  18. }
  19. -(IBAction)openSms {//打開短信
  20. // Text to Google SMS
  21. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://466453"]];
  22. }
  23. -(IBAction)openBrowser {//打開瀏覽器
  24. // Lanuch any iPhone developers fav site
  25. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunesconnect.apple.com"]];
  26. }

附參考鏈接:http://blog.csdn.net/arthurchenjs/article/details/6920631

IOS開發~兩個App互相調用

分類: IOS

1、新建兩個項目:AppOne,AppTwo;

2、分別在其屬性列表中添加如下:

AppOwn:

技術分享圖片

AppTwo:

技術分享圖片

3、分別實現代碼:

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");

}

}


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、分別運行下兩個項目,然後無論把其中一個項目進程殺掉,或者使其進入休眠,都可以通過另一個項目來打開另一個項目。

IOS 在一個應用裏打開另一個應用 及其 兩個應用互相調用