在SDK中開啟其他接入應用的解決方案

來源:互聯網
上載者:User

在SDK中開啟其他接入應用的解決方案一直以來,在iOS的開發中,在程式中開啟另外一個應用是不允許。後來有正義之士用class-dump在私人API中找到了這樣的功能。那就是使用UIApplication的launchApplicationWithIdentifier:suspended:來開啟。使用的辦法如下: NSString *identifier = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];[[UIApplication sharedApplication] launchApplicationWithIdentifier:identifier suspended:NO]; 畢竟是私人API不是一個好的辦法,至少你永遠都得不到App Store的認可。 在某些時候是其實我們可能還是需要這樣的功能。作為一個SDK,其實還是有一種比較好的解決方案的。那就是使用UIApplication的openURL:的方法。 我們先來瞭解一下openURL和實現的方案。OpenURL其實是有很豐富的功能,除了簡單的調用safari開啟網站,還可有google地圖搜尋,Mail,撥打到電話,傳送簡訊,開啟AppStore。 -(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"]];    }那怎樣來製作從一個應用開啟其他應用,這其實很簡單,開啟info.plist,添加一項URL types,展開URL types,再展開Item1,將Item1下的URL identifier修改為URL Scheme,展開URL Scheme,將Item1的內容修改為myapp其他程式可通過myapp://訪問此自訂URL。其實就是類似下面的樣式。  這樣就只要open這個應用的自訂url,系統就可以幫我們找到並開啟這個程式。 NSURL *url = [NSURL URLWithString:@" myapp:"];  [[UIApplication sharedApplication] openURL:url];  作為SDK比普通應用的優勢在於,每一個接入的應用都有一個AppId用於區分,我們就可以充分利用這個AppId來製作。 我們可以要求第三方開發人員需要在他們Info.Plist中配置這樣的欄位,這樣我們就可以在我們的SDK介面中開啟對應AppId的應用,當然,這需要裝置中真的有安裝這個程式。 例如某應用程式指派AppId為111122223333,我們要求其再Info.plist定義URL Schemes為NDSDK111122223333,這樣,我們在內部代碼就可以準確識別是否有這樣的程式。 更有甚者,我們可以通過canOpenURL這個方法來判斷這台裝置是否安裝了這個應用,如果可以開啟,返回YES,那應該是有安裝這樣的程式,不管是ipa還是Pxl的程式,應該都是沒有問題的。 如果我們真的選擇這樣子做,那就需要在文檔中說明清楚。但是需要注意的是,也許作為程式員,可能不是很喜歡看文檔,也許你費盡心思寫的文檔他並沒有看到。這時我們應該來一點強硬的手段,於是有了下面這段代碼的功能。1:檢查使用者是否配置了AppId2:有沒有準確配置Info的CFBundleURLSchemes欄位3:是不是可以正確開啟。    // Check App ID:    // This is really a warning for the developer, this should not    // happen in a completed app    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) {www.2cto.com            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];        }    } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.