在 iOS 应用程序中通过 WhatsApp 共享图像/文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8354417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Share image/text through WhatsApp in an iOS app
提问by Albert Arredondo Alfaro
Is it possible to share images, text or whatever you want through Whatsapp in a iOS app? I'm searching on google but I only found results talking about Android implementations.
是否可以在 iOS 应用程序中通过 Whatsapp 共享图像、文本或任何您想要的内容?我在谷歌上搜索,但我只找到了关于 Android 实现的结果。
采纳答案by rckoenes
No this is not possible, whatsapp does not have any public API you can use.
不,这是不可能的,whatsapp 没有您可以使用的任何公共 API。
Please note that this answer is correct for 2011 when there was no API for WhatsApp.
请注意,当 WhatsApp 没有 API 时,此答案适用于 2011 年。
Now there is an api available for interacting with WhatsApp: http://www.whatsapp.com/faq/en/iphone/23559013
现在有一个可用于与 WhatsApp 交互的 api:http: //www.whatsapp.com/faq/en/iphone/23559013
The Objective-C call to open one of these URLs is as follows:
打开这些 URL 之一的 Objective-C 调用如下:
NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
}
回答by Wagner Sales
Is now possible in this way:
现在可以通过这种方式:
Send Text - Obj-C
发送文本 - Obj-C
NSString * msg = @"YOUR MSG";
NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",msg];
NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
} else {
// Cannot open whatsapp
}
Send Text - Swift
发送文本 - Swift
let msg = "YOUR MSG"
let urlWhats = "whatsapp://send?text=\(msg)"
if let urlString = urlWhats.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.sharedApplication().canOpenURL(whatsappURL) {
UIApplication.sharedApplication().openURL(whatsappURL)
} else {
// Cannot open whatsapp
}
}
}
Send Image - Obj-C
发送图像 - Obj-C
-- in .h file
-- 在 .h 文件中
<UIDocumentInteractionControllerDelegate>
@property (retain) UIDocumentInteractionController * documentInteractionController;
-- in .m file
-- 在 .m 文件中
if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"whatsapp://app"]]){
UIImage * iconImage = [UIImage imageNamed:@"YOUR IMAGE"];
NSString * savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/whatsAppTmp.wai"];
[UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
_documentInteractionController.UTI = @"net.whatsapp.image";
_documentInteractionController.delegate = self;
[_documentInteractionController presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:self.view animated: YES];
} else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"WhatsApp not installed." message:@"Your device has no WhatsApp installed." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
Send Image - Swift
发送图像 - Swift
let urlWhats = "whatsapp://app"
if let urlString = urlWhats.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.sharedApplication().canOpenURL(whatsappURL) {
if let image = UIImage(named: "image") {
if let imageData = UIImageJPEGRepresentation(image, 1.0) {
let tempFile = NSURL(fileURLWithPath: NSHomeDirectory()).URLByAppendingPathComponent("Documents/whatsAppTmp.wai")
do {
try imageData.writeToURL(tempFile, options: .DataWritingAtomic)
self.documentInteractionController = UIDocumentInteractionController(URL: tempFile)
self.documentInteractionController.UTI = "net.whatsapp.image"
self.documentInteractionController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)
} catch {
print(error)
}
}
}
} else {
// Cannot open whatsapp
}
}
}
Because a new security feature of iOS 9, you need add this lines on .plistfile:
<key>LSApplicationQueriesSchemes</key> <array> <string>whatsapp</string> </array>
More information about url sheme: https://developer.apple.com/videos/play/wwdc2015-703/
由于 iOS 9 的新安全功能,您需要在.plist文件中添加以下行 :
<key>LSApplicationQueriesSchemes</key> <array> <string>whatsapp</string> </array>
有关 url sheme 的更多信息:https: //developer.apple.com/videos/play/wwdc2015-703/
I did not find a single solution for both. More information on http://www.whatsapp.com/faq/en/iphone/23559013
我没有为两者找到单一的解决方案。更多信息请访问 http://www.whatsapp.com/faq/en/iphone/23559013
I made a small project to help some. https://github.com/salesawagner/SharingWhatsApp
我做了一个小项目来帮助一些人。 https://github.com/salesawagner/SharingWhatsApp
回答by Adel
It is now possible. Have not tried yet though.
现在可以了。不过还没试过。
The latest release notes for whatsappindicate that you can through the share extension:
WhatsApp accepts the following types of content:
WhatsApp 接受以下类型的内容:
- text (UTI: public.plain-text)
- photos (UTI: public.image)
- videos (UTI: public.movie)
- audio notes and music files (UTI: public.audio)
- PDF documents (UTI: com.adobe.pdf)
- contact cards (UTI: public.vcard)
- web URLs (UTI: public.url)
- 文本(UTI:public.plain-text)
- 照片(UTI:public.image)
- 视频(UTI:public.movie)
- 音频笔记和音乐文件(UTI:public.audio)
- PDF 文档(UTI:com.adobe.pdf)
- 联系卡(UTI:public.vcard)
- 网址(UTI:public.url)
回答by Devyani Shinde
This is the correct code for share link to whats app users.
这是分享链接到 whats app 用户的正确代码。
NSString * url = [NSString stringWithFormat:@"http://video...bla..bla.."];
url = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef) url, NULL,CFSTR("!*'();:@&=+$,/?%#[]"),kCFStringEncodingUTF8));
NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@",url];
NSURL * whatsappURL = [NSURL URLWithString:urlWhats];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
} else {
// can not share with whats app
}
回答by Mehul
Simple code and Sample code ;-)
简单代码和示例代码 ;-)
Note:- You can only share text or image, both sharing together in whatsApp is not working from whatsApp side
注意:-您只能共享文本或图像,两者在 WhatsApp 中共享在 WhatsApp 端不起作用
/*
//Share text
NSString *textToShare = @"Enter your text to be shared";
NSArray *objectsToShare = @[textToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
*/
//Share Image
UIImage * image = [UIImage imageNamed:@"images"];
NSArray *objectsToShare = @[image];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
回答by JoeGalind
Swift 3 version of Wagner Sales' answer:
Swift 3 版本的 Wagner Sales 的回答:
let urlWhats = "whatsapp://app"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) {
if let whatsappURL = URL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL) {
if let image = UIImage(named: "image") {
if let imageData = UIImageJPEGRepresentation(image, 1.0) {
let tempFile = NSURL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai")
do {
try imageData.write(to: tempFile!, options: .atomic)
self.documentIC = UIDocumentInteractionController(url: tempFile!)
self.documentIC.uti = "net.whatsapp.image"
self.documentIC.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
}
catch {
print(error)
}
}
}
} else {
// Cannot open whatsapp
}
}
}
回答by Berni
I added a Whatsapp Sharer to ShareKit.
我在 ShareKit 中添加了一个 Whatsapp Sharer。
Check out here: https://github.com/heringb/ShareKit
回答by Himanshu Garg
回答by Hassan Refaat
Yes it's possible :
是的,这是可能的:
NSMutableArray *arr = [[NSMutableArray alloc]init];
NSURL *URL = [NSURL fileURLWithPath:path];
NSString *textToShare = [NSString stringWithFormat:@"%@ \n",_model.title];
NSString *SchoolName= [[AppUtility sharedUtilityInstance]getAppConfigInfoByKey:@"SchoolName" SecondKeyorNil:Nil];
[arr addObject:textToShare];
[arr addObject:URL];
[arr addObject:_model.body];
[arr addObject:SchoolName];
TTOpenInAppActivity *openInAppActivity = [[TTOpenInAppActivity alloc] initWithView:_parentController.view andRect:((UIButton *)sender).frame];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:arr applicationActivities:@[openInAppActivity]];
// Store reference to superview (UIActionSheet) to allow dismissal
openInAppActivity.superViewController = activityViewController;
// Show UIActivityViewController
[_parentController presentViewController:activityViewController animated:YES completion:NULL];
回答by mourodrigo
Swift 3version for sending text:
用于发送文本的Swift 3版本:
func shareByWhatsapp(msg:String){
let urlWhats = "whatsapp://send?text=\(msg)"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
if let whatsappURL = NSURL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
UIApplication.shared.openURL(whatsappURL as URL)
} else {
let alert = UIAlertController(title: NSLocalizedString("Whatsapp not found", comment: "Error message"),
message: NSLocalizedString("Could not found a installed app 'Whatsapp' to proceed with sharing.", comment: "Error description"),
preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("Ok", comment: "Alert button"), style: UIAlertActionStyle.default, handler:{ (UIAlertAction)in
}))
self.present(alert, animated: true, completion:nil)
// Cannot open whatsapp
}
}
}
}
Also, you need to add whatsapp
to LSApplicationQueriesSchemes
in your Info.plist
此外,您需要添加whatsapp
到LSApplicationQueriesSchemes
您的Info.plist