Devhess
Devhess

Reputation: 289

Calling func session didReceiveMessage in AppDelegate

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?

Upvotes: 0

Views: 916

Answers (1)

Muneeba
Muneeba

Reputation: 1776

If your iPhone app is closed and you have implemented the

 func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
}

in a specific VC you won't get the message until your VC is live but if you have implemented the above method in AppDelegate , you will received it even when your app is closed.

As mentioned in the link

Calling this method from your WatchKit extension while it is active and running wakes up the corresponding iOS app in the background and makes it reachable. Calling this method from your iOS app does not wake up the corresponding WatchKit extension

Your app did wake up in background but since your VC is not live that's why you are not getting the message.

Upvotes: 2

Related Questions