m.b. I'm asking stupid question, but I'm really newbie in Swift and iPhone programming. Using XCode 8.0.
I want to create WebView application and have taken next code from here: https://sourcefreeze.com/uiwebview-example-using-swift-in-ios/
My Code :
import UIKit
import Foundation
class ViewController: UIViewController {
#IBOutlet weak var myWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL (string: "http://www.sourcefreeze.com");
let requestObj = NSURLRequest(URL: url!);
myWebView.loadRequest(requestObj);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Of course I've created UI object of UIWebView and connected it to controller:
#IBOutlet weak var myWebView: UIWebView!
During the build I receive next red alert:
'NSURL' is not implicitly convertible to 'URL'; did you mean to use 'as' to explicitly convert?
As I understand this code worked perfectly before 2 years. I guess that I'm missing something, but what?
Thanks for advance.
Try to use URL instead of NSURL as it is replaced with URL type in Swift 3
let url = URL(string: "http://www.sourcefreeze.com")
Here is the instruction from apple developer forum
The Swift overlay to the Foundation framework provides the URL
structure, which bridges to the NSURL class. The URL value type offers
the same functionality as the NSURL reference type, and the two can be
used interchangeably in Swift code that interacts with Objective-C
APIs. This behavior is similar to how Swift bridges standard string,
numeric, and collection types to their corresponding Foundation
classes.
For more information about value types, see Classes and Structures in
The Swift Programming Language (Swift 3) and Working with Cocoa
Frameworks in Using Swift with Cocoa and Objective-C (Swift 3).
It is the simplest way to solve issue:
let url = URL(string: "http://www.sourcefreeze.com")!
myWebView.load(URLRequest(url: url))
Xcode often tales you what is wrong. So from Xcode message it is possible to understand that NSURL should be converted to URL. Try to read relevant resources and bot use old tutorials. Swift is dynamically changes language always try to use up to date resources.
Try in this way
myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.sourcefreeze.com")))
myWebView.delegate = self;
self.view.addSubview(myWebView)
This code worked:
let myUrl = URL (string: "http://www.sourcefreeze.com")!
let request = URLRequest(url: myUrl);
myWebView.loadRequest(request);
Now the WebView is invisible, but I'll try firstly to search the solution in a forum.
Related
A Mac app requires that a HTML file be called in a WebView (the legacy type, not the newer WKWebView) in a localized form to present the user with some content.
As I side note, I realize that WebView should not be used today, and WKWebView is preferred, however this is a legacy app that currently needs support.
I've used a similar method for the iOS version, however it does not seem to be working. The HTML files are simply called "Term.HTML" and are placed in each localization folder alongside the localized string and all other localized content. This is the code I tried to use:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:NSLocalizedString(#"fileTerm", nil) ofType:#"html"];
htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[termsView takeStringURLFrom:htmlString];
Where my localized strings file each contain a line that says:
"fileTerm" = "Term";
This is what links the declaration of the first line to the actual file. It works in iOS. However, when running the app and the view containing the WebView attempt to the run, XCode will automatically create a breakpoint on the third line when I actually attempt to give the HTML file to "termsView" which is my WebView. After skipping this breakpoint, and forcing the app to run, the whole view containing the WebView will simply not appear. I would be thankful if anyone knew why this was or if there was a better way to do this? Thank you everyone!
may be someone needs in SWift: I solved this problem with saving 3 html file for every language, and then in ViewController class checked current app language. And called up the html file for current language
func loadHtmlFile() {
let preferredLanguage = NSLocale.preferredLanguages[0]
if preferredLanguage == "kz" {
let url = Bundle.main.url(forResource: "aboutUs_kz", withExtension:"html")
let request = URLRequest(url: url!)
webView.load(request)
}
if preferredLanguage == "ru" {
let url = Bundle.main.url(forResource: "aboutUs_ru", withExtension:"html")
let request = URLRequest(url: url!)
webView.load(request)
}
if preferredLanguage == "en" {
let url = Bundle.main.url(forResource: "aboutUs_en", withExtension:"html")
let request = URLRequest(url: url!)
webView.load(request)
}
}
in viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
loadHtmlFile()
}
BEGINNER ALERT! Please talk to me like I'm a 5-year-old because I am new to this platform and language. I will be grateful and unoffended.
I have the following code in my Xcode project to open a website in the native browser from a button on the app home page:
#IBAction func faceURL(_ sender: Any) {
NSURL *spotiURL = [NSURL URLWithString:#"https://www.facebook.com"];
[[UIApplication sharedApplication] openURL:faceURL options:#{} completionHandler:^(BOOL success) {
if (success){
NSLog(#"Opened url");
}
}];
}
On the line:
NSURL *spotiURL = [NSURL URLWithString:#"https://www.facebook.com"];
it's throwing the following error:
Expected "," separator Consecutive statements on a line must be separated by ";"
It recommends putting a ; between the colon and # symbol before the URL. I know some of this is deprecated with the new Xcode and Swift format, but I can't find a straightforward answer for this particular issue. Thank you in advance for your wisdom.
UPDATE
Thank you to the folks who helped with this. In case anyone else comes across this issue, here is the code that ended up working to touch the button and open a URL in the native browser:
#available(iOS 10.0, *)
#IBAction func openURL(_ sender: Any) {
let openURL = URL(string: "https://www.facebook.com")
UIApplication.shared.open(openURL!, options: [:], completionHandler: nil)
}
I also had to delete the original button and corresponding reference in the ViewController, replace the button, and do a fresh connection with ViewController to get it working. Many thanks!
It looks like you're trying to use Objective C code in a Swift method. If your project is written in Swift then this is (probably) the code you need:
#IBAction func faceURL(_ sender: Any) {
if let facebookURL = URL(string: "https://www.facebook.com")
{
UIApplication.shared.openURL(facebookURL) // the open method you were using doesn't exist in Swift
}
}
I'd suggest you work through a few tutorials to learn the basics of iOS development.
Well, you are mixing Swift (first line) and ObjC (all the other) syntax, so you have to choose one of them first of all. Your "mixed" code looks good btw.
First: you have faceURL in openURL when it should be spotiURL.
Second: you mean to convert the code you copied from SO from Obj-C to Swift which I will do for you :)
let spotiURL = URL(string: "https://www.facebook.com")
UIApplication.shared.openURL(spotiURL!)
Go easy on me I am new to I OS and Swift :). I am trying to create a IOS app using swift. I have a web view display that is working correctly, displaying the website. YAY!!
What I need to do now is create a unique identifier that is stored locally and when the app is opened is sent to the remote server. I see i can use this...
UIDevice.currentDevice().identifierForVendor!.UUIDString
However i would like to store it locally for future use and send it to the remote server every time the app is opened. I have done research on this and have come upon answers for other objects just not a web view.
If someone knows of a tutorial or example code for this solution i would greatly appreciate it.
UPDATE
let uuid = UIDevice.currentDevice().identifierForVendor!.UUIDString
and for the url im using
let url= NSURL (string:"https://example.com");
Could i do something like this? Or like it?
let url= NSURL (string:"https://example.com");
let requestobj= NSURLRequest(URL:url! ADD VAR HERE? );
Where ADD VAR HERE is the uuid to pass to the server which i can catch with a php script?
Latest update..
Im having a hard time integrating that into my existing code. Where would be the best place to put it?
import UIKit
class ViewController: UIViewController {
let uuid = UIDevice.currentDevice().identifierForVendor!.UUIDString
#IBOutlet weak var WebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL (string: "https://example.com");
let requestObj = NSURLRequest(URL: url?)
WebView.loadRequest(requestObj);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is the answer i was looking for. Thanks for your help everyone!
let device_uuid = UIDevice.currentDevice().identifierForVendor!.UUIDString
let api_host = "https://example.com?uuid=" + device_uuid
let url = NSURL(string: api_host)
let req = NSURLRequest(URL: url!)
WebView.loadRequest(req);
Apparently what i needed to do was build my URL into a variable. Then i can structure it using the NSURL and use it from there. This guide helped me. Just ignore the ruby on rails part if that's not what your doing.
http://ericlondon.com/2015/12/09/sending-messages-between-a-swift-webview-and-a-rails-backend-using-javascript.html
You will need to check on the webserver side to confirm exactly what you need to pass in - but if you are developing that side as well, then you should have control :-)
Should be something like this - please not that you don't need ; in swift
let request= NSURLRequest(URL:url)
var bodyData = "myUUID=\(uuid)&otherData=value1"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
Keep in mind that this identifier will change if a user uninstalls the application. If you need to persist it then I'd recommend to store it on the keychain so the id is always the same for the same phone even if the app is uninstalled.
Check this other question: How to preserve identifierForVendor in ios after uninstalling ios app on device?
What is the easiest library / framework to use?
I see lots of libraries but don't know which the industry standard is.
Here is some working Code for you to make it simpler. This works perfectly in my game.
import AVFoundation
var bgMusic:AVAudioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
var bgMusicURL:NSURL = NSBundle.mainBundle().URLForResource("yourTune", withExtension: "mp3")!
bgMusic = AVAudioPlayer(contentsOfURL: bgMusicURL, error: nil)
bgMusic.numberOfLoops = 1
bgMusic.prepareToPlay()
bgMusic.play()
}
Make sure to change the name of your tune! :)
You don't need an additional library to play sounds in iOS. Just use what is provided to you by the corresponding SDK: AVAudioPlayer.
Hi I am really new to coding in Swift, and am trying to follow the codes in this book: http://www.apress.com/9781484202098. Learn iOS 8 App Development 2nd Edition by James Bucanek
In particular, I am working through Chapter 3 - building a URL shortening app, but despite having copied the code exactly, I am getting an error on the code in Page 76:
if let toShorten = webView.request.URL.absoluteString {
which states 'NSURLRequest?' does not have a member named 'URL'.
I have tried googling an answer, but unfortunately have not come across anything. Any response I can find seems to suggest that my code ought to be working (e.g. How to get url which I hit on UIWebView?). This seems to have the closest answer SWIFT: Why I can't get the current URL loaded in UIWebView? but the solution does not appear to work for me. If I add a ? after the request, it will then at least build it, but I then have a nil variable returned.
I am using Xcode v6.1.1. Here is the piece of code that is coming up with the error in ViewController.swift:
let GoDaddyAccountKey = "0123456789abcdef0123456789abcdef" //this is replaced by my actual account key in my own code
var shortenURLConnection: NSURLConnection?
var shortURLData: NSMutableData?
#IBAction func shortenURL( AnyObject ) {
if let toShorten = webView.request?.URL.absoluteString { // ? now added
let encodedURL = toShorten.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let urlString = "http://api.x.co/Squeeze.svc/text/\(GoDaddyAccountKey)?url=\(encodedURL)"
shortURLData = NSMutableData()
if let firstrequest = NSURL(string: urlString) //added if here and removed !
let request = NSURLRequest(URL:firstrequest)
shortenURLConnection = NSURLConnection(request:request, delegate:self)
shortenButton.enabled = false
}
}
}
If you have any suggestions on how I can fix this, I would really appreciate it!
Update:
Following suggestions from Ashley below, I have amended my code so that it is no longer bringing up the error (see comments above). However, it is now no longer running. This appears to be because the urlString is being created as http://api.x.co/Squeeze.svc/text/d558979bb9b84eddb76d8c8dd9740ce3?url=Optional("http://www.apple.com/"). The problem is therefore the Optional() that is included and thus makes it an invalid URL. Does anyone have a suggestion on how to remove this please?
request is an optional property on UIWebView:
var request: NSURLRequest? { get }
also stringByAddingPercentEscapesUsingEncoding returns an optional:
func stringByAddingPercentEscapesUsingEncoding(_ encoding: UInt) -> String?
What you need is to make user of optional binding in a few places:
if let toShorten = webView.request?.URL.absoluteString {
if let encodedURL = toShorten.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
let urlString = "http://api.x.co/Squeeze.svc/text/\(GoDaddyAccountKey)?url=\(encodedURL)"
shortURLData = NSMutableData()
if let firstrequest = NSURL(string: urlString) { // If a method can return a nil, don't force unwrap it
let request = NSURLRequest(URL:first request)
shortenURLConnection = NSURLConnection(request:request, delegate:self)
shortenButton.enabled = false
}
}
}
See Apple's docs on optional chaining for details
See Apple's docs for NSURL class