2015-12-02 13 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

を置き換える次のエラーが表示さあなたのアプリケーションはLSApplicationQueriesSchemesキー(文字列の配列)の下でのInfo.plistに照会したい任意のURLスキームをホワイトリスト:スキームと

enter image description here

はInfo.plistのすべてに含ま前と同じように動作します。 iOS 9とリンクする場合、Info.plistで必要なものを宣言するだけで50種類のスキームに制限されるわけではありません。あなたが含めることができるスキームの数に制限はないようですが、あなたがあなたがそのメカニズムを悪用していると思うなら、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 "];' それ以外の場合は、nil値を作成します。 – Leandro

1

あなたが使用している場合は、 "[[のUIApplication sharedApplication]のOpenURL:whatsappURL];" 文字列は、それはSafariブラウザではないのWhatsAppを開きますreplcement後、

そうしないのWhatsApp開く場合iOSの9あなたがする必要があります。canOpenURL

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

をチェックする際に、文字列

2

は、あなたが共有するためのWhatsAppを開く必要のViewControllerにこのコードを実装あなたのInfo.plistに

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

をこれを追加します。 (例えばのようなボタンアクションを言う)SWIFT 3バージョン(Xcodeの8.x)のための 更新:は非推奨のための更新:ここでshowAlert(

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.") 
} 

)が警告を示すためのカスタム関数です。

+2

openURL'はiOSの10で廃止されました 'ので、あなたが代わりにopenURL''使用する必要があります。 'UIApplication.shared.open(whatsappURL !,オプション:[:]、completionHandler:ゼロ)' – Felix

+0

感謝:)素敵なシェア –

+1

感謝答えのために –

関連する問題