The user can already tweet (the text that was dictated on watch) in foreground if a specific vc is active on the iPhone.
But now I want to go a step forward and send this tweet in Background. So the user dictate a text in the watch app and the text is tweeting even if the app on the iPhone is closed.
I have used this code for the definitions:
let accountStore = ACAccountStore()
let accountType = ACAccountStore().accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter)
var twitterAccount: ACAccount?
var session: WCSession!
And that for the method:
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
let message2Tweet = message["text2Tweet"]! as? String
dispatch_async(dispatch_get_main_queue(), {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "CalledAppDelegate")
print("Nachricht in AppDelegate: \(message2Tweet)")
if(WCSession.isSupported()){
self.session = WCSession.defaultSession()
self.session.delegate = self
self.session.activateSession()
}
self.accountStore.requestAccessToAccountsWithType(self.accountType, options: nil) { (success, error) -> Void in
if !success {
print("Kein Zugriff")
} else {
let allAccounts = self.accountStore.accountsWithAccountType(self.accountType)
if allAccounts.count > 0 {
self.twitterAccount = allAccounts.last as? ACAccount
}
}
let url = NSURL(string: "https://api.twitter.com/1.1/statuses/update.json")
let txt = message2Tweet
if txt != "" {
let request = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: .POST, URL: url, parameters: ["status": txt!])
request.account = self.twitterAccount
request.performRequestWithHandler { (data, response, error) -> Void in
if response.statusCode != 200 {
print(error)
} else {
print("No error")
}
}
}
}
})
}
But it's not working in the for- and background.
How to fix that?