简体   繁体   中英

App Crashes When trying to load a web view in iOS 7

I am working with swift 2.2 and have developed an app that has a table view . on a particular row's tap , I am loading a web view .. Everything seems perfect and works perfectly on an ipad which is running on iOS 9 . when I try to load the same app and try to load the web view on an iOS 7 device, the app crashes . The code is

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)
}

This works perfectly on iOS 9 device.. and the error that's being shown in the log for iOS 7 device is

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). 

Can anyone find out what might be the reason ?

I've try your code, it works well. The exception which says :

[__NSArrayM insertObject:atIndex:]: object cannot be nil'

probably come out because you try to localize a string where, for example, your file Localizable.strings already existed but has not been localized yet.

This happened when an old version of your app has been launched to your iOS7 device or simulator device,and you re-run your new version but this unlocalized file still resides in the resources folder in the application bundle.

When you build an run a new version of your app unused files will not be deleted so the best way is to remove the app completely from your simulator or real device, clear the cache and build an run again.

Furthermore, as you explain by the comments, the update situation through app Catalog cause this kind of crash. The best way to avoid this kind of thing is to protect your localization with if let or guard conditions.

As of iOS 7, I believe you can make strings file UTF-8 instead of UTF-16.

Coming from official Apple doc :

Note: It is recommended that you save strings files using the UTF-8 encoding, which is the default encoding for standard strings files. Xcode automatically transcodes strings files from UTF-8 to UTF-16 when they're copied into the product. For more information about the standard strings file format, see Creating Strings Resource Files. For more information about Unicode and its text encodings, go to http://www.unicode.org/ or http://en.wikipedia.org/wiki/Unicode .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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