2015-12-02 111 views
15

我用這些代碼共享應用程序鏈接什麼是應用程序,但沒有什麼是在WhatsApp的文本字段。如果使用簡單文本,那麼它的工作。任何人都可以提出最終結果。分享鏈接使用whatsapp

NSString *theTempMessage = @"whatsapp://send?text=https://itunes.apple.com/in/app/myapp/id1054375332?ls=1&mt=8"; 
NSString *theFinalMessage; 

theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@":" withString:@"%3A"]; 
theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"]; 
theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"]; 
theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"," withString:@"%2C"]; 
theTempMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"]; 
theFinalMessage = [theTempMessage stringByReplacingOccurrencesOfString:@"&" withString:@"%26"]; 

NSString * stringToSend=theFinalMessage; 
NSURL *whatsappURL = [NSURL URLWithString:stringToSend]; 

if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) 

{ 
    [[UIApplication sharedApplication] openURL: whatsappURL]; 
} 
+0

爲什麼你正在使用字符串替換 – satheesh

回答

36

跟隨誤差顯示白名單您的應用希望在Info.plist中查詢LSApplicationQueriesSchemes鍵(字符串數組)下的任何URL方案:

enter image description here

隨着方案包括在Info.plist的一切和以前一樣。當你連接到iOS 9時,你不僅限於50個不同的方案,你只需要在Info.plist中聲明你需要的東西。對於您可以包含多少計劃似乎沒有限制,但如果他們認爲您濫用此機制,我會希望App Store審覈團隊提出問題。

Note that this mechanism only applies to canOpenURL and not openURL. You do not need to have a scheme listed in Info.plist to be able to open it with openURL.

NSString * msg = @"Application%20Name%20https://itunes.apple.com/in/app/myapp/id1054375332?ls=1&mt=8"; 

msg = [msg stringByReplacingOccurrencesOfString:@":" withString:@"%3A"]; 
msg = [msg stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"]; 
msg = [msg stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"]; 
msg = [msg stringByReplacingOccurrencesOfString:@"," withString:@"%2C"]; 
msg = [msg stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"]; 
msg = [msg stringByReplacingOccurrencesOfString:@"&" withString:@"%26"]; 

NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",msg]; 
NSURL * whatsappURL = [NSURL URLWithString:urlWhats]; 

if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) { 
    [[UIApplication sharedApplication] openURL: whatsappURL]; 
} else { 
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
} 

This是應用安全的WWDC 2015年官方視頻。

+0

@CJIOSDeveloper上面的代碼我安裝在我的iPhone'iOS9.1'它完美的工作。 –

+0

感謝所有給予答覆。現在這個工作.........根據上面。 –

+0

這段代碼是唯一適用於我的代碼(Swift 3,Xcode 8.2.1)。我剛剛添加了一個替換行: 'msg = [msg stringByReplacingOccurrencesOfString:@「」withString:@「%20」];' 否則,它會創建一個零值。 – Leandro

1

如果使用 「[[UIApplication的sharedApplication]的OpenURL:whatsappURL];」 後弦replcement它會打開Safari瀏覽器沒有的WhatsApp,

如果你想打開的WhatsApp不檢查canOpenURL

failed for URL: "whatsapp://" - error: This app is not allowed to query for scheme whatsapp

當在iOS中9,你必須替換字符串

2

添加到您的Info.plist

<key>LSApplicationQueriesSchemes</key> 
    <array> 
     <string>whatsapp</string> 
    </array> 

,你需要打開WhatsApp的共享實現此代碼到ViewController。 (例如像說一個按鈕動作) 更新迅速版本3(Xcode的8.x中):更新了棄用

var str = "This is the string which you want to share to WhatsApp" 
str=str.addingPercentEncoding(withAllowedCharacters: (NSCharacterSet.urlQueryAllowed))! 
let whatsappURL = URL(string: "whatsapp://send?text=\(str)") 
if UIApplication.shared.canOpenURL(whatsappURL) { 
    UIApplication.shared.open(whatsappURL!, options: [:], completionHandler: nil) 
} else { 
    showAlert(message: "Whatsapp is not installed on this device. Please install Whatsapp and try again.") 
} 

這裏showAlert()是用於顯示警報的自定義功能。

+2

由於'openURL'在iOS 10中已被棄用,因此您需要使用'openURL'代替: 'UIApplication.shared.open(whatsappURL !, options:[:],completionHandler:nil)' – Felix

+0

謝謝:) nice分享 –

+1

謝謝爲了回答 –