Using the Twitter Account

Now let’s fill out the post-else case, replacing the simple success NSLog. We’re going to work with the accountStore that the user has graciously given us permission to use. Because it’s a local variable in scope at the time of the closure’s creation, we can use it within the completion handler closure.

On iOS, the user may have set up several accounts of a given type; a fancier app would show them and let the user pick one, but for now, we’ll just make sure there’s at least one. We can use the ACAccountStore’s accountsWithAccountType to get all configured accounts of type twitterAccountType. Code the following, directly before the }) that ends the closure.

 let​ twitterAccounts =
  accountStore.accountsWithAccountType(twitterAccountType)
 guard​ twitterAccounts.count > 0 ​else​ {
 NSLog​ (​"no twitter accounts configured"​)
 return
 }

Once again, we’re just using a guard and bailing out with an NSLog message to the console if there are no Twitter accounts configured. We could come back later and give the user a helpful UIAlertController dialog in this case.

Continuing on the post-guard happy path, let’s assume the array contains at least one Twitter account. What do we do with it? A while back—before we had to work through getting access to the account—we noted there is an SLRequest class that accesses the web service APIs of the social networks like Twitter. It doesn’t have a lot of methods, and one that we should focus on is performRequestWithHandler:, whose docs say it “performs an asynchronous request and calls the specified handler when done.”

And look, it takes another closure! Well, actually it takes an SLRequestHandler, which if we click the link to its documentation, is defined as follows:

 typealias​ ​SLRequestHandler​ = (​NSData​!, ​NSHTTPURLResponse​!, ​NSError​!) -> ​Void

So this is another closure, taking NSData, and NSHTTPURLResponse, and NSError as optional parameters and returning Void. We’ll assume those parameters will give us everything we need to handle the response from the Twitter web service. But since we’ll probably have a bunch of work to do for that, and this method is already getting pretty long, let’s stub out a method to accept those parameters and deal with the response when it comes in. Somewhere outside all the existing methods’ curly braces—right before the class’s closing curly brace would be a great place for it—let’s stub out the following method, empty but for a simple NSLog that at least lets us know we got this far:

 private​ ​func​ handleTwitterData (data: ​NSData​!,
  urlResponse: ​NSHTTPURLResponse​!,
  error: ​NSError​!) {
 guard​ ​let​ data = data ​else​ {
 NSLog​ (​"handleTwitterData() received no data"​)
 return
  }
 NSLog​ (​"handleTwitterData(), ​​(​data.length​)​​ bytes"​)
 }

Now we’ll be able to have our performRequestWithHandler closure just call this method, allowing us to put off for now just what’s in the Twitter response and how we’re going to deal with it.

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

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