2017-10-06 62 views
0

我想從app_A打開一個iOS app_B,然後想要將app_B的數據放回到app_A中,在下面嘗試打開whatsapp,但沒有奏效。請幫助如何在iOS中實現這一點,在此先感謝。如何在Swift中點擊按鈕打開Whatsapp或其他iOS應用程序?

@IBAction func clickMe(_ sender: UIButton) { 

     let url = "https://api.whatsapp.com/send?00919599****** " 
     let whatUpUrl = NSURL(string: url) 
     if UIApplication.shared.canOpenURL(whatUpUrl! as URL){ 
      UIApplication.shared.openURL(whatUpUrl! as URL) 
     } else { 
      //redirect to safari because the user doesn't have Whatsapp installed 
      UIApplication.shared.openURL(NSURL(string: "http://whatsapp.com/")! as URL) 
     } 
    } 

回答

1

WEBURL鏈接打開聊天

let whatsappURL = URL(string: "https://api.whatsapp.com/send?phone=9512347895&text=Invitation") 
    if UIApplication.shared.canOpenURL(whatsappURL!) { 
     UIApplication.shared.openURL(whatsappURL!) 
    } 

注:在info.plist中

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

添加URL方案添加這Info.plist中

enter image description here

然後在你想打開whatsapp的按鈕上使用這個。

@IBAction func whatsappButtonPressed(_ sender: Any) { 
     var str = "Hello to whatsapp" 
     str = str.addingPercentEncoding(withAllowedCharacters: (NSCharacterSet.urlQueryAllowed))! 
     let whatsappURL = NSURL(string: "whatsapp://send?text=\(str)") 

     if UIApplication.shared.canOpenURL(whatsappURL! as URL) { 
      UIApplication.shared.openURL(whatsappURL! as URL) 
     } else { 
      showAlert(message: "Whatsapp is not installed on this device. Please install Whatsapp and try again.") 
     } 
} 

同樣,對於其他應用: 替換 - NSURL(字符串: 「WhatsApp的://發送文本=(STR)?」)與 NSURL(字符串: 「APPNAME://」)。 您還需要將該應用程序添加到Info.plist,如上所述。

謝謝!

+1

@Sudhir&Ankit,謝謝,夥計們,有怎麼了打開的,但我也需要從第二個應用程序回撥我的第一個應用程序,你可以請建議 – equanimousFly

+0

請你詳細說明一下嗎? –

+0

@ Ankit:我想要app_B的callBack響應數據不需要它是wats up。例如當我從app_A打開app_B時,app_B執行一些任務,然後將響應返回給app_A。 – equanimousFly

相關問題