1

I'm trying to make an IOS application and checking whether the user has logged in or not. When a user is not logged in, it should display a login form. Ive created multiple View Controllers and connected them with segue's. Than I used self.performSegueWithIdentifier("userLoggedOff", sender: self) to activate the segue. But the error EXC_BAD_ACCESS appears.

However, when I try to activate the same segue with the same code linked to a button, it all works. Really frustrating... Thnx for your help.

This is the full code. I used JSON to get the user's account info. It's probably not the way to do it, but i'm a beginner in xcode, swift and app-making in general.

import UIKit
import WebKit

class StartController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        checkLoginStatus_main()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func checkLoginStatus_main(){
        let requestURL: NSURL = NSURL(string: "https://www.example.net/account/accountinfo.php")!
        let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithRequest(urlRequest) {
            (data, response, error) -> Void in


            let httpResponse = response as! NSHTTPURLResponse
            let statusCode = httpResponse.statusCode

            if (statusCode == 200) {

                do{

                    let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)

                    if let accounts = json["account"] as? [[String: AnyObject]] {

                        for account in accounts {

                            if let status = account["status"] as? String {

                                if let name = account["name"] as? String {
                                    if(status == "true"){
                                        print("LoggedIn") // THIS WORKS
                                        self.continueScanner()

                                    } else {
                                        print("LoggedOut") // THIS WORKS
                                        self.continueLoginForm()
                                    }
                                }

                            }
                        }

                    }

                }catch {
                    print("Error with Json: \(error)")
                }

            }

        }
        task.resume()
    }

    func continueLoginForm(){
        self.performSegueWithIdentifier("userLoggedOff", sender: self)
    }

    func continueScanner(){
        self.performSegueWithIdentifier("userLoggedIn", sender: self)
    }
}

This is the error: Screenshot of the error

Here is the full output:

LoggedOut
1   0x18771c654 <redacted>
2   0x1834bcfe4 <redacted>
3   0x1834b1e50 _os_once
4   0x1834ba728 pthread_once
5   0x1886a6d98 <redacted>
6   0x1886a6680 WebKitInitialize
7   0x188c4bfa0 <redacted>
8   0x100791a3c _dispatch_client_callout
9   0x1007928b4 dispatch_once_f
10  0x182ebcfc8 <redacted>
11  0x182ec38b8 <redacted>
12  0x182ecdd78 <redacted>
13  0x188f9c130 <redacted>
14  0x188f9c2cc <redacted>
15  0x188f9be9c <redacted>
16  0x188ca9ff4 <redacted>
17  0x188f9c154 <redacted>
18  0x188f9be9c <redacted>
19  0x188e780a0 <redacted>
20  0x188f9c154 <redacted>
21  0x188f9c2cc <redacted>
22  0x188f9be9c <redacted>
23  0x188e773d4 <redacted>
24  0x188d19140 <redacted>
25  0x188addfcc <redacted>
26  0x1889a07ec <redacted>
27  0x1889a0744 <redacted>
28  0x18928f504 <redacted>
29  0x188cfcca0 <redacted>
30  0x188d22a04 <redacted>
31  0x188d257a0 <redacted>
Lenny
  • 350
  • 1
  • 13
  • Please post the full error. There should be more than just `EXC_BAD_ACCESS`. – JAL Apr 26 '16 at 20:59
  • I added a screenshot – Lenny Apr 26 '16 at 21:17
  • did you try to set Exception breakpoint? – heximal Apr 26 '16 at 21:17
  • @Lenny There should be an explanation in the Xcode console saying why you are getting the EXC_BAD_ACCESS. Look at that and add that information to your post. – Drew C Apr 26 '16 at 21:18
  • @DrewC updated my post – Lenny Apr 26 '16 at 21:22
  • @heximal where should is set the breakpoint? – Lenny Apr 26 '16 at 21:30
  • 1
    You should go to Breakpoints panel and click little Plus button in the bottom. When the breakpoint is set, run your project as usual, this may give you a clue. http://i.stack.imgur.com/BPe8N.png – heximal Apr 26 '16 at 21:52
  • You should try dispatching your `performSegueWithIdentifier` on the main thread - `dispatch_async(dispatch_get_main_queue(), { self.performSegue...` – Paulw11 Apr 26 '16 at 22:13
  • Make sure your call to performSegueWithIdentifier is being made on the main thread. Have a look at http://stackoverflow.com/questions/26262398/uilabel-text-updated-inside-a-swift-closure-refuses-to-show for more info. – Calvedos Apr 26 '16 at 22:14
  • @Paulw11 clearly types faster than me :) – Calvedos Apr 26 '16 at 22:16
  • @Paulw11 It works, thank you! :) So if I understand it correctly, the code makes sure the segue is performed on the main thread? – Lenny Apr 27 '16 at 11:10
  • Yes, that is right. The network operation completes on a background thread and all UI updates must be on the main queue. – Paulw11 Apr 27 '16 at 11:35

2 Answers2

0

@Paulw11 has solved the problem in the comments on my post.

dispatch_async(dispatch_get_main_queue(), { self.performSegue...

Thanks for your help everyone!

Lenny
  • 350
  • 1
  • 13
-1

I think you probably need to implement prepareForSegue

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        if (segue.identifier == "userLoggedOff") {
            let vc = segue.destinationViewController as? LogInScreenViewController
        } else if (segue.identifier == "userLoggedIn") {
            let vc = segue.destinationViewController as? UserLoggedInViewController 
//This should be the name of whatever view controller they go to if they are logged in.
    }
    }
rmickeyd
  • 1,551
  • 11
  • 13
  • it won't help. prepareForSegue method is optional in viewcontroller implementation. – heximal Apr 26 '16 at 21:12
  • Thank you for your answer! Unfortunately, it doesn't seem to slove the problem – Lenny Apr 26 '16 at 21:19
  • This won't make any difference. All you're doing is needlessly loading the destination view controller into a variable you don't do anything with. – Calvedos Apr 26 '16 at 22:02