How to: watchkit button displays image at url in Swift? - ios

I want a button press in Watchkit to display an image from a URL. Given there's no UIWebView in Watchkit, my assumption is that I will need to trigger via the watch button, the loading of a UIWebView for the URL on the phone, store in an UIImage, that I can pass back to the watch to display. First is that the best strategy, secondly can anyone help me with the code for storing a UIImage from UIWebView in Swift?
Phone's ViewController:
import UIKit
class ViewController: UIViewController {
var URLPath = "myURL.jpeg"
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
loadAddressURL()
}
//to fit webpage within webView
override func viewDidAppear(animated: Bool) {
webView.frame = UIScreen.mainScreen().bounds
webView.center = self.view.center
webView.scalesPageToFit = true
}
func loadAddressURL () {
let url = NSURL(string: URLPath)
let requestObj = NSURLRequest(URL: url!)
webView.loadRequest(requestObj)
}
}

Related

Constraints not keeping WKWebView inside Safe Area

Please excuse my ignorance. I am very new to iOS development and Xcode. I have tried searching many pages on this site and haven't found any that directly solve my issue. Any help you can provide would be greatly appreciated!
I am unable to get my webview (WKWebview) to remain within the Safe Area boundaries (I think that's the best way to describe it). I have autogenerated constraints set on the webview and I can see within the Main.Storyboard editor that the webview is within the safe area.
Unfortunately the webview ignores these boundaries and encompasses the entire view and the text on my webpage appears at the top of the display behind the time, battery and connection icons.
What am I doing wrong?
Here is my code and a few pictures:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
#IBOutlet var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = Bundle.main.url(forResource: "bulk_material_table", withExtension: "html")!
webView.loadFileURL(url, allowingReadAccessTo: url)
let request = URLRequest(url: url)
webView.load(request)
}
}
Your constraints are OK. The problem is with your overridden loadView() method. You don't need to create a new WKWebView since you are using storyboards and Interface Builder is creating it. Remove this method and move uiDelegate to viewDidLoad():
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.uiDelegate = self // Move delegate here
let url = Bundle.main.url(forResource: "bulk_material_table", withExtension: "html")!
webView.loadFileURL(url, allowingReadAccessTo: url)
let request = URLRequest(url: url)
webView.load(request)
}
}
Tried to make this work programmatically. Is it OK? Does anybody know how to change the color of top inset of safe area?
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(webView)
guard let url = URL(string: "https://apple.com/") else {
return
}
webView.load(URLRequest(url: url))
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// webView.frame = view.bounds // previously
let topPadding = view.safeAreaInsets.top
webView.frame = view.frame.inset(by: UIEdgeInsets(top: topPadding, left: CGFloat(0), bottom: CGFloat(0), right: CGFloat(0)))
}
it doesn't keep WKWebView inside safe area before and here what I've thought to add
view.insetsLayoutMarginsFromSafeArea = true

WKWebView loaded twice, trying to go back I have to press twice on back navigation button

I'm trying to show a website using the WKWebView, it works fine as for the internet work.
problem is it is loading the site twice (it seems like it's going to another ViewController).
here is my code:
#IBOutlet weak var myWebView: WKWebView!
override func loadView() {
myWebView = WKWebView()
myWebView.navigationDelegate = self as? WKNavigationDelegate
view = myWebView
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = false
let url = URL(string: "https://google.com")!
myWebView.load(URLRequest(url: url))
}

how can playing video within webView in swift

i have an issue for playing video from youtube within webView i try to playing video from youtube not in full screen in webView but i couldn't and i set the the property allowsInlineMediaPlayback for webView in viewDidLoad() but it's not working
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://www.youtube.com")
let YOUTUBE = URLRequest(url:url!)
webView.loadRequest(YOUTUBEURL)
webView.allowsInlineMediaPlayback = true
}
so please can help me for how can i playing video youtube not in full screen in webView
thanks A lot
you should use UIWebView instead of WKWebView.
here is the needed code for you:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
myWebView.allowsInlineMediaPlayback = true
myWebView.loadHTMLString("<iframe width=\"\(myWebView.frame.width)\" height=\"\(myWebView.frame.height)\" src=\"https://www.youtube.com/embed/FDT343j3zD0?playsinline=1\" frameborder=\"0\" allowfullscreen></iframe>", baseURL: nil)
}
}

Multiple accounts login in different webView in same app

How i can login many accounts of same site in different webView. For example i have Tab Bar Controller That contains three View Controllers and each View Controllers contain webView. And for example i embed stackoverflow url for webView in every class. How user can login to different accounts at the same time using these three webView? I've tried this but i just login one user at a time. Can any one please tell me how this is possible in (Swift or Objective-C)??
Thanks
class FirstViewController: UIViewController , UIWebViewDelegate{
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
webView.delegate = self
let requestURL = NSURL(string: "http://stackoverflow.com")
let request = NSURLRequest(URL: requestURL!)
activityIndicator.hidesWhenStopped = true
activityIndicator.startAnimating()
webView.loadRequest(request)
}
func webViewDidFinishLoad(webView: UIWebView) {
activityIndicator.stopAnimating()
}
}
class SecondViewController: UIViewController, UIWebViewDelegate{
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
webView.delegate = self
let requestURL = NSURL(string: "http://stackoverflow.com")
let request = NSURLRequest(URL: requestURL!)
activityIndicator.hidesWhenStopped = true
activityIndicator.startAnimating()
webView.loadRequest(request)
}
func webViewDidFinishLoad(webView: UIWebView) {
activityIndicator.stopAnimating()
}
}

How do I correctly disable link detection in UIWebView?

I Have a UIWebView strictly for viewing PDF's, and I don't want any link detection in the view, Like this:
So I added the following code to negate the detection of links and the presentation of that view:
override func viewDidLoad() {
super.viewDidLoad()
myWebView.dataDetectorTypes = UIDataDetectorTypes.None
}
However it still picks up links, and presents that modal view. How do I correctly implement code to stop this?
Here is my updated code:
class PdfViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var myWebView: UIWebView!
var contentUrlPassedOn: String!
var allowLoad = true
override func viewDidLoad() {
super.viewDidLoad()
myWebView.delegate = self
let url: NSURL! = NSURL(string: contentUrlPassedOn)
myWebView.loadRequest(NSURLRequest(URL: url))
myWebView.dataDetectorTypes = UIDataDetectorTypes.None
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return allowLoad
}
func webViewDidStartLoad(webView: UIWebView) {
var hasFinishedLoading = false
updateProgress()
}
func webViewDidFinishLoad(webView: UIWebView) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC))),
dispatch_get_main_queue()) {
[weak self] in
if let _self = self {
_self.hasFinishedLoading = true
self!.allowLoad = false
}
}
}
If your webview was added by the storyboard, You can disable this in the attributes inspector.
[1
It's an objective-c code, but u can do the same in Swift. It will fix the interaction with links. The con is that they will be still detected.
- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if (inType == UIWebViewNavigationTypeLinkClicked) {
return NO;
}
return YES;
}
Finely Solve the problem for you,
Working Demo
Cannot disable long press link menu, Which is Bug in iOS, That's solve in iOS 8.2.
According to bug, when press long on link then its open Action Sheet, Now in iOS 8.2 its solve as below.
Load PDF using WKWebView as below.
import UIKit
import WebKit
class ViewController: UIViewController , WKUIDelegate {
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView
}
override func viewDidLoad() {
super.viewDidLoad()
super.viewDidLoad()
var url = NSURL(string:"http://www.nursing.umich.edu/studentresources/documentation/CreatingBookmarksPDFdocsV7.pdf")
var req = NSURLRequest(URL:url!)
self.webView!.loadRequest(req)
}
}
Now you Click on your long Press link then its doesn't open ActionSheet PopUp.
Enjoy..!!
Output :

Resources