【问题标题】:How can I share text and images through whatsapp? [duplicate]如何通过whatsapp分享文字和图片? [复制]
【发布时间】:2016-07-24 22:28:34
【问题描述】:

如何在 iOS 中通过 whatsapp 分享文字和图片?

【问题讨论】:

    标签: ios iphone xcode whatsapp


    【解决方案1】:

    不,这是不可能的,whatsapp 没有任何你可以使用的公共 API。

    请注意,这个答案在 2011 年是正确的,当时没有 WhatsApp 的 API。

    现在有一个 api 可用于与 WhatsApp 交互:http://www.whatsapp.com/faq/en/iphone/23559013

    但如果你想试试这个 现在可以通过这种方式:

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

    发送图片 - 斯威夫特

    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
            }
        }
    }
    
    由于 iOS 9 的新安全功能,您需要在 .plist 文件中添加以下行:
    <key>LSApplicationQueriesSchemes</key>
     <array>
         <string>whatsapp</string>
     </array>
    

    更多关于 url sheme 的信息:https://developer.apple.com/videos/play/wwdc2015-703/

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

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

    【讨论】:

    • 我们可以在whatsapp中同时分享图片和文字?
    • 非常感谢。
    猜你喜欢
    • 2013-03-07
    • 2021-11-12
    • 2014-05-29
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多