IOS 在一個應用裡開啟另一個應用 及其 兩個應用互相調用

來源:互聯網
上載者:User

標籤:manage   eth   itunes   連結   coding   互相調用   操作   cancel   open   

在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互相調用分類: IOS2013-07-01 20:27 273人閱讀 評論(1) 收藏 舉報

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 在一個應用裡開啟另一個應用 及其 兩個應用互相調用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.