2017-04-14 83 views
2

我需要分享一些链接的Facebook和Twitter应用程序,如果安装在设备上的按钮tap.I可以共享相同使用WhatsApp(代码如下)。我想知道我是否也可以在Facebook和Twitter应用程序中做同样的事情。如何分享Facebook和Twitter应用程序在Swift 3没有SDK

@IBAction func whatsappbtn(_ sender: UIButton) { 

    var str = "This is the string which you want to share to WhatsApp" 
    str=str.addingPercentEncoding(withAllowedCharacters: (NSCharacterSet.urlQueryAllowed))! 
    let whatsappURL = URL(string: "whatsapp://send?text=\(str)") 
    if UIApplication.shared.canOpenURL(whatsappURL!) { 
     if #available(iOS 10.0, *) { 
      UIApplication.shared.open(whatsappURL!, options: [:], completionHandler: nil) 
     } else { 
      // Fallback on earlier versions 
     } 
    } else { 

     self.delegate?.alerting(msg: "Please install Whatsapp and try again.") 

    } 

} 

回答

5

如果你不想使用FB/Twitter SDK。那么你可以尝试使用活动控制器并从你的应用程序共享。在这里你会得到每一个可能的份额选项。

var activityViewController:UIActivityViewController? 
textField .text = "Some Test" 

@IBAction func shareText(sender: UIButton) { 
    let activityViewController = UIActivityViewController(
     activityItems: [textField.text as NSString], 
     applicationActivities: nil) 

    presentViewController(activityViewController, animated: true, completion: nil) 
} 

enter image description here

1

首先,你需要从Facebook的开发者网站的FBSDK。

然后,你可以做这样的事情:

func shareToFacebook() { 
     let inviteDialog = FBSDKAppInviteDialog() 

     if inviteDialog.canShow() { 

      guard let appLinkURL = URL(string: "YOUR LINK") else { return } 
      guard let previewImageURL = URL(string: "YOUR LINK IMAGE") else { return } 

      let inviteContent = FBSDKAppInviteContent() 
      inviteContent.appLinkURL = appLinkUrl 
      inviteContent.appInvitePreviewImageURL = previewImageURL 

      inviteDialog.content = inviteContent 
      inviteDialog.delegate = self 
      inviteDialog.show() 
     } 
    } 

不要忘记设置委托方法:

func appInviteDialog(_ appInviteDialog: FBSDKAppInviteDialog!, didCompleteWithResults results: [AnyHashable : Any]!) { 
     print("Did complete sharing.. ") 
    } 

    func appInviteDialog(_ appInviteDialog: FBSDKAppInviteDialog!, didFailWithError error: Error!) { 
     print("Error tool place in appInviteDialog \(error)") 
    } 

斯威夫特3+ 上述作品让我知道如果它适合你。欢呼声

+1

日Thnx,但我需要没有SDK代码。 –

相关问题