在 google+ IOS Swift 中分享

Sharing in google+ IOS Swift

我想通过我的应用在 Google+ 中分享帖子,我已完成本教程中的所有操作: https://developers.google.com/+/mobile/ios/share#adding_basic_sharing

超过 1 次 ...

问题是:

@IBAction func postToGoogle(sender: UIButton) {

    var shareDialog : GPPShareBuilder = GPPShare().shareDialog()

    shareDialog.setPrefillText("Check This out")

    shareDialog.setURLToShare(NSURL(string: "https://developers.ggoe.com/+/"))

    shareDialog.setCallToActionButtonWithLabel("BookMark", URL: NSURL(string: "https://developers.google.com/+/mobile/ios/"),deepLinkID: "")

    shareDialog.open()

}

当按下 postToGoogle 总是在控制台中得到这个:

'You must specify |clientID| for |GPPSignIn|'

但在我看来DidLoad:

 signIn = GPPSignIn.sharedInstance()
    signIn?.shouldFetchGoogleUserEmail = true
    signIn?.shouldFetchGoogleUserID = true
    signIn?.shouldFetchGooglePlusUser = true

    signIn?.clientID = "89239876213876321yuwqiuhwq.apps.googleusercontent.com"
    signIn?.scopes = [kGTLAuthScopePlusLogin]
    signIn?.delegate = self

如您所见,我已经分配了客户端 ID。即使我登录了,一切正常。

ps:这个客户端id是假的。

请帮我解决这个错误。 提前致谢 广告

所以我发现了问题:

在 vewDidLoad 中添加:

gppShare = GPPShare.sharedInstance()

将 de 按钮操作更改为:

 var shareDialog = gppShare?.shareDialog()

    shareDialog?.setPrefillText("Check This out")

    shareDialog?.setURLToShare(NSURL(string: "https://developers.google.com/+/"))

    shareDialog?.setCallToActionButtonWithLabel("BookMark", URL: NSURL(string: "https://developers.google.com/+/mobile/ios/"),deepLinkID: "")

    shareDialog?.open()

正在调用共享对话框。但它仍然缺少深 link.

以下代码适用于我:

@IBAction func shareClicked(sender: AnyObject) {
  var shareDialog = GPPShare.sharedInstance().nativeShareDialog();

  // This line will fill out the title, description, and thumbnail from
  // the URL that you are sharing and includes a link to that URL.
  shareDialog.setURLToShare(NSURL(fileURLWithPath: kShareURL));

  shareDialog.open();
}

Full project sources on GitHub.

我没有使用 Google Plus SDK 就可以工作:

@IBAction func googlePlusShare(sender: AnyObject) {

    let urlstring = "https://developers.google.com/+/mobile/ios/share/basic-share"

    let shareURL = NSURL(string: urlstring)

    let urlComponents = NSURLComponents(string: "https://plus.google.com/share")

    urlComponents!.queryItems = [NSURLQueryItem(name: "url", value: shareURL!.absoluteString)]

    let url = urlComponents!.URL!

    if #available(iOS 9.0, *) {
        let svc = SFSafariViewController(URL: url)
        svc.delegate = self
        self.presentViewController(svc, animated: true, completion: nil)
    } else {
        debugPrint("Not available")
    }
}

// MARK: - SFSafariViewControllerDelegate

@available(iOS 9.0, *)

func safariViewControllerDidFinish(controller: SFSafariViewController)
{

    controller.dismissViewControllerAnimated(true, completion: nil)

}

第一次导入: 导入 SafariServices

N.B:此功能可从 iOS 9.0 开始使用,无需 Google SDK。