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

谢谢为了回答 –