IOS 应用程序崩溃 - 但不是在模拟器中 - 硬编码 URL 有效

标签 ios json swift

通过 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()

}

如前所述,如果我取消对硬编码变量的注释,它会正常运行而不会出现问题。

任何帮助将不胜感激,因为我仍然无法弄清楚为什么它在模拟模式下运行没有任何问题但在实时测试中失败,即使所有字段都有值。

编辑:(崩溃数据)

#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:) 如果字符串不是正确格式的 URL,则可能返回 nil。将其强制解包为 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)

关于IOS 应用程序崩溃 - 但不是在模拟器中 - 硬编码 URL 有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45364013/

相关文章:

ios - 模态视图 Controller 不覆盖状态栏

ios - 如何在 Swift 2.0 中使用异步方法更新 tableviewcell?

swift - 如何使用在不同约束之间进行插值的自动布局执行交互式动画?

ios - 点击时清除横幅通知

ios - 在 iOS 8 中更改 UISearchBar 取消按钮文本

javascript - JSON解码: Mixed types - not valid syntax error

json - Realm 格式日期 JSON swift

ios - UITableView的indexPathsForSelectedRows是否按选择顺序排序?

ios - CGImageCreateWithImageInRect 区域不正确?

php - 如何在数据库的php sdk4中存储facebook的GraphObject?