2011-12-19 48 views
11

在我的應用程序中,如果用戶提供了他們的gmail帳戶,那麼我需要使用gmail登錄憑證打開郵件客戶端,當我們通過編程方式選擇郵件的gmail選項時,存儲在郵件中,然後我需要將用戶直接重定向到他們的帳戶。任何人都可以給我一個我可以如何以編程方式實現這一點的一瞥。以編程方式打開iPhone的iPhone郵件客戶端

回答

38

由於iPhone上的所有應用程序都已經過沙盒處理,因此無法控制郵件應用程序,從而防止它們與Apple應用程序混淆。

你可以做(​​如果你想打開郵件客戶端發送郵件)的唯一的事情,是這樣的:

/* create mail subject */ 
NSString *subject = [NSString stringWithFormat:@"Subject"]; 

/* define email address */ 
NSString *mail = [NSString stringWithFormat:@"[email protected]"]; 

/* define allowed character set */ 
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet]; 

/* create the URL */ 
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"mailto:?to=%@&subject=%@", 
                         [mail stringByAddingPercentEncodingWithAllowedCharacters:set], 
                         [subject stringByAddingPercentEncodingWithAllowedCharacters:set]]];  
/* load the URL */ 
[[UIApplication sharedApplication] openURL:url]; 

/* release the URL. If you are using ARC, remove this line. */ 
[url release]; 
+0

好的,謝謝你的回覆。 – user574089 2011-12-19 07:24:45

2

斯威夫特:

 if let url = NSURL(string: "mailto://\(email)") { 
      UIApplication.sharedApplication().openURL(url) 
     } 
2

萊昂斯威夫特版本羅登堡的回答:

// define email address 
    let address = "[email protected]" 

    // create mail subject 
    let subject = "Subject" 

    // create the URL 
    let url = NSURL(string: "mailto:?to=\(address)&subject=\(subject)".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!) 

    // load the URL 
    UIApplication.sharedApplication().openURL(url!) 
0

我會建議一個更加改進的答案。 Slack.com移動應用程序執行此操作,它會檢測設備上列出的常見電子郵件客戶端,並顯示您想要打開的「哪個」電子郵件客戶端的彈出選擇器。

所以要實現:

  1. 谷歌四處找前10名的電子郵件客戶端(如郵件,谷歌的收件箱,OUTLOOK,航空郵件等)。

  2. 通過搜索所有應用程序,獲取手機上安裝的應用程序列表(但我被告知現在只能查找是否明確安裝了應用程序,因此您需要檢測應用程序)。

  3. 顯示一個彈出列表,如果檢測到超過1個電子郵件應用程序,請求他們打開'哪個'應用程序,例如。郵件,收件箱。

這是我見過最好的解決方案。

相關問題