Setting Up Twitter API Calls

When we first set up our tweet-sending button, we found the documentation for the Social framework and made use of the SLComposeViewController. To start using the rest of Twitter’s features (or any other social network supported by iOS), we’ll need to use another class in this framework. SLRequest lets us call the various social networks’ web APIs by just providing a URL, whose contents vary by service and are documented at their various developer sites (such as http://dev.twitter.com). For Twitter, we have an additional detail to work through first: as of May 2013, all Twitter requests need to be authenticated, meaning they need to come from a signed-in Twitter user.

Fortunately, iOS allows a user to sign in to her Twitter account from the Settings app, and the iOS SDK will allow us to use that authentication. The key to this is that the SLRequest includes an account property that represents an authenticated user. All we need to do is to set that property before we send off our request.

To use a social-networking account, we have to ask the ACAccountStore for access to it, by means of a requestAccessToAccountsWithType method. And that raises an interesting question: what happens if the user says no? In fact, let’s consider the worst case: that the user switches out of our app, goes to the privacy settings, and changes the permission setting for our app’s access to Twitter while our app is running. We’re basically going to have to plan on asking for permission to use Twitter every time we need to make a request. And as it turns out, that has some interestingly asynchronous behavior.

images/asynchronicity/twitter-access-request-alert.png

The first time we call requestAccessToAccountsWithType, the user will be presented with an alert asking if she wants to grant our app access to her Twitter accounts, as shown in this figure. We have no idea whether the answer will be Don’t Allow or OK, and we certainly don’t want to hold up the whole app waiting for an answer, so instead we’ll make this call and move on with the rest of our app. If and when the user approves our use of her Twitter account, then we’ll go ahead and call Twitter’s web service. Actually, the user will only ever see the alert once—after that, she can grant or deny access via the Settings app’s Privacy settings—but our code won’t behave any differently; the decision about whether or not to run our asynchronous code will just be made sooner.

Let’s start by adding an import Accounts to the top of ViewController.swift, just like we did when we added the Social framework.

 import​ ​Accounts

Then look at the requestAccessToAccountsWithType method in the documentation viewer. It takes an ACAccountType, which has constants for Twitter, Facebook, and a few other services. The second argument is a dictionary of options whose use depends on the service. The third parameter is of type ACAccountStoreRequestAccessCompletionHandler. That’s new, so we click on its documentation and see this:

 typealias​ ​ACAccountStoreRequestAccessCompletionHandler​ =
  (​Bool​, ​NSError​!) -> ​Void

What...the...heck?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.194.248