Enable zooming in WKWebView in Swift 4, Xcode 10, for iOS 12 - ios

I am trying to enable zooming/magnification (by pinching) in a very simple WebView in a simple app. (I am pretty new to this).
The Swift code that loads the web page is this and it works fine, I got from here, I think.
import UIKit
import WebKit
class SecondViewController: UIViewController {
#IBOutlet weak var WebViewSchedule: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// load the page
let url = URL(string: "https://mywebsite.com")
let request = URLRequest(url: url!)
WebViewSchedule.load(request)
//how do i make it zoomable ????
}
}
I've looked at a number of answers about this, and they all refer to a property "allowsMagnification". I have tried various ways of using this and got nowhere.
So could some kind person give me a beginners' guide to how to make my iOS Web page zoomable?

Related

How can I specify the user agent of a device in a web app utilizing WKWebView in Swift 4?

I have been trying to figure out how to specify a user agent for my web app in Swift 4 so that the website it opens is in mobile-view. It is defaulting to desktop view instead of mobile. Is there any way to fix this? I am currently learning Swift so I am not that familiar with it yet. I have attached the code below, changing the website for confidentiality reasons.
import UIKit
import WebKit
class ViewController: UIViewController {
var website = "https://www.example.com"
#IBOutlet var webpage: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: website)
let request = URLRequest(url: url!)
webpage.load(request)
}
}
You may change your user agent adding the following HTTPHeaderField:
// valid only for this request
var request = URLRequest(url: URL(string:"https://www.apple.com")!)
request.addValue("myUserAgent", forHTTPHeaderField: "User-Agent")
Another possibility which will completely override the default user agent (might be harmful) is this:
wkWebView.customUserAgent = "myUserAgent"

Swift Error : Classes implemented in two places but files I don't touch?

I am testing out swifts UIWebView, I have placed a simple UIWebview on my storyboard and this is my viewController code
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = URL(string: "https://mywebsitehere.com")
webView.loadRequest(URLRequest(url: url!))
} }
Unfortunately I am getting this Error below, I have added App transport security settings to my app like other answers to a similar question have said to do but it still isn't working...
Can anyone help?
objc[946]: Class PLBuildVersion is implemented in both
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices
(0x124feecc0) and
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices
(0x124e056f0). One of the two will be used. Which one is undefined.
This is bug with xCode while simulating the real iOS device, some time it through such kind of error.
Please check whether your url is active or not.
let url = URL(string: "https://google.com")
webView.loadRequest(URLRequest(url: url!))
I used the same code with google URL and it is working.

WKWebView's (beta) takeSnapshot method, how to implement?

I noticed the WKWebView documentation now lists a method called takeSnapshot that is supported by iOS 11 and macOS 10.13 and above (Xcode 9 beta).
Has anyone played around with yet or implemented? I'm trying to get it working in a playground but I'm not sure where to start? Is this a method on a WKWebView?
My code:
import UIKit
import PlaygroundSupport
import WebKit
let frame = CGRect(x: 0, y: 0, width: 800, height:600)
let web = WKWebView(frame: frame)
let rq = URLRequest(url: NSURL(string: "http://apple.com")! as URL)
web.load(rq)
PlaygroundPage.current.liveView = web
PlaygroundPage.current.needsIndefiniteExecution = true
//Take snapshot?
As far as I tested, the method takeSnapshot(with:completionHandler:) actually exists as an instance method and works as expected.
Just that it's a little hard to use it.
The method declares its first parameter as WKSnapshotConfiguration?, but the class WKSnapshotConfiguration is not imported with import WebKit. You can pass nil to the parameter, but to use the method, you need to import the type WKSnapshotConfiguration. And I could not have found any dependent submodules to import WKSnapshotConfiguration.
So, if you want to play with this new feature, you need to create an App project with bridging-header.
(If you know how to use bridging-header in the Playground, you may test this feature in it. But I do not know if you can or how.)
{ProjectName}-Bridging-Header.h:
#import CoreGraphics;
#import <WebKit/WKSnapshotConfiguration.h>
And an example of ViewController.swift:
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet weak var webView: WKWebView!
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let request = URLRequest(url: URL(string: "http://apple.com")!)
webView.load(request)
}
#IBAction func buttonPressed(_ sender: UIButton) {
webView.takeSnapshot(with: nil) {image, error in
if let image = image {
self.imageView.image = image
print("Got snapshot")
} else {
print("Failed taking snapshot: \(error?.localizedDescription ?? "--")")
}
}
}
}
(Place a WKWebView, a UIImageView and a UIButton on a View.)
One more, this seems to be a bug of WebKit framework, you should better send a bug report to Apple.

How to setup EVURLCache in iOS Swift 3

I create a app for loading a website. It loads fine but it takes 2 to 3 min every time. So I choose to implement EVURLCache.
I use the pod for EVURLCache. And I import that in MainVC.But I don't know how to setup it.
my Aim is to reduce the load time one time it loaded.
GitHub of EVURLCache : https://github.com/evermeer/EVURLCache
MainVC
import UIKit
import EVURLCache
class MainVC: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
activityIndicator.startAnimating()
webView.delegate = self
let BASE_URL = "http://www.xeoscript.com/"
let url = URL(string: BASE_URL)
let request = URLRequest(url: url!)
webView.loadRequest(request)
}
func webViewDidFinishLoad(_ webView: UIWebView) {
activityIndicator.stopAnimating()
}
}
As you can see in the appdelegate of the demo, it's enough to add the following line in your didFinishLaunchingWithOptions.
EVURLCache.activate()
See the demo appdelegate for the available configuration options.
If you then run the app, then all your content will be downloaded to a Cache folder in your documents folder. The next time you start the app the files will be loaded from there. It's also possible to include that cache folder inside your app so that people will have the data available directly after downloading your app. The only thin you have to do for this is copying the content from the Cache folder to a PreCache folder in the root of your application.
See the demo app to see how it is setup.

ArcGIS iOS Map does not pan or zoom

I am walking through the basic iOS guide to add a map to my App here: https://developers.arcgis.com/ios/10-2/swift/guide/develop-your-first-map-app.htm
When running the app, it displays the basemap I have added but it does not seem to respond to any actions so I can not pane/zoom.
Here is my exact Swift controller code:
import UIKit
import ArcGIS
class ViewController: UIViewController, AGSMapViewLayerDelegate {
#IBOutlet var mapView: AGSMapView!
override func viewDidLoad() {
super.viewDidLoad()
let tiledLayer = AGSLocalTiledLayer(name: "Norfolk")
self.mapView.addMapLayer(tiledLayer, withName: "Norfolk")
mapView.locationDisplay.startDataSource()
}
}
I have tested on both an iPad and iPhone and the behavior is the same.
I am using ArcGIS 10.2.5
Where is your map url?
let url = NSURL(string: "http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer")
let tiledLayer = AGSTiledMapServiceLayer(URL: url)
self.mapView.addMapLayer(tiledLayer, withName: "Basemap Tiled Layer")
And have you added Privacy - Location When In Use Usage Description into info.plist of your app? You should do that if you call
mapView.locationDisplay.startDataSource()
I am not sure what I did incorrectly the first time, but I removed the view and re added it and it worked the 2nd time.

Resources