2016-11-29 40 views
6

我正在與夫特3在iOS 10. sharedInstance()方法引發錯誤,當用戶拒絕權限從系統或帳戶以考慮不配置(例如到控制檯「無法寫入應用獲取錯誤使用系統帳戶進行​​身份驗證「)。錯誤在進入關閉之前在控制檯上顯示。我不會在應用中向用戶顯示此錯誤警惕。這是我的代碼:從Twitter.sharedInstance()夫特3的iOS 10

Twitter.sharedInstance().logIn { (session, error) in 
       if error != nil { 
       // print(error?.localizedDescription ?? " ") 
        return 
       }) 

我得到這個錯誤:

2016-11-29 14:49:09.023 CarReview[1254:31719] [TwitterKit] did encounter error with message "Unable to authenticate using the system account.": Error Domain=TWTRLogInErrorDomain Code=2 "User allowed permission to system accounts but there were none set up." UserInfo={NSLocalizedDescription=User allowed permission to system accounts but there were none set up.} 
2016-11-29 14:49:09.024 CarReview[1254:31719] [TwitterKit] No matching scheme found. 
2016-11-29 14:49:09.292 CarReview[1254:31719] [TwitterKit] did encounter error with message "Error obtaining user auth token.": Error Domain=TWTRLogInErrorDomain Code=-1 "<?xml version="1.0" encoding="UTF-8"?> 
<hash> 
    <error>Desktop applications only support the oauth_callback value 'oob'</error> 
    <request>/oauth/request_token</request> 
</hash> 
" UserInfo={NSLocalizedDescription=<?xml version="1.0" encoding="UTF-8"?> 
<hash> 
    <error>Desktop applications only support the oauth_callback value 'oob'</error> 
    <request>/oauth/request_token</request> 
</hash> 
} 

我想向用戶展示這一點:「無法使用系統帳戶驗證用戶允許的權限系統帳戶,但什麼都沒有設置向上。」

回答

0

我不知道我理解你想要做什麼,但你可能想打印的主線程結果:

Twitter.sharedInstance().logIn{(session, error) in 
    DispatchQueue.main.async{ 
     if error != nil { 
      print("Failed to login with Twitter/error:", error!.localizedDescription) 
     }else{ 
      print("succeeded") 
     } 
    } 
} 
+0

是不是我的意思。在關閉呼叫之前在控制檯上顯示錯誤,我對此沒有影響。我不會向用戶顯示這個錯誤。控制檯上的錯誤顯示了它自己。 –

+0

如果你不希望它在控制檯關閉前被印刷出來,您需要在我的答案添加DispatchQueue.main.async等。 –

+0

儘管我沒有控制錯誤,但是控制檯出錯了,但它不是我的打印功能。 –

0

OK,我使用此代碼來通知用戶的一些錯誤:

if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter) { 
      if ACAccountStore().accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter).accessGranted { 

       Twitter.sharedInstance().logIn{ 
        (session, error) in 
        if error != nil { 
         self.showAlert(title: "Twitter - Error", message: (error?.localizedDescription)!) 
         return 
        } 

        guard let token = session?.authToken else { return } 
        guard let secret = session?.authTokenSecret else { return } 

        let credential = FIRTwitterAuthProvider.credential(withToken: token, secret: secret) 
        FIRAuth.auth()?.signIn(with: credential, completion: { (user, error) in 
         if error != nil { 
          self.showAlert(title: "Firebase (Twitter)", message: (error?.localizedDescription)!) 
          return 
         } 

         self.showAlert(title: "Firebase (Twitter)", message: "Logged to Firebase via Twitter.") 
        }) 
       } 
      } else { 
       showAlert(title: "Twitter - Error", message: "Give access to the system Twitter account.") 
      } 
     } else { 
      showAlert(title: "Twitter - Error", message: "No system accounts set up.") 
     } 

但它不是我想要的:/

0

您需要使用withMethods並使用systemAccounts,而不是基於Web或全部使用iOS Twitter的設置中指定。下面的代碼是在斯威夫特3:

twitSharedInstance.logIn(withMethods: .systemAccounts) { (session :TWTRSession?, error :Error?) in 
     if (error != nil) { 
      if (session != nil) { 
       //We have logged into Twitter. 
      } 
     } 
    } 
0

我會盡力去猜測你的問題其實是:

「我在控制檯的錯誤,但回調不叫,這樣我就可以」 t怎麼辦?

由於您的consumerKeyconsumerSecret未在Info.plist中設置,您會收到這些錯誤。您需要正確install Twitter SDK

這是什麼寫在註釋logIn(completion:)

@warning This method requires that you have set up your consumerKey and consumerSecret .

的Info.plist應該有這樣的事情(注意<key>consumerKey</key><key>consumerSecret</key>字符串):

<key>Fabric</key> <dict> 
    <key>APIKey</key> 
    <string> YOUR API KEY </string> 
    <key>Kits</key> 
    <array> 
     <dict> 
      <key>KitInfo</key> 
      <dict> 
       <key>consumerKey</key> 
       <string> YOU PROBABLY MISSED YOUR CONSUMER KEY </string> 
       <key>consumerSecret</key> 
       <string> YOU PROBABLY MISSED YOUR CONSUMER SECRET </string> 
      </dict> 
      <key>KitName</key> 
      <string>Twitter</string> 
     </dict> 
    </array> 
</dict> 
10

我面臨着同樣的問題,如問題。 我剛纔設置的回調URL到Twitter的應用程序和解決的問題。

轉到https://apps.twitter.com/app - >設置 - >回調URL和更新設置進行保存。

+2

我需要在回撥網址中輸入什麼內容? – user3033437

+1

您可以添加的任何有效URL。 –