Learning swift 2/ Xcode 7 and creating a app iOS 9 where I can enter a custom address. I have mapkit working and can search regular address. I have my current location working. Instead of entering an address: "Number,Street, city, zip code", I want the user to enter: PR33.1 for example, and that would show the user that location. I have the long and lats for the custom address's, I've read many things on geocoding and annotations but nothing that would let me accomplish what I need. Can this be done? jSon file maybe.. I'm really new at this...
thanks
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
let arbitraryString: [String:String] =
["161 RL2": "23908709138882,-106.7433588579297",
"40.9 RL112":"32.393144,-106.727762"]
#IBAction func enterButtonTapped(sender: AnyObject) {
if let textField = arbitraryString["161 RL2"] {
print(" \(textField).")
} else {
print("That is not in the dictionary.")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Related
I'm very new to Swift and I'm struggling a bit with a simple task. I have made a simple app which will launch a local HTML file.
I now want to add a text box that appears, asking the user "Enter IP address". Then, that text will be inserted into the HTML file in 3 different places.
Does anyone have any ideas on this? Or know of a good site which might have some tutorials which relate to this idea? Here's my code so far:
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet weak var htmlload: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let htmlpath = Bundle.main.path(forResource: "index", ofType: "html")
let url = URL(fileURLWithPath: htmlpath!)
let request = URLRequest(url: url)
htmlload.load(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is an simple Swifter application in Swift 3.2 and xCode9.
Its working.
But I would write this application when this app is in background, then it bringing to front and I will see the standard white screen.
What is the best way for this idea?
import UIKit
import AudioToolbox
import WatchKit
import Foundation
import Swifter
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let server = HttpServer()
server["/hello"] = {
var queryParamsInfo = ""
for (name, value) in $0.queryParams {
queryParamsInfo += "\(name) -> \(value)<br/>"
}
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
return .ok(.html("<h3>You asked for \(queryParamsInfo)</h3>"))
}
do {
try server.start()
print("Server is started")
while true {
}
}
catch {
print("Error!")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Thanks for your help!
This is not possible on iOS. An application can only enter the foreground if
The user taps the app icon
The user taps a notification
The user brings the app to the foreground from the multitasking menu
The app is opened by another app (using URL Schemes)
You can not bring your app to the foreground programatically.
Im trying to play a Youtube video in my app with the Webview
the build is succsesful but when i run the app it shows all white and i get this error: Thread 1:EXC_BAD_INSTRUCTION(code=EXC_I1386_INVOP,subcode=0x0)
it happenes on the Youtube.loadRequest(URLRequest(url:url!))
//
// ViewController.swift
// CFBC
//
// Created by KWIA on 6/6/17.
// Copyright © 2017 KWIA. All rights reserved.
//
import UIKit
import Firebase
class ViewController: UIViewController {
#IBOutlet weak var Youtube: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
getVideo(videoCode: "RmHqOSrkZnk")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getVideo(videoCode:String)
{
let url = URL(string: "https//www.youtube.com/embed/\(videoCode)")
Youtube.loadRequest(URLRequest(url: url!))
}
}
Your URL is not valid, you are missing : after https.
It should be: "https://www.youtube.com/embed/\(videoCode)"
That leads to URL(string:) returning nil and to a crash when you try to force unwrap it using !.
I want to stream music to my app using this library I found online called Jukebox. Heres a link to their page: https://github.com/teodorpatras/Jukebox
import UIKit
import Jukebox
class ViewController: UIViewController {
var jukebox = Jukebox()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.jukebox = Jukebox(delegate: self, items: [ JukeboxItem(URL: NSURL(string: "http://mixtapemonkey.com/mixtapes/zip/491/Chance%20The%20Rapper%20-%2010Day%20Official%20Final/03%20Nostalgia.mp3")!)
])
}
I get an error where it says 'Jukebox(delegate: self' stating that I cannot convert the value of type 'ViewController' to expected argument type 'JukeboxDelegate?'
#IBAction func Play(sender: UIButton) {
self.jukebox.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
where am i going wrong? Thanks
My guess is that you've failed to inherit from the Jukebox delegate in your class declaration. Should be something like this:
class ViewController: UIViewController, JukeboxDelegate {
var jukebox = Jukebox()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.jukebox = Jukebox(delegate: self, items: [ JukeboxItem(URL: NSURL(string: "http://mixtapemonkey.com/mixtapes/zip/491/Chance%20The%20Rapper%20-%2010Day%20Official%20Final/03%20Nostalgia.mp3")!)
])
}
}
Since these delegate methods aren't optional, you'll also have to make your ViewController conform to the delegate by including the delegate methods, listed below.
Defines the five possible states that Jukebox can be in.
public protocol JukeboxDelegate : class {
func jukeboxStateDidChange(jukebox : Jukebox)
func jukeboxPlaybackProgressDidChange(jukebox : Jukebox)
func jukeboxDidLoadItem(jukebox : Jukebox, item : JukeboxItem)
}
The library seems to be written in Swift, and unless you put some extra effort into it, delegate methods can't be optional which is why you're getting the error about your VC not conforming to the methods. You at least have to have the method signatures in your class, and it's up to you whether you want anything to happen when they're called.
i don't know Jukebox - but something doesn't work with your ViewController. Your ViewController is a class not a class instance. You need to first create an instance of this controller.
Maybe something in this direction helps .. but im not good in Swift, too.
let VCJukeBox = UIViewController()?
VCJukeBox.jukebox = Jukebox(delegate: self, items: [ JukeboxItem(URL: NSURL(string: "yoururl.mp3")!)
])
Im not good in swift, too - but maybe this helps somehow.
This is my first time doing a simple project in swift and these errors are bugging me for last couple of hours. I have this code below and even though i have curly braces around and statements inside the if/else i still get that errors. Any help would be greatly appreciated guys.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var `switch`: UISwitch!
#IBOutlet var Answer: UILabel!
#IBOutlet var tempInput: UITextField!
//aqnswer value
#IBAction func switchPressed(sender: AnyObject)
{
if switch.on {
self.Answer.text = "cel to fah"
}
else {
self.Answer.text = "fah to cel"
}
}
//textfield value
#IBAction func calculate(sender: AnyObject)
{
//get user input
// value = celcius
var Value:Int = tempInput.text.toInt()!
var toFah :Int = ( 32 + Value * 9 ) / 5
//to celcius
var toCel: Int = (Value-32) * 5 / 9
if switch.on {
self.Answer.text = toFah.description
}
else {
self.Answer.text = toCel.description
}
// println(fah)
// Answer.text = fah.description
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The Swift Language Guide says:
If you need to give a constant or variable the same name as a reserved
Swift keyword, surround the keyword with back ticks (`) when using it
as a name. However, avoid using keywords as names unless you have
absolutely no choice.
In your example you have indeed a choice…
But if you really really really want to use switch as a variable name you have to wrap all occurrences of switch in back ticks.