繁体   English   中英

iOS应用程序崩溃-但不在模拟器中-硬编码网址有效

[英]IOS app crash - but not in simulator - hardcoding URL works

通过xcode成功构建后,该应用程序将在模拟器和我的iPhone中运行-当我分发它进行测试时,当测试用户尝试执行搜索时,该应用程序将崩溃。

如果我对URL元素进行硬编码-它可以在模拟模式下工作,并且可以在用户测试试用中完美运行。

我正在运行CrashLytics,它说崩溃是此行代码的结果

let allContactsData = try Data(contentsOf: urlResults!)

我在以前的VC中检查所有字段都包含一个值-并且“打印”确认相同

Company : Accountant
Suburb  : Southport
State   : QLD
Getting results from: http://www.myawsmurl.com.au/api/get_details.php?no=Accountant&state=QLD&suburb=Southport

var通过以下方法设置在类的顶部:

var toSearchFor: String = ""
var toSearchForSuburb: String = ""
var toSearchForState: String = ""

这是导致问题的功能:

func getResults() {
    toSearchFor = searchingFor.trimmingCharacters(in: .whitespacesAndNewlines)
    toSearchForSuburb = searchingSuburb.trimmingCharacters(in: .whitespacesAndNewlines)
    toSearchForState = searchingState.trimmingCharacters(in: .whitespacesAndNewlines)

    print("Company : \(toSearchFor)")
    print("Suburb  : \(toSearchForSuburb)")
    print("State   : \(toSearchForState)")

    //toSearchFor = "Accountant"
    //toSearchForSuburb = "Southport"
    //toSearchForState = "QLD"

    //print("Company : \(toSearchFor)")
    //print("Suburb  : \(toSearchForSuburb)")
    //print("State   : \(toSearchForState)")

    let searchURL = ("http://www.myawsmurl.com.au/api/get_details.php?no=" + toSearchFor + "&state=" + toSearchForState + "&suburb=" + toSearchForSuburb)
    let urlResults = URL(string:searchURL)
    print("Getting results from: \(searchURL)")

    do {
        let allContactsData = try Data(contentsOf: urlResults!)
        let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : NSArray]

        etc etc
    }
    catch {

    }

    tableView.reloadData()

}

如前所述,如果我取消注释硬编码的var,它将按例外运行,没有问题。

任何帮助将不胜感激,因为我仍然不知道为什么它在模拟模式下运行时没有任何问题,但是即使所有字段都有值,也无法通过实时测试。

编辑:(崩溃数据)

#0. Crashed: com.apple.main-thread
0  ThisAWSMAPP                    0x1000752e4 ViewController.getResults() -> () (ViewController.swift:158)
1  ThisAWSMAPP                    0x100076180 specialized ViewController.viewDidAppear(Bool) -> () (ViewController.swift)
2  ThisAWSMAPP                    0x100071e3c @objc ViewController.viewDidAppear(Bool) -> () (ViewController.swift)
3  UIKit                          0x18cb0ddb0 -[UIViewController _setViewAppearState:isAnimating:] + 856
4  UIKit                          0x18cb0e31c -[UIViewController _endAppearanceTransition:] + 228
5  UIKit                          0x18cbc4d64 -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 1224
6  UIKit                          0x18cc93c5c __49-[UINavigationController _startCustomTransition:]_block_invoke + 232
7  UIKit                          0x18cc1aa1c -[_UIViewControllerTransitionContext completeTransition:] + 116
8  UIKit                          0x18cd68fac __53-[_UINavigationParallaxTransition animateTransition:]_block_invoke.99 + 724
9  UIKit                          0x18cb2e9d0 -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 492
10 UIKit                          0x18cb2e4f8 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 312
11 UIKit                          0x18cb2e314 -[UIViewAnimationState animationDidStop:finished:] + 160
12 QuartzCore                     0x189cdf0d4 CA::Layer::run_animation_callbacks(void*) + 260
13 libdispatch.dylib              0x18587e9a0 _dispatch_client_callout + 16
14 libdispatch.dylib              0x1858835e8 _dispatch_main_queue_callback_4CF + 996
15 CoreFoundation                 0x1869750c0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
16 CoreFoundation                 0x186972cdc __CFRunLoopRun + 1572
17 CoreFoundation                 0x1868a2d94 CFRunLoopRunSpecific + 424
18 GraphicsServices               0x18830c074 GSEventRunModal + 100
19 UIKit                          0x18cb5b130 UIApplicationMain + 208
20 ThisAWSMAPP                    0x1000646d8 main (AppDelegate.swift:16)
21 libdyld.dylib                  0x1858b159c start + 4
  1. 如果字符串格式不正确,URL(string :)可能返回nil。 强制将其包装为urlResults!urlResults! ,特别是如果您是根据用户输入来构造它的话。

这样更安全:

if let url = urlResults {
    let allContactsData = try Data(contentsOf:url) 
    // etc
} else {
    // log an error for debugging
}
  1. 您应该确保字符串是URL编码的

String.trimmingCharacters仅从字符串的开头和结尾删除字符。 但是您的输入变量可能包含嵌入空格或URL字符串的其他非法字符。

像这样翻译输入字符串:

toSearchFor = searchingFor.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

给定行上的urlResults表示您的urlResults以某种方式urlResults零。

在制作URL对象之前,请尝试对url字符串进行编码。

let toSearchFor = ""
let toSearchForSuburb = ""
let toSearchForState = ""

let searchURL = ("http://www.myawsmurl.com.au/api/get_details.php?no=" + toSearchFor + "&state=" + toSearchForState + "&suburb=" + toSearchForSuburb).addingPercentEncoding(withAllowedCharacters: . urlQueryAllowed)

if let url = searchURL, let urlResults = URL(string:url) {
    print("Getting results from: \(url)")

    do {
        let allContactsData = try Data(contentsOf: urlResults!)
        let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : NSArray]
    }
    catch {

    }
}

好像是线程问题。 您可以尝试在主线程上执行代码吗?

DispatchQueue.main.async({
let searchURL = ("http://www.myawsmurl.com.au/api/get_details.php?no=" + toSearchFor + "&state=" + toSearchForState + "&suburb=" + toSearchForSuburb)
    let urlResults = URL(string:searchURL)
    print("Getting results from: \(searchURL)")

    do {
        let allContactsData = try Data(contentsOf: urlResults!)
        let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : NSArray]

        etc etc
    }
    catch {}
self.tableView.reloadData()
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM