【问题标题】:App Crashes When trying to load a web view in iOS 7尝试在 iOS 7 中加载 Web 视图时应用程序崩溃
【发布时间】:2016-05-13 18:55:43
【问题描述】:

我正在使用 swift 2.2 并开发了一个具有表格视图的应用程序。在特定行的水龙头上,我正在加载一个网络视图.. 一切看起来都很完美,并且在运行 iOS 9 的 ipad 上完美运行。当我尝试加载同一个应用程序并尝试在 iOS 7 设备上加载 Web 视图时,应用程序崩溃。代码是

class AppCatalogViewController: UIViewController , UIWebViewDelegate {

var alertView = UIAlertView()

override func viewDidLoad() {
    super.viewDidLoad()
    NSLog("enetered the webView view controller")
    self.title = NSLocalizedString("mdm.agent.common.appCatalog", comment : "")
    self.view.autoresizesSubviews = true
    alertView = UIAlertView(title: NSLocalizedString("mdm.agent.common.loadingData", comment: ""), message: "", delegate: nil, cancelButtonTitle: nil, otherButtonTitles: "")
    let actInd : UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    actInd.startAnimating()
   actInd.frame = CGRectMake(125, 60, 37, 37)
    alertView.addSubview(actInd)
    //Change self.view.bounds to a smaller CGRect if you don't want it to take up the whole screen
    NSLog("setting the frame of the webview")
    let webView : UIWebView = UIWebView(frame: self.view.bounds)

    webView.autoresizingMask = UIViewAutoresizing.FlexibleWidth
    webView.scalesPageToFit = true
    webView.autoresizesSubviews = true
    webView.delegate = self



    let persist = Persistence()
    let url : String = "https://\(persist.getObject(mdmiosagent_Constants.SERVERNAMEKEY)):\(persist.getObject(mdmiosagent_Constants.SERVERPORTKEY))/showAppsList.mobapps?udid=\(persist.getObject(mdmiosagent_Constants.UDIDKEY))&isNativeAgent=true&authtoken=\(defaults.authToken)&SCOPE=\(defaults.scope)"

    let request : NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: url)!)
    request.timeoutInterval = 10
    var userAgent : String = ""
    if(UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Phone) {
        userAgent = "iPhone"
    } else {
        userAgent = "iPad"
    }

    request.setValue(userAgent, forHTTPHeaderField: "User-Agent")

    webView.loadRequest(request)

    self.view.addSubview(webView)
    alertView.show()

}

func webViewDidStartLoad(webView: UIWebView) {

}
func webViewDidFinishLoad(webView: UIWebView) {

    alertView.dismissWithClickedButtonIndex(-1, animated: true)
}

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

func showError(error : NSString) {
    let alert : UIAlertView = UIAlertView(title: NSLocalizedString("mdm.agent.common.error", comment: ""), message: error as String, delegate: nil, cancelButtonTitle: "mdm.agent.common.okay", otherButtonTitles: "", "")
    alert.show()
}
// method to check authentication
func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {

    if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
        challenge.sender!.useCredential(NSURLCredential(forTrust: (challenge.protectionSpace.serverTrust!)), forAuthenticationChallenge: challenge)
    }
    challenge.sender?.continueWithoutCredentialForAuthenticationChallenge(challenge)
}

这在 iOS 9 设备上完美运行.. 在 iOS 7 设备的日志中显示的错误是

May 13 12:58:38 iPhone mdm.ios[1040] <Error>: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x2fc3bf83 0x3a3ecccf 0x2fb75f0d 0x328f21af 0x328f2f75 0x328f352b 0xdcd84 0xdce24 0x3245d4ab 0x3245d269 0x325e936b 0x32506d63 0x32506b6d 0x32506b05 0x32458d59 0x320d662b 0x320d1e3b 0x320d1ccd 0x320d16df 0x320d14ef 0x320cb21d 0x2fc07255 0x2fc04bf9 0x2fc04f3b 0x2fb6febf 0x2fb6fca3 0x34a75663 0x324bc14d 0xc1318 0x3a8f9ab7). 

谁能找出原因?

【问题讨论】:

  • 问题不在tableView中?正如错误表明您正在将 nil 插入到数组中,但我在上面的代码中看到了任何数组?
  • tableView 没有问题,完美运行
  • 事实上我在我的项目中没有使用过 NSArray
  • 只需在所有异常上启用断点,您将在五分钟内修复错误。
  • 难道不能在调试器下运行应用程序吗?.. 在这种情况下获取崩溃日志并对其进行符号化(可能由 Xcode 自动完成)。从堆栈跟踪中,您将看到异常出现的位置。

标签: ios swift webview


【解决方案1】:

我试过你的代码,效果很好。 异常说:

[__NSArrayM insertObject:atIndex:]: 对象不能为 nil'

可能是因为您尝试本地化一个字符串,例如,您的文件 Localizable.strings 已经存在但尚未本地化。

当您的应用程序的旧版本已启动到您的 iOS7 设备或模拟器设备,并且您重新运行您的新版本但此未本地化的文件仍驻留在应用程序包的资源文件夹中时,就会发生这种情况。

当您构建运行时,不会删除应用的新版本未使用的文件,因此最好的方法是从您的模拟器或真实设备中完全删除应用,清除缓存并再次构建运行。

此外,正如您在 cmets 所解释的那样,通过 app Catalog 的更新情况导致了这种崩溃。避免这种事情的最好方法是使用 if let 或 guard 条件来保护您的本地化。

从 iOS 7 开始,我相信您可以将字符串文件设为 UTF-8 而不是 UTF-16。

来自official Apple doc

注意:建议您使用 UTF-8 保存字符串文件 encoding,这是标准字符串文件的默认编码。 Xcode 自动将字符串文件从 UTF-8 转码为 UTF-16 时 它们被复制到产品中。有关更多信息 标准字符串文件格式,请参阅创建字符串资源文件。为了 有关 Unicode 及其文本编码的更多信息,请访问 http://www.unicode.org/http://en.wikipedia.org/wiki/Unicode

【讨论】:

  • 我必须通过应用程序目录推送此应用程序的更新,用户收到更新并更新应用程序。但是,程序成功越过self.view.addSubview(webView),但在调用 alertview.show() 时崩溃
  • 如果是这样的话,那么应用程序也应该在 ios 9 上崩溃了?
  • 我的意思是它在 ios 8 或 9 中怎么不会崩溃?\
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
  • 2014-05-22
相关资源
最近更新 更多