在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 plain

copy

  1. 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.testHellotestHello://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  
  • {  
  •     if

 (!url) {  

return

 NO; }  

  •     NSString *URLString = [url absoluteString];  
  •     NSLog(@"%@"

,URLString);  

  •     //[[NSUserDefaults standardUserDefaults] setObject:URLString forKey:@"url"];

  

  •     //[[NSUserDefaults standardUserDefaults] synchronize];

  

  •     return

 YES;  

  • }  

其他

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

[csharp]

 view plaincopy

  1. // 檢查用戶是否配置了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

  1. -(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"

]];  

  • }  

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

  

IOS開發~兩個App互相調用

分類: IOS

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM