0

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?

Devhess
  • 289
  • 4
  • 16

1 Answers1

2

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.

Muneeba
  • 1,756
  • 10
  • 11
  • Thank you for your answer! I already had used this in the AppDelegate. But's it's bot working one More Time. That's why I thought that it's not right to simply use this in the AppDelegate. To get sure a that there is no other reason I set a bool NSUserDefault to true if the AppDelegate get a message and checked that in my app with a print in the console. And it's still false, so there was no message. – Devhess Jan 06 '16 at 17:54
  • Thats strange! i have tried this and its working.Can you show the implementation of didReceiveMessage? – Muneeba Jan 07 '16 at 04:07
  • ` func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) { let message2Tweet = message["text2Tweet"]! as? String dispatch_async(dispatch_get_main_queue(), { \\Whole code is too long... NSUserDefaults.standardUserDefaults().setBool(true, forKey: "CalledAppDelegate") print("Nachricht in AppDelegate: \(message2Tweet)") } ` – Devhess Jan 07 '16 at 12:50
  • Why are you activating the WCSession in your didReceiveMessage function. The session needs to be activated prior to that. Put the WCSession activiation code in didFinishLaunchWithOptions of AppDelegate. – Muneeba Jan 08 '16 at 04:13
  • Thank you! That solves! But why should I call this in didFinishLaunchWithOptions, I thought this method is only activating if I launch the App... – Devhess Jan 08 '16 at 09:07
  • No if you are class is supposed to interact with WCSession then the session must be activated as soon as that class is lived so that it will listen/receive the message,. In case of UIViewController write the activiation code in viewDidLoad and since in AppDelegate the didfinishlaunchWithOptions is the first method so we write the code there – Muneeba Jan 08 '16 at 09:42