簡體   English   中英

應用程序崩潰嘗試在iOS 7中加載Web視圖時

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

我正在使用swift 2.2並開發了一個具有表格視圖的應用程序。 在一個特定的行上,我正在加載一個Web視圖..一切看起來都很完美,並且在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). 

任何人都可以找出可能是什么原因?

我試過你的代碼,效果很好。 例外情況說:

[__NSArrayM insertObject:atIndex:]:object不能為nil'

可能是因為你試圖本地化一個字符串,例如你的文件Localizable.strings已經存在但尚未本地化。

當您的應用程序的舊版本已啟動到您的iOS7設備或模擬器設備,並且您重新運行新版本但這個未本地化的文件仍駐留在應用程序包中的資源文件夾中時,會發生這種情況。

當您構建運行時,新版本的應用程序未使用的文件將不會被刪除,因此最好的方法是從模擬器或真實設備中完全刪除應用程序,清除緩存並再次構建運行。

此外,正如您通過評論解釋的那樣,通過應用程序目錄的更新情況會導致此類崩潰。 避免此類事情的最佳方法是使用let或guard條件保護您的本地化。

從iOS 7開始,我相信你可以創建字符串文件UTF-8而不是UTF-16。

來自Apple官方文檔

注意:建議您使用UTF-8編碼保存字符串文件,這是標准字符串文件的默認編碼。 當它們被復制到產品中時,Xcode會自動將字符串文件從UTF-8轉碼為UTF-16。 有關標准字符串文件格式的詳細信息,請參閱創建字符串資源文件。 有關Unicode及其文本編碼的更多信息,請訪問http://www.unicode.org/http://en.wikipedia.org/wiki/Unicode

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM