Getting single Tweet with TwitterKit/Fabric authentication failure - ios

I'm trying to retrieve a single tweet and show it in my application via the TWTRTweetView provided in the TwitterKit. I've followed this Fabric guide and ended up with following code.
import UIKit
import TwitterKit
class SingleTweetViewController: UIViewController{
#IBOutlet weak var plainView: UIView!
override func viewDidLoad(){
super.viewDidLoad()
Twitter.sharedInstance().logInGuestWithCompletion { session, error in
if let validSession = session {
Twitter.sharedInstance().APIClient.loadTweetWithID("4831830029115392") { tweet, error in
if let t = tweet {
let tweetView = TWTRTweetView(tweet: tweet)
tweetView.showActionButtons = true
self.plainView.addSubview(tweetView)
} else {
println("Failed to load Tweet: \(error?.localizedDescription)")
}
}
} else {
println("Unable to login as guest: \(error.localizedDescription)")
println(error?.localizedFailureReason)
}
}
}
The code generates these two errors due to authentication failure.
Unable to login as guest: Request failed: forbidden (403)
Optional("Twitter API error : Unable to verify your credentials (code 99)")
It is worth mentioning that the app successfully signs in to Twitter via the login button added following this guide. Does anyone have a clue how this error could be fixed? Am I missing some code here or is the issue related to Fabric?

You have to initialize Fabric before trying to use it which is what you're doing in your example code.
For the initialization, follow instructions from the Fabric documentation site. In essence, you have add the following lines to your app delegate (in addition to importing TwitterKit):
Twitter.sharedInstance().startWithConsumerKey("your_key", consumerSecret: "your_secret")
Fabric.with([Twitter.sharedInstance()])
Then copy and paste you're consumer key and secret from fabric.io. Fabric should automatically generate these for you.

I solved the problem using Fabric applications built in option "View tweet in application" found in the same menu as the "Add login button button" option. The Fabric application then inserts proper authentication keys into the Info.plist file. The code provided by Fabric application seems to do the same thing as the code given on Fabric docs but the result differs depending on which one you use. The two code samples look like this:
Code from Fabric docs:
Twitter.sharedInstance().logInGuestWithCompletion { session, error in
if let validSession = session {
Twitter.sharedInstance().APIClient.loadTweetWithID("20") { tweet, error in
if let t = tweet {
self.tweetView.configureWithTweet(t)
} else {
println("Failed to load Tweet: \(error.localizedDescription)")
}
}
} else {
println("Unable to login as guest: \(error.localizedDescription)")
}
}
Code from Fabric application:
Twitter.sharedInstance().logInGuestWithCompletion { (session, error) in
Twitter.sharedInstance().APIClient.loadTweetWithID("20") { (tweet, error) in
self.view.addSubview(TWTRTweetView(tweet: tweet))
}
}
Running the first code will give an authentication error while the second one will load a tweet without issues. The if-statement if let validSession = session returns a false and an authentication error occurs. I couldn't find what the validSession is exactly but it probably compares two different sets of keys or something similar.

Related

migrating project to comply with google sign in requirements for iOS

I'm trying to get a google sign in button to work with my iOS app, but I'm getting this error here:
Cannot find 'presentingViewController' in scope
This is the code segment:
func handleSignInButton() {
GIDSignIn.sharedInstance.signIn(
with: signInConfig,
presenting: presentingViewController // this is the line with the error,
callback: GIDSignInCallback? = nil) { user, error in
guard let signInUser = user else {
// Inspect error
return
}
// If sign in succeeded, display the app's main content View.
}
}
I've been told I need to migrate the way I am signing in and I found this here:
https://developers.google.com/identity/sign-in/ios/quick-migration-guide
but I'm confused about this part here:
Manually connect GIDSignInButton to a method that calls signInWithConfiguration:presentingViewController:callback: using an IBAction or similar.
Can someone show me how to do this properly? I'm a iOS novice :)
Thanks!

Users can't open CKShare record when they accepted the record and application starts

Working my way thru implementing the CKShare functionality of CloudKit.
I Haven manged to get to the part to share a record (via email for example) and also can confirm the user received the invite. The problem is that when user accepts the record then the app pops up but nothing happens. In order to assist here are some key Elements of the app, and please tell me if i am wrong.
1) The application does not require user to login using their Apple ID
2) I am testing the application via a direct built on two different phones (with seperate Apple IDs) when i Connect the phones to the computer with a Cable (aka not using TestFlight yet).
3) I have checked in the CloudkitDashboard and i can see the record that hah been shared and also see that the recored hah been shared, but instead of seeing the user email I sent the invite I see my email and the fact that the record hah been "accepted"
4) I Haven added the CKSharingSupported key in the Info.plist file.
5) The code in the AppDelegate.swift file am using to accept the CKShare is below. I world like to raw your attention to the fact that the string "User Accepted the Share" never gets printed, which makes me think that this part of the code never runs.
func application(_ application: UIApplication, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {
print("User Accepted the Share")
let acceptShareOperation: CKAcceptSharesOperation = CKAcceptSharesOperation(shareMetadatas: [cloudKitShareMetadata])
acceptShareOperation.qualityOfService = .userInteractive
acceptShareOperation.perShareCompletionBlock = {meta, share,
error in
print("The Record Share was Accepted")
}
acceptShareOperation.acceptSharesCompletionBlock = {
error in
/// Send your user to where they need to go in your app
let viewController: SNPDetailsViewController = self.window?.rootViewController as! SNPDetailsViewController
viewController.fetchShare(cloudKitShareMetadata)
}
CKContainer(identifier:
cloudKitShareMetadata.containerIdentifier).add(acceptShareOperation)
}
Any insight to guide me where I am wrong, will be much appreciated.
Thank you for your time!
Please see this answer for more context: CloudKit CKShare URL Goes Nowhere
But make sure that:
You specify a fallback URL for your CloudKit Container that redirects to your application.
Inside your app in Xcode, you set up a URL scheme so that custom URLs like yourapp:// open your application and the query parameter from the fallback URL gets passed into userDidAcceptCloudKitShareWith.
After weeks of trial and error, research and LUCK I managed to find out the problem. All tutorials and online solutions relate to the below code in the AppDelegate.swift, to accept a CKShare record:
func application(_ application: UIApplication, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {
let acceptShareOperation: CKAcceptSharesOperation = CKAcceptSharesOperation(shareMetadatas: [cloudKitShareMetadata])
acceptShareOperation.qualityOfService = .userInteractive
acceptShareOperation.perShareCompletionBlock = {meta, share,
error in
DispatchQueue.main.async() {
print("The Record Share was Accepted")
}
}
acceptShareOperation.acceptSharesCompletionBlock = {
error in
guard (error == nil) else{
print("Error \(error?.localizedDescription ?? "")")
return
}
let viewController: SNPDetailsViewController = self.window?.rootViewController as! SNPDetailsViewController
viewController.fetchShare(cloudKitShareMetadata)
}
CKContainer(identifier:
cloudKitShareMetadata.containerIdentifier).add(acceptShareOperation)
}
The solution in my case was to add it in the SceneDelegate.swift in the following function:
func windowScene(_ windowScene: UIWindowScene, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {
(add above code)
}
Hope this helps others!

Spotify SessionManager continually failing with error "invalid_grant"

What I'm trying to do:
I am implementing the Spotify SDK into my iOS project. I am successfully receiving access tokens for Spotify's API as I am able to do things like search artists, search songs, and view playlists using said API.
The one thing I am struggling to do is play music with the SDK. I have a button that, upon clicking, I want the following flow to happen:
I request Spotify access by doing the following function and using the following Session Manager:
let SpotifyClientID = "###"
let SpotifyRedirectURL = URL(string: "bandmate://")!
lazy var configuration = SPTConfiguration(
clientID: SpotifyClientID,
redirectURL: SpotifyRedirectURL
)
lazy var sessionManager: SPTSessionManager = {
if let tokenSwapURL = URL(string: "https://bandmateallcaps.herokuapp.com/api/token"),
let tokenRefreshURL = URL(string: "https://bandmateallcaps.herokuapp.com/api/refresh_token") {
configuration.tokenSwapURL = tokenSwapURL
configuration.tokenRefreshURL = tokenRefreshURL
configuration.playURI = ""
}
let manager = SPTSessionManager(configuration: configuration, delegate: self)
return manager
}()
func requestSpotifyAccess() {
let requestedScopes: SPTScope = [.appRemoteControl, .userReadPrivate]
self.sessionManager.initiateSession(with: requestedScopes, options: .default)
}
Upon initiation of a SPTSession, I want to connect my remote:
lazy var appRemote: SPTAppRemote = {
let appRemote = SPTAppRemote(configuration: configuration, logLevel: .debug)
appRemote.delegate = self
return appRemote
}()
func sessionManager(manager: SPTSessionManager, didInitiate session: SPTSession) {
self.appRemote.connectionParameters.accessToken = session.accessToken
self.appRemote.connect()
}
Upon app connection, I want to play the ID of a Spotify track that is declared globally:
var pendingSpotifyId: String!
func appRemoteDidEstablishConnection(_ appRemote: SPTAppRemote) {
print("connected")
self.appRemote.playerAPI!.delegate = self
self.appRemote.playerAPI!.subscribe(toPlayerState: { (result, error) in
if let error = error {
debugPrint(error.localizedDescription)
} else if self.pendingSpotifyId != nil {
self.appRemote.playerAPI!.play(self.pendingSpotifyId, callback: { (any, err) in
self.pendingSpotifyId = nil
})
}
})
}
My problem:
This flow is broken up as any time I try to initiate a session, sessionManager(manager: SPTSessionManager, didFailWith error: Error) is always called returning the following error:
Error Domain=com.spotify.sdk.login Code=1 "invalid_grant" UserInfo={NSLocalizedDescription=invalid_grant}
I need the session to initiate successfully so that sessionManager(manager: SPTSessionManager, didInitiate session: SPTSession) can be called and I can connect my remote and, ultimately, play my Spotify track.
What I've tried:
I have ensured a number of things:
Ensured the state of the Spotify app in the background on the user's device is playing (per this ticket: https://github.com/spotify/ios-sdk/issues/31)
Ensured that the correct scopes are in place when receiving an access token. Returned JSON looks something like:
{"access_token":"###","token_type":"Bearer","expires_in":3600,"refresh_token":"###","scope":"app-remote-control user-read-private"}
Things I'm suspicious of:
I am unaware if my token swap via Heroku is being done correctly. This is the only reason I can think of as to why I would be getting this issue. If I am able to use the Spotify API, is this evidence enough that my token swap is being done correctly? (I suspect it is)
Here's what we found out, hope it will help:
The SpotifySDK tutorial doesn't mention that Bundle ID and App Callback URL must precisely match across App Info.plist, source code, Spotify App Dashboard and Heroku Env Vars. The Bundle ID used must match your application Bundle ID.
The App Callback URL must not have empty path, ie: my-callback-scheme://spotify-login-callback
When using both Web Spotify SDK and iOS Framework Spotify SDK in app, take care that only one of them performs auth. Otherwise the App Callback URL will be called twice resulting in error.
The Spotify configuration.playURI may need to be set to empty string rather than nil. Sample app has a note on it.
It's best to have only one instance of object managing Spotify auth in the app. Otherwise ensuring that the correct object is called from the AppDelegate open url method can be tricky.

Why is admob consent form not loading on actual devices?

I am trying to implement Admob for my iOS app. The form loads on the Xcode simulator devices. I am located in the US, but I have used the following code to test that the Consent SDK is working for European users. When I use this with a simulator, the form and ads load.
PACConsentInformation.sharedInstance.debugIdentifiers = ["SPECIFIC_TO_MY_DEVICE"]
PACConsentInformation.sharedInstance.debugGeography = PACDebugGeography.EEA
The form does not load on my physical device with this configuration. The form also did not load when I used testflight to distribute a test version to a test user in the EU. Subsequently, the ads did not load on "European" devices.
When the form should load, I get an error from the below block of code. Also. I get the error WebKitDomain Error 101. My ATS settings are set up in the plist per the Admob documentation.
thisForm.load {(_ error: Error?) -> Void in
if let error = error {
print("Error loading form: \(error.localizedDescription)")
//I am getting the error here.
} else {
thisForm.present(from: self) { (error, userPrefersAdFree) in
print("in present handler")
if let error = error {
// Handle error.
print("error presenting: \(error.localizedDescription)")
} else if userPrefersAdFree {
//TODO: find a way to disable ads
} else {
// Check the user's consent choice.
//let status = PACConsentInformation.sharedInstance.consentStatus
}
}
}
Does anyone know what may be causing these errors with physical devices? I have tried with a real ad id and a test ad id.
Present consent form only if the following equals true
If requestLocationInEEAOrUnknown == true {
//present consent form
}
else {
//do whatever is needed
}

What's the iOS API for AWS Cognito User Pool Custom Authentication Flow?

Amazon docs docs outlines how its custom authentication flow works. But there are only passing mentions of iOS.
I have a working authentication system using AWS User Pools and its Custom Authentication Flow using Python Triggers and an iOS Swift app.
But there's a detail still troubling me - see comment after code.
In the AWSCognitoIdentityCustomAuthentication handler I've got this:
func getCustomChallengeDetails(_ authenticationInput: AWSCognitoIdentityCustomAuthenticationInput, customAuthCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityCustomChallengeDetails>) {
self.customAuthenticationCompletion = customAuthCompletionSource
if authenticationInput.challengeParameters.count > 0 {
DispatchQueue.main.async {
if let code = self.codeTextField.text {
let details = AWSCognitoIdentityCustomChallengeDetails(
challengeResponses: ["CODE" : code])
details.initialChallengeName = "CUSTOM_CHALLENGE"
customAuthCompletionSource.set(result: details)
}
}
}
func didCompleteStepWithError(_ error: Error?) {
// handling code
}
}
The first call of getCustomChallengeDetails() has an empty list for challengeParameters. The second call has a correctly populated challengeParameters
The method didCompleteStepWithError(_ error: Error?) misled me as I thought it only called when an error occurs but is in fact also called on success with error set to nil.
I also have a UIViewController that prompts the user for a CODE which they've been emailed by my server code. When the user submits the CODE I call this:
if let code = codeTextField.text {
let details = AWSCognitoIdentityCustomChallengeDetails(
challengeResponses: ["CODE" : code])
details.initialChallengeName = "CUSTOM_CHALLENGE"
self.customAuthenticationCompletion?.set(result: details)
DispatchQueue.main.async {
self.dismiss(animated: true, completion: nil)
}
}
This works. The server code will authenticate users who enter correct CODE values but deny those who submit an incorrect value for CODE.
But why the two customAuthenticationCompletion?.set(result: details) calls?
Can anyone say where I've taken a misstep?

Resources