首页 > 解决方案 > 除与 Xcode 连接的设备外,iOS 应用程序在所有设备上崩溃

问题描述

当用户尝试注册新帐户时,我的应用程序崩溃并退出,但这发生在任何设备上,但使用 Xcode 部署应用程序的设备除外。

所有设备均已在开发者帐户中注册并运行 iOS 11.4.1

这是注册按钮功能:

@IBAction func regButton(_ sender: Any) {

    usernameText = usernameTextField.text
    mobileText = mobileTextField.text
    emailText = emailTextField.text
    passwordText = passwordTextField.text
    fieldText = categoryTextField.text

    print(usernameText ?? "damn")
    print(mobileText ?? "damn")
    print(emailText ?? "damn")
    print(passwordText ?? "damn")
    print(fieldText ?? "damn")

    if(type=="Seeker")
    {
        let url1 = "http://app.alosboiya.com.sa/hourjob.asmx/insert_jobseeker?name="+usernameText!+"&phone="+mobileText!

        let url2 = "&email="+emailText!+"&password="+passwordText!+"&workex="+"companyDescText!"

        let url3 = "&category="+fieldText!+"&image="+"downloadURLGlobal!"

        let url4 = "&unpaidhour="+"string"+"&hourpaidlast30="+"string"+"&totalhourworked="+"string"+"&balance="+"string"+"&username="+usernameText!

        stringURL = url1 + url2 + url3 + url4
    }else
    {
        let url1 = "http://app.alosboiya.com.sa/hourjob.asmx/insert_company?name="+usernameText!+"&field="+fieldText!

        let url2 = "&phone="+mobileText!+"&email="+emailText!+"&password="+passwordText!+"&workex="+"companyDescText!"+"&crcopy="+"downloadURLGlobal!"+"&logo="+"string"+"&username="+usernameText!

        stringURL = url1 + url2
    }

    if Reachability.isConnectedToNetwork()
    {

        if(checkbox.on==true)
        {

        let url = URL(string: stringURL!)
        Alamofire.request(url!).responseString {

            (response) in

            let result = response.result.value
            do {
                if(result=="True")
                {

                    let alert = UIAlertController(title: "Registration Successfully", message: "Registration Done Successfully Congratulations",
                                                  preferredStyle: UIAlertControllerStyle.alert)
                    alert.addAction(UIAlertAction(title: "OK", style:
                        UIAlertActionStyle.default, handler: self.doSomething))
                    self.present(alert, animated: true, completion: nil)



                }else
                {
                    let alert = UIAlertController(title: "Registration Failed", message: "Registration Failed Please Try Again", preferredStyle: UIAlertControllerStyle.alert)

                    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (action) in
                        alert.dismiss(animated: true, completion: nil)
                    }))

                    self.present(alert, animated: true, completion: nil)
                }

            }

        }

    }else
        {
            let alert = UIAlertController(title: "License Agreement", message: "Check to Agree Licence Agreement", preferredStyle: UIAlertControllerStyle.alert)

            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (action) in
                alert.dismiss(animated: true, completion: nil)
            }))

            self.present(alert, animated: true, completion: nil)
        }

    }else
    {
        let alert = UIAlertController(title: "No Network Connection", message: "Connection Error Please Try Again", preferredStyle: UIAlertControllerStyle.alert)

        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (action) in
            alert.dismiss(animated: true, completion: nil)
        }))

        self.present(alert, animated: true, completion: nil)
    }



}

标签: iosswift

解决方案


如果用户点击按钮但一个或两个文本字段为空,则您的应用程序将由于您在代码中使用的强制展开(!标记)而崩溃。

示例:如果 mobileText 字段为空,则您的应用程序将崩溃:

 let url1 = "http://app.alosboiya.com.sa/hourjob.asmx/insert_jobseeker?name="+usernameText!+"&phone="+mobileText!

解决方案是使用保护语句

guard let usernameText = usernameTextField.text,
    mobileText = mobileTextField.text,
    emailText = emailTextField.text,
    passwordText = passwordTextField.text,
    fieldText = categoryTextField.text else {
return
}

推荐阅读