iOS web view error - ios

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 !.

Related

How to insert user entered data into a local html document

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.
}
}

simple data insertion using realm swift

I'm an newbie in realm-swift. Trying to do an simple data insertion in realm DB.
I'm getting following warning:-
WARNING: An RLMRealm instance was deallocated during a write transaction and
all pending changes have been rolled back. Make sure to retain a reference
to the RLMRealm for the duration of the write transaction.
Here is my code :-
//
// Dog.swift
// RealmDemo
//
// Created by RIPA SAHA on 20/07/16.
// Copyright © 2016 RIPA SAHA. All rights reserved.
//
import Foundation
class Dog {
dynamic var name = ""
dynamic var age = ""
}
//
// ViewController.swift
// RealmDemo
//
// Created by RIPA SAHA on 19/07/16.
// Copyright © 2016 RIPA SAHA. All rights reserved.
//
import UIKit
import RealmSwift
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let myDog = Dog()
myDog.name = "puppies"
myDog.age = "5"
// Get the default Realm
let realm = try! Realm()
// Add to the Realm inside a transaction
realm.beginWrite()
realm.add(myDog)
/*realm.add(myDog)
realm.commitWrite()*/
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
any help would be appreciated...
Follow this example:
import Foundation
import RealmSwift
class Dog : Object {
dynamic var name = ""
dynamic var age = ""
}
Wrap your write code as follow:
try! realm.write {
realm.add(myDog)
}
This should fix the problem

Custom Address Search iOS mapkit

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.
}
}

`how to open a link in a UIWebView with latest swift version

I'm a newbie on IOS development. Recently, I use UIWebView to load a webpage, and this is successful, and try to open links in it on the same UIWebView instance. However, after a click on some link, there's no response from UIWebView, and in xcode, it reports
2015-03-29 23:42:07.246 xxxxx[17349:1251340] Unknown result for URL http://www.xxxxxx.org/bugs/xxxxxx-xxxx-xxxxxx, for frame
I looked it up on Internet but I got little useful help. I know someone said to implement func webView in UIWebViewDelegate, but my app still doesn't work.
Can someone help me? I'm using the latest version of swift language. And a snippet of code is :
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var front: UIWebView!
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType nt: UIWebViewNavigationType) -> Bool {
if (nt ==.LinkClicked){
return true
}
return true;
}
override func viewDidLoad() {
super.viewDidLoad()
UIWebView.loadRequest(self.front)(NSURLRequest(URL: NSURL(string: "http://www.google.com")!)) // 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.
}
}
Here is the sample Code :
UIWebView.loadRequest(webviewInstance)(NSURLRequest(URL: NSURL(string: "www.google.in")))

DELAY issues: how to apply styles immediately or not show loaded webpage until styles are applied?

Technologies Used: XCode 6, iOS8, Swift
I'm loading a webpage in a uiwebview and I'm also appending a new stylesheet to the body of that webpage and overwriting some of its styles. But, there is a delay (maybe 1 second or 2) between when the webpage loads and the styles are applied so you can see the webpage before its restyled. I'm using javascript to append the new styles to the body of the webpage. How can I fix this so that the webpage will only show with the styles are already applied? Here is my code:
import UIKit
class SecondViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var website: UIWebView!
var url = "http://www.fake-website-url.net"
func loadUrl() {
let requestURL = NSURL(string: url)
let request = NSURLRequest(URL: requestURL!)
website.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
website.delegate = self
loadUrl()
}
func webViewDidFinishLoad(website: UIWebView) {
var loadStyles = "var script = document.createElement('link');script.type = 'text/css';script.rel = 'stylesheet';script.href = 'http://fake-url.styles.css';document.getElementsByTagName('body')[0].appendChild(script);"
website.stringByEvaluatingJavaScriptFromString(loadStyles)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Note, I'm using Swift.
What I would do is create a property to store the downloaded page. Then override the property setter to add your custom style sheet after the page is saved to that property. Then finally load it into your Web View.
Hope that makes sense.

Resources