2015-08-09 16 views
7

ho una parte di codice che funziona come previsto su tutte le versioni di iOS, ma non su iOS 9:strano (bug?) Con Xcode 7/iOS 9 B5 con dataWithContentsOfURL

NSData *response = [NSData dataWithContentsOfURL: [NSURL URLWithString: url] options:NSDataReadingUncached error:&error]; 

E 'un testo semplice JSON .

ho ottenuto questo errore:

Error Domain=NSCocoaErrorDomain Code=256 "The file “xxx.php” couldn’t be opened." UserInfo={NSURL= http://xxx.xxx.com/xxx/xxx.php?lang=fr }

Come questo URL può essere interpretate come un file? Risposta = nil ...

Grazie.

+0

Invia domanda su forum Apple. –

+0

Chiedo di essere sicuro che non sarei manca qualcosa (per esempio: https obbligatorio dal iOS 9) – skrew

risposta

10

Tecnicamente è a causa dei cambiamenti di NSURLSession di networking in iOS9. Per risolvere il problema, è necessario andare a info.plist dell'app, NSAppTransportSecurity [Dictionary] deve avere una chiave NSAllowsArbitraryLoads [Boolean] da impostare su YES o chiamare urls con https.

Si può vedere di più su cambiamenti di NSURLSession di networking in iOS9 in http://devstreaming.apple.com/videos/wwdc/2015/711y6zlz0ll/711/711_networking_with_nsurlsession.pdf?dl=1

+1

C & P Versione: ' NSAppTransportSecurity \t NSAllowsArbitraryLoads \t ' – bk138

+0

Non fare questo! Se lo fai, stai sostanzialmente sconfiggendo le funzionalità di sicurezza aggiuntive di "App Transport Security" introdotte in iOS9. Invece vedi questa risposta: http://stackoverflow.com/a/30836686/505093 – kwahn

+0

questo è stato un aiuto huuuuuuuge, grazie! – Mona

0

dopo il debug per 3 ore, ho evitato il bug alltogether utilizzando asincrono NSMutableURLRequest, che ho anche osservato è il modo più veloce rispetto al NSData sincrono.

let requestURL: NSURL = NSURL(string: url)! 
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL) 
let session = NSURLSession.sharedSession() 
let task = session.dataTaskWithRequest(urlRequest) { 
    (data, response, error) -> Void in 
    if error == nil { 
     var response = UIImage(data:data!) 
    } else { 
     NSLog("Fail") 
    } 
} 
task.resume() 
Problemi correlati