Remove website Top Navigation Bar in a WKWebView - ios

I am trying to show a website through a WKWebView.
My problem is that I don't want to show the navigationBar of the WebSite.
It looks like I have to use webview.evaluateJavaScript but is there an other way of doing it. If NO, can you provide an example with webview.evaluateJavaScript
Thanks

Looking a bit online, I found that on the particular website you can check for navigator.userAgent.
Then I could use the following that helped me remove the content that I want
webView.evaluateJavaScript("navigator.userAgent") { [weak webView] (result, error) in
if let webView = webView, let userAgent = result as? String {
webView.customUserAgent = userAgent + "/_app_"
}
}

Related

Programmatically scroll pdf in UIDocumentInteractionController

I'm showing a pdf file inside a UIDocumentInteractionController, like this:
let docController = UIDocumentInteractionController(url: documentsURL)
let url = NSURL(string:"itms-books:");
if UIApplication.shared.canOpenURL(url! as URL) {
docController.delegate = self
docController.presentPreview(animated: true)
}
I need to automatically scroll to the last page when the controller is shown, is there a way to do that? I didn't find one. Thanks to anyone who will send help!
I am afraid there is not a straight forward solution for this, even the documentation says it's a UIViewController but Xcode shows it is inherited from NSObject.
I'd recommend either rendering the PDF on a WKWebView or a UIScrollView. This will give you a room to wiggle.

How to use my view controllers and other class in the Share Extension ? iOS | Swift 4

I am creating a chatting application. User can share the images from other application to my application. I have added Share Extension to show my app in the native share app list. I'm also getting the selected data in didSelectPost Method. From here I want to show the list of the users to whom the image can be forwarded. For this, I'm using an already created view controller in the main app target.
override func didSelectPost() {
// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
if let content = self.extensionContext!.inputItems[0] as? NSExtensionItem {
let contentType = kUTTypeImage as String
// Verify the provider is valid
if let contents = content.attachments as? [NSItemProvider] {
for attachment in contents {
if attachment.hasItemConformingToTypeIdentifier(contentType) {
attachment.loadItem(forTypeIdentifier: contentType, options: nil) { (data, error) in
let url = data as! URL
let imageData = try! Data(contentsOf: url)
// Here I'm navigating to my viewcontroller, let's say: ForwardVC
}
}
}
}
}
I don't want to recreate the same screen in Share Extension. Apart from this view controllers, I have many more classes and wrappers that I want to use within the share extension. Like, SocketManager, Webservices, etc. Please suggest me your approach to achieve the same.
P.S.: I've tried setting multiple targets to required viewControllers and using same pods for Share Extention. In this approach, I'm facing a lot of issues as many of the methods and pods are not extention compliant. Also, is it the right way to do this.

Linkedin sharing popup in ios swift

I am not able to find popup for sharing link on LinkedIn.
I got REST API and also called successfully.
let url: String = "https://api.linkedin.com/v1/people/~/shares?format=json"
let payloadStr: String = "{\"comment\":\"I_SHARE_EXXO5__llEO_0009099\",\"visibility\":{\"code\":\"anyone\"}}"
let payloadData = payloadStr.data(using: String.Encoding.utf8)
LISDKAPIHelper.sharedInstance().postRequest(url, body: payloadData, success: { (response) in
Do i need to add custom popup from my side in app?
LinkedIn is not providing any dialog or popup for sharing text so if you need to show pop up you have to design your custom popup.

How to hide the keyboard assistant bar

I'm working on an iPad app, and I am unable to hide the UIKeyboardAssistantBar, the bar shown above the soft keyboard, with text prediction etc. See picture below, which shows the full keyboard, just to give a reference - the bar I want to hide is above the keyboard (the one displaying "2")
The problem I have is when an external keyboard is used: the soft keyboard is not shown when a text view obtains the focus, but that assistant bar is always shown instead - the only way I've found so far is to let the user manually hide it, using the icon at the far right.
Ideally, the solution I'm looking for is a global call that enables or disables that, so that I don't have to handle that individually for each text view.
Any idea?
There is a trick that you can try. Here is the code:
let item = self.yourTextView.inputAssistantItem;
item.leadingBarButtonGroups = [];
item.trailingBarButtonGroups = [];
The accepted solution will hide the leading and trailing BarButtonGroups on the keyboard, however unfortunately it will not hide the suggestion/auto correct bar (the center buttons with the suggested "2".
My need was for an iPad native app that required an HTML login page using a WKWebView to render the HTML login page. To hide the suggestion buttons, I used some injected javascript because I had no control over the HTML login page. Swift 3 code below creates the WKWebView (replaces the view object and injects the userScript into the page):
var webView: WKWebView!
override func loadView() {
let autocorrectJavaScript = "var inputTextElement = document.getElementById('userName');"
+ " if (inputTextElement != null) {"
+ " var autocorrectAttribute = document.createAttribute('autocorrect');"
+ " autocorrectAttribute.value = 'off';"
+ " inputTextElement.setAttributeNode(autocorrectAttribute);"
+ " }"
let userScript = WKUserScript(source: autocorrectJavaScript, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
let webConfiguration = WKWebViewConfiguration()
webConfiguration.userContentController.addUserScript(userScript)
webView = WKWebView(frame: .zero, configuration: webConfiguration)
view = webView
}
Actually, here is a method that works, even with contenteditable enabled:
func findKeyboardAssistantView() -> UIView? {
let result: UIView? = nil
let windows = UIApplication.shared.windows
let prefixes = [
"<UIInputSetContainerView",
"<UIInputSetHostView",
"<_UIKBCompatInputView",
"<UIKeyboardAutomatic",
"<UIKeyboardImpl",
]
for window in windows {
if window.description.hasPrefix("<UIRemoteKeyboardWindow") {
var last = window.subviews
for p in prefixes {
for s in last {
if s.description.hasPrefix(p) {
last = s.subviews
}
}
}
for s in last {
if s.description.hasPrefix("<UIKeyboardAssistantBar") {
return s
}
}
break
}
}
return result
}
findKeyboardAssistantView()?.isHidden = true
Note that it has to be fired when UIResponder.keyboardWillShowNotification is sent

Scale UIWebView content

I would like to ask you about a problem that I'm facing with a project. The problem is that i'm trying to show a UIWebView control, I'm loading a video streaming into the web view , this is the code:
if(self.cameraUrl != nil) {
let url = NSURL (string: self.cameraUrl!)
let requestObj = NSURLRequest(URL: url!)
self.view_webvideo.loadRequest(requestObj)
self.view_webvideo.contentMode = UIViewContentMode.Center;
let zoom = self.view_webvideo.bounds.size.width / self.view_webvideo.scrollView.contentSize.width
self.view_webvideo.scrollView.setZoomScale(zoom, animated: true)
}
As you could see in the code I had research about that and try a lot of things but i can't scale the content, the video have width=360 and height=640, I can't change the video size because it is a third party service.
Also i'm using the property: Scales Page To Fit = true
I want the video scale to fix into the webview component and don't show any scroll
I hope somebody could help me with that.
Thank you in advanced
Try replacing the last three lines with this (similar to what manman said)
self.view_webvideo.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('video')[0].style.width='100%'")

Resources