2015-12-02 19 views
15

Ho usato questi codici per il collegamento dell'app condivisa in cosa è l'app, ma nulla è arrivato nel campo di testo di whatsapp. Se si usa un testo semplice, allora il suo lavoro. Qualcuno può suggerire il risultato finale.Condividi collegamento tramite 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

perché stai usando la sostituzione della stringa – satheesh

risposta

36

A seguito di errore mostra al momento del check canOpenURL

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

in iOS 9 è necessario whitelist eventuali schemi URL tua App vuole interrogare in Info.plist sotto le LSApplicationQueriesSchemes chiave (un array di stringhe):

enter image description here

con gli schemi inclusi in Info.plist tutto funziona come prima. Quando ti colleghi a iOS 9 non sei limitato a 50 schemi distinti, devi solo dichiarare ciò che ti serve in Info.plist. Non sembra esserci alcun limite per il numero di schemi che è possibile includere, ma mi aspetterei domande dal team di revisione di App Store se pensano che si stia abusando del meccanismo.

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 è il video ufficiale del WWDC 2015 per la sicurezza app.

+0

@ CJIOSDeveloper Sopra il codice che ho installato nel mio iPhone con 'iOS9.1' funziona perfettamente. –

+0

Grazie a tutti per aver risposto. Ora funziona ......... come sopra. –

+0

Questo codice è stato l'unico che ha funzionato per me (Swift 3, Xcode 8.2.1). Ho appena aggiunto una riga di sostituzione: 'msg = [msg stringByReplacingOccurrencesOfString: @" "withString: @"% 20 "];' In caso contrario, creerebbe un valore nullo. – Leandro

1

Se si utilizza "[[UIApplication sharedApplication] openURL: whatsappURL];" dopo stringa replcement si aprirà il browser Safari non il whatsapp,

Se si desidera aprire WhatsApp non lo fanno sostituire la stringa

2

Aggiungi questo al vostro Info.plist

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

implementare questo codice al ViewController in cui è necessario aprire WhatsApp per la condivisione. (Come ad esempio dire un'azione del pulsante) Aggiornamento per SWIFT versione 3 (Xcode 8.x): Aggiornamento per deprecati:

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

Qui showAlert() è una funzione personalizzata per mostrare un avviso.

+2

Poiché 'openURL' è stato dichiarato obsoleto in iOS 10 È necessario utilizzare' openURL' invece: 'UIApplication.shared.open (whatsappURL !, opzioni: [:], completionHandler: nil)' – Felix

+0

Grazie :) bella condivisione –

+1

Grazie per risposta –