# Buggy code
def ios_source_analysis(src):
    """iOS Objective-C Code Analysis"""
    try:
        logger.info("Starting iOS Source Code and PLIST Analysis")
        api_rules = ios_apis.CODE_APIS
        code_rules = ios_rules.CODE_RULES
        code_findings = {}
        api_findings = {}
        email_n_file = []
        url_n_file = []
        url_list = []
        domains = {}

        for dirname, _, files in os.walk(src):
            for jfile in files:
                if jfile.endswith(".m"):
                    jfile_path = os.path.join(src, dirname, jfile)
                    if "+" in jfile:
                        new_path = os.path.join(src, dirname, jfile.replace("+", "x"))
                        shutil.move(jfile_path, new_path)
                        jfile_path = new_path
                    dat = ""
                    with io.open(
                        jfile_path, mode="r", encoding="utf8", errors="ignore"
                    ) as flip:
                        dat = flip.read()

                    # Code Analysis
                    relative_src_path = jfile_path.replace(src, "")
                    code_rule_matcher(
                        code_findings, [], dat, relative_src_path, code_rules
                    )
                    # API Analysis
                    api_rule_matcher(
                        api_findings, [], dat, relative_src_path, api_rules
                    )

                    # Extract URLs and Emails
                    urls, urls_nf, emails_nf = url_n_email_extract(
                        dat, relative_src_path
                    )
                    url_list.extend(urls)
                    url_n_file.extend(urls_nf)
                    email_n_file.extend(emails_nf)
        # Domain Extraction and Malware Check
        logger.info("Performing Malware Check on extracted Domains")
        domains = malware_check(list(set(url_list)))
        logger.info("Finished Code Analysis, Email and URL Extraction")
        code_analysis_dict = {
            "api": api_findings,
            "code_anal": code_findings,
            "urlnfile": url_n_file,
            "domains": domains,
            "emailnfile": email_n_file,
        }
        return code_analysis_dict

    except:
        PrintException("[ERROR] iOS Source Code Analysis")


# Traceback (KeyError)
[INFO] 08/Feb/2019 16:17:54 - VM Starting
[INFO] 08/Feb/2019 16:17:55 - "POST /DynamicAnalyzer/ HTTP/1.1" 200 32400
[INFO] 08/Feb/2019 16:17:55 - "GET /static/css/devices.min.css HTTP/1.1" 200 44241
[INFO] 08/Feb/2019 16:17:55 - "GET /download/screen/screen.png HTTP/1.1" 302 0
[DEBUG] 08/Feb/2019 16:17:55 - Exception while resolving variable 'exp' in template 'general/error.html'.
Traceback (most recent call last):
File "/home/userhome/Mobile-Security-Framework-MobSF/venv/lib/python3.6/site-packages/django/template/base.py", line 829, in _resolve_lookup
current = current[bit]
File "/home/userhome/Mobile-Security-Framework-MobSF/venv/lib/python3.6/site-packages/django/template/context.py", line 83, in __getitem__
raise KeyError(key)
KeyError: 'exp'

During handling of the above exception, another exception occurred:

# Fixed code
def ios_source_analysis(src):
    """iOS Objective-C Code Analysis"""
    try:
        logger.info("Starting iOS Source Code and PLIST Analysis")
        api_rules = ios_apis.CODE_APIS
        code_rules = ios_rules.CODE_RULES
        code_findings = {}
        api_findings = {}
        email_n_file = []
        url_n_file = []
        url_list = []
        domains = {}

        for dirname, _, files in os.walk(src):
            for jfile in files:
                if jfile.endswith(".m"):
                    jfile_path = os.path.join(src, dirname, jfile)
                    if "+" in jfile:
                        new_path = os.path.join(src, dirname, jfile.replace("+", "x"))
                        shutil.move(jfile_path, new_path)
                        jfile_path = new_path
                    dat = ""
                    with io.open(
                        jfile_path, mode="r", encoding="utf8", errors="ignore"
                    ) as flip:
                        dat = flip.read()

                    # Code Analysis
                    relative_src_path = jfile_path.replace(src, "")
                    code_rule_matcher(
                        code_findings, [], dat, relative_src_path, code_rules
                    )
                    # API Analysis
                    api_rule_matcher(
                        api_findings, [], dat, relative_src_path, api_rules
                    )

                    # Extract URLs and Emails
                    urls, urls_nf, emails_nf = url_n_email_extract(
                        dat, relative_src_path
                    )
                    url_list.extend(urls)
                    url_n_file.extend(urls_nf)
                    email_n_file.extend(emails_nf)
        # Domain Extraction and Malware Check
        logger.info("Performing Malware Check on extracted Domains")
        domains = malware_check(list(set(url_list)))
        logger.info("Finished Code Analysis, Email and URL Extraction")
        code_analysis_dict = {
            "api": api_findings,
            "code_anal": code_findings,
            "urlnfile": url_n_file,
            "domains": domains,
            "emailnfile": email_n_file,
        }
        return code_analysis_dict

    except:
        PrintException("iOS Source Code Analysis")