Share image/text through WhatsApp in an iOS app

2019-01-02 15:09发布

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.

Thanks in advance!

标签: iphone ios
13条回答
还给你的自由
2楼-- · 2019-01-02 15:28

I added a Whatsapp Sharer to ShareKit.

Check out here: https://github.com/heringb/ShareKit

查看更多
牵手、夕阳
3楼-- · 2019-01-02 15:29

This is the correct code for share link to whats app users.

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
}
查看更多
步步皆殇っ
4楼-- · 2019-01-02 15:29

Swift 4

let urlWhats = "whatsapp://app"
        if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlHostAllowed) {

            if let whatsappURL = URL(string: urlString) {

                if UIApplication.shared.canOpenURL(whatsappURL) {

                        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.documentController = UIDocumentInteractionController(url: tempFile)
                                self.documentController.uti = "net.whatsapp.image"
                                self.documentController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
                            } catch {
                                print(error)
                            }
                        }

                } else {
                    let ac = UIAlertController(title: "MessageAletTitleText".localized, message: "AppNotFoundToShare".localized, preferredStyle: .alert)
                    ac.addAction(UIAlertAction(title: "OKButtonText".localized, style: .default))
                    present(ac, animated: true)

                    print("Whatsapp isn't installed ")

                    // Cannot open whatsapp
                }
            }
        }
查看更多
无与为乐者.
5楼-- · 2019-01-02 15:36

Swift 3 version for sending text:

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

查看更多
千与千寻千般痛.
6楼-- · 2019-01-02 15:37

It is now possible. Have not tried yet though.

The latest release notes for whatsapp indicate that you can through the share extension:

WhatsApp accepts the following types of content:

  • 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)
查看更多
泪湿衣
7楼-- · 2019-01-02 15:38

For Swift 4 - Works fine

delclare

var documentInteractionController:UIDocumentInteractionController!

func sharePicture() {
    let urlWhats = "whatsapp://app"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
        if let whatsappURL = NSURL(string: urlString) {

            if UIApplication.shared.canOpenURL(whatsappURL as URL) {

                let imgURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
                let fileName = "yourImageName.jpg"
                let fileURL = imgURL.appendingPathComponent(fileName)
                if let image = UIImage(contentsOfFile: fileURL.path) {
                    if let imageData = image.jpegData(compressionQuality: 0.75) {
                        let tempFile = NSURL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/yourImageName.jpg")
                        do {
                            try imageData.write(to: tempFile!, options: .atomicWrite)
                            self.documentInteractionController = UIDocumentInteractionController(url: tempFile!)
                            self.documentInteractionController.uti = "net.whatsapp.image"
                            self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
                        } catch {
                            print(error)
                        }
                    }
                }
            } else {
                // Cannot open whatsapp
            }
        }
    }
}

Do not forget to edit the .plist with the following lines

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

Enjoy!!!

查看更多
登录 后发表回答