gpt4 book ai didi

iphone - 在 iOS 应用程序中通过 WhatsApp 共享图像/文本

转载 作者:IT王子 更新时间:2023-10-29 07:37:27 24 4
gpt4 key购买 nike

是否可以通过 iOS 应用程序中的 Whatsapp 共享图像、文本或任何您想要的内容?我在谷歌上搜索,但我只找到了关于 Android 实现的结果。

最佳答案

现在可以用这种方式:

发送文本 - 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
}

发送文本 - 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
}
}
}

发送图像 - Obj-C

-- 在.h文件中

<UIDocumentInteractionControllerDelegate>

@property (retain) UIDocumentInteractionController * documentInteractionController;

-- 在.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];
}

发送图片 - 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 .plist file:

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

More information about url sheme: https://developer.apple.com/videos/play/wwdc2015-703/

我没有为两者找到单一的解决方案。有关 http://www.whatsapp.com/faq/en/iphone/23559013 的更多信息

我做了一个小项目来帮助一些人。 https://github.com/salesawagner/SharingWhatsApp

关于iphone - 在 iOS 应用程序中通过 WhatsApp 共享图像/文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8354417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com