如何避免每次使用Swift中的Twitter Kit进行授权

时间:2018-02-06 09:48:05

标签: ios swift xcode twitter

我用Twitter Kit实现了Twitter分享应用程序。但是每次点击“推文”按钮需要授权打开Safari。 每次都会出现屏幕(图像下方)。

我想制作不需要授权的Twitter分享,至少只需在应用中授权一次。 (我看到应用程序在应用程序中不需要任何授权,即使我是第一次玩它。这是我的理想。)

我可以避免这种授权吗?或减少授权数量?

Authorization screen that appears when you click Tweet button each time

代码:

AppDelegate.swift

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

   if TWTRTwitter.sharedInstance().application(app, open: url, options: options) {
       return true
   }
   // Your other open URL handlers follow […]
   return true
}

TweetView.swift

let tweetDescription:String = "Succeeded tweet"
let shareImage = UIImage(named:"Twitter_shareImage")

let composer = TWTRComposer()
TWTRTwitter.sharedInstance().logIn(completion: { (session, error) in
     if let sess = session {
//       print("signed in as \(sess.userName)");
     } else {
           print("error: \(String(describing: error?.localizedDescription))");
     }
})

if (TWTRTwitter.sharedInstance().sessionStore.hasLoggedInUsers()) {

    composer.setText(tweetDescription)
    composer.setImage(shareImage)
    composer.show(from: parentViewController()!, completion: nil)

} else {
     // Log in, and then check again
     TWTRTwitter.sharedInstance().logIn { session, error in

         if session != nil { // Log in succeeded
              composer.setText(tweetDescription)
              composer.setImage(shareImage)
              composer.show(from: self.parentViewController()!, completion: nil)
         } else {
              let alert = UIAlertController(title: "No Twitter Accounts Available", message: "You must log in before presenting a composer.", preferredStyle: .alert)
              let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler:{
                        (action: UIAlertAction!) -> Void in
              })
              alert.addAction(cancelAction)
              self.parentViewController()?.present(alert, animated: false, completion: nil)
        }
    }
}

0 个答案:

没有答案