通過openURL啟動第三方app並傳參數


通過openURL啟動第三方app並傳參數

轉自:http://lifeinbeta.diandian.com/post/2012-05-31/40028982809

=====工程A=====
0. 建立工程A, 先調出URL Types

1.Add Row 一個URL Schemes 並隨便起個名字 (這就是調用這個app的唯一鏈接)

2. 在工程A的AppDelegate.m里加入以下系統方法:
(這個方法會捕獲調用本工程的程序傳遞過來的URL identifier文本)

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL*)url
{
  // 處理傳遞過來的參數
  UIAlertView *alertView;
  NSString*text = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  alertView = [[UIAlertView alloc] initWithTitle:@"Text"
                                         message:text                            
                                        delegate:nil
                               cancelButtonTitle:@"OK"
                               otherButtonTitles:nil];
    [alertView show];
    [alertView release];
    return YES;
}

====工程B=====
3. 建立工程B, 添加調用語句:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"myapp://foo=444bar=222"]];

4. 編譯運行工程B, 結果是:
B啟動->A啟動->A彈出Alert:

另外:
0. 從結果看出app的地址構成是: URL Scheme://URL identifier
1. 單用URL Scheme 即可打開程序, 即URL identifier是可選的
2. myapp://后面的字 可以為點”.”和等號”=” 不可以為空格和問號
3. stackoverflow上有很多人指出這是apple禁止的功能, 所以請謹慎使用, 但如果是客戶問”設備能力”這方面的問題, 那么答案是”可以”, 但 不建議那么做.

參考閱讀:
Launching Your Own Application via a Custom URL Scheme
iOS SDK: Working with URL Schemes


免責聲明!

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



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