2015-05-02 121 views
9

我正在使用UIActivityViewController来共享图像。在WhatsApp最近更改允许共享后,我可以在共享选项中看到WhatsApp。我正在分享图片和消息,我可以看到短信,但我无法分享图片。相同的代码适用于Viber,FB,微博等,不知道我缺少什么WhatsApp。Swift UIActivityViewController图像共享不起作用

func shareImage() { 
    var messageStr:String = "Check out my awesome photo!" 
    var img: UIImage = currentPhoto! 

    var shareItems:Array = [img, messageStr] 

    let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil) 
    activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo] 

    self.presentViewController(activityViewController, animated: true, completion: nil) 

} 
+0

WhatsApp FAQ http://www.whatsapp.com/faq/en/iphone/23559013 –

+0

我希望这些是早期的技术,我想使用上周推出的iOS 8共享扩展WhatsApp。我的问题是我能分享messageStr但不是UIImage img。 –

+1

只有'shareItems'数组只包含'img'时,它似乎才能共享图像。所以我认为这取决于WhatsApp。 – Satachito

回答

5

要共享文本||图片试试这个Swift

func share(shareText shareText:String?,shareImage:UIImage?){ 

    var objectsToShare = [AnyObject]() 

    if let shareTextObj = shareText{ 
     objectsToShare.append(shareTextObj) 
    } 

    if let shareImageObj = shareImage{ 
     objectsToShare.append(shareImageObj) 
    } 

    if shareText != nil || shareImage != nil{ 
     let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) 
     activityViewController.popoverPresentationController?.sourceView = self.view 

     presentViewController(activityViewController, animated: true, completion: nil) 
    }else{ 
     print("There is nothing to share") 
    } 
} 

要共享只需要调用是这样的:

let imageToShare = UIImage(named: "myImage") 
share(shareText: "Sharing this text", shareImage: imageToShare) 
+2

建议:更改'shareText!= nil || shareImage!= nil'到'objectsToShare.count> 0' –

+0

Zaid Pathan ---不适合我的只有共享文本或共享图像不是两个...我也试过facebook&whatsapp太 –

+0

@bLacKhoLE 你能检查'shareText','shareImage'不是零? –

17

似乎只有当数组包含图像而不是图像和文本的组合时,WhatsApp才会共享图像。

func shareImage() { 
    //var messageStr:String = "Check out my awesome iPicSafe photo!" 
    var img: UIImage = currentPhoto! 
    //var shareItems:Array = [img, messageStr] 
    var shareItems:Array = [img] 
    let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil) 
    activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo] 
    self.presentViewController(activityViewController, animated: true, completion: nil) 
} 
+0

有没有添加两者的方法? – Amulya

+0

不。这就是它现在与whatsApp协同工作的方式。他们可能会在未来改变它。 –

+0

我想分享我的应用程序的徽标,但图像增加,有时也会减少。分享facebook/whatsapp/twitter/mail时,图像是否有任何尺寸? –

0

在斯威夫特3使用此代码

@IBAction func whatsappShareWithImages(_ sender: AnyObject) 
    { 

     let urlWhats = "whatsapp://app" 
     if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) { 
      if let whatsappURL = URL(string: urlString) { 

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

        if let image = UIImage(named: "whatsappIcon") { 
         if let imageData = UIImageJPEGRepresentation(image, 1.0) { 
          let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai") 
          do { 
           try imageData.write(to: tempFile, options: .atomic) 
           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 
       } 
      } 
     } 

    } 

在您的应用程序 “的plist”

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

添加该代码您也可以参考小应用程序以供参考:https://github.com/nithinbemitk/iOS-Whatsapp-Share

相关问题