I updated to iOS 8.1 and updated my xcode but now something is broken on with my webview.
I used this part of code:
let stream = "http://stream.hive365.co.uk:8088/live"
let url = NSURL(string: stream)
let request = NSURLRequest(URL: url)
webplayer.loadRequest(request)
xcode gives a error about the let request
value of optional type NSURL? not unwrapped; did you mean to use! or ?
I'm new with iOS swift development and can't get the error away...
any help would be appreciated!
NSURL returns an optional so you need to check if it is nil
let stream = "http://stream.hive365.co.uk:8088/live"
if let url = NSURL(string: stream) {
let request = NSURLRequest(URL: url)
webplayer.loadRequest(request)
}
change
let url = NSURL(string: stream)
to
let url = NSURL(string: stream)?
or
let url : NSURL = NSURL(string: stream) as NSURL
Related
this line gives me error "use of unresolved identifier 'URL'"
let url = URL(string: urlpath!)
if you are swift ver is 3 then use
let url = URL(string: urlpath!)
else it is lesser than swift3 then Use NSURL instead of URL
let url = NSURL(string: urlpath!)
For swift 2.2 , this is the correct way:
let url = NSURL(string: urlpath!)
I'm trying to convert NSURL to NSData. My url string is a local file url which I'm getting from my device.
let url = NSURL(string: url)
let imageData = NSData(contentsOfURL: url)
let image = UIImage(data: imageData)
I'm getting NSURL value properly. But while converting it to NSData it gives nil value. I know there are similar questions in stackoverflow but none of them solves my problem. I'm using swift 2.2
If the string points to a local file url then you are using the wrong API. The correct one is
let url = NSURL(fileURLWithPath: urlString)
NSURL(string: is only for URL strings which start with a valid file scheme (e.g. http or ftp)
Try this code.
let urlString = "/var/mobile/Media/DCIM/101APPLE/IMG_1827.JPG"
let url = URL(fileURLWithPath: urlString)
let imageData = NSData(contentsOf: url)
let image = UIImage(data: imageData as! Data)
The Correct one is use contentsOf: forecastURL! as URL instead of contentsOf: forecastURL
let forecastURL = NSURL(string: "http://photos.state.gov/libraries/media/788/images/90x90.gif")
let testImage = NSData (contentsOf: forecastURL! as URL)
print("data",testImage!)
let image = UIImage(data: testImage! as Data)
print("imaGE :-",image!)
Getting an "unexpected found nil" error, but when checking the value - its there:
override func viewDidLoad() {
super.viewDidLoad()
if whichLink == "official link" {
let urlStr = videoGame.offLink!
let url = NSURL(string: urlStr)!
let request = NSURLRequest(URL: url)
webView.loadRequest(request)
}
else if whichLink == "moby game link" {
print("yo yo yo, value is here! \(videoGame.mgLink) ")
let urlStr1 = videoGame.mgLink!
let url1 = NSURL(string: urlStr1)!
let request = NSURLRequest(URL: url1)
webView.loadRequest(request)
}
}
I'm suspecting an error in storyboard... but can't locate anything.
Did anyone has a clue what can be wrong?
The full project can be found # https://github.com/flostik2008/Favorite-Games
Your URL string is incorrectly formatted with the space at the end, so the NSURL initialization is returning nil.
You should URL encode all raw strings before trying to create an NSURL:
let urlStr1 = videoGame.mgLink!.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())! should work
Hello when I use WKWebView codes with Swift 3 gives me this error
'URLRequest'; did you mean to use 'as' to explicitly convert?
I think this is bug I need help or ideas ? My codes under below
Thanks
import UIKit
import WebKit
class SocialsViewController: UIViewController, WKNavigationDelegate {
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://facebook.com")!
webView.loadRequest(NSURLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
}
Use URL and URLRequest instead:
let url = URL(string: "https://facebook.com")!
webView.load(URLRequest(url: url))
This is quite similar to https://stackoverflow.com/a/37812485/2227743: you either use NSURL and have to downcast it as URL, or you directly use the new Swift 3 structs.
If you follow the error message, your example would become:
let url = NSURL(string: "https://facebook.com")!
webView.load(URLRequest(url: url as URL))
It could be even worse:
let url = NSURL(string: "https://facebook.com")!
webView.load(NSURLRequest(url: url as URL) as URLRequest)
All this works but of course it's much better to start using URL and URLRequest in Swift 3, without using all this downcasting.
Try this one
let myUrl = URL(string: "https://facebook.com")!
webView.loadRequest(URLRequest(url:myUrl));
I have a problem with the UIWebView. I cant open a Website with Windows Authentication... In the Safari Browser a pop-up appears, therefor in the WebView Control happened nothing.
Can someone help me with this problem?
Here my code:
let url = NSURL (string: "http://inside.domain.com");
let requestObj = NSURLRequest(URL: url!);
browser.loadRequest(requestObj);
I also set the Info.plist.....
Thanks for helping
I am assuming that your username is myUserName and password is myPassword
Now modifying your piece of code
let urlComponents = NSURLComponents(string: "http://inside.domain.com");
urlComponents.user = "myUserName";
urlComponents.password = "myPassword";
let url = urlComponents.URL;
let requestObj = NSURLRequest(URL: url!);
browser.loadRequest(requestObj);