Integrate web text into App (Swift) - ios

I am having a news section in the App and i want to renew it through a website that shows the text and pictures it should copy how do i do that?
I just want that it exactly copies the text and pictures from a website.
Thanks in advance.

You can use UIWebView for that! First, drop a web view on the scene and then:
Here is the code for SWIFT 2:
import UIKit
class ViewController: UIViewController{
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad(){
super.viewDidLoad()
let url = NSURL(string: "http://www.google.com") // instead of "http://www.google.com", you put the website link you want, but with "http://" or "https://"
let urlRequest = NSURLRequest(URL: url!)
webView.loadRequest(urlRequest)
}
}
Version For SWIFT 3:
import UIKit
class ViewController: UIViewController, UIWebViewDelegate{
#IBOutlet var webView: UIWebView!
override func viewDidLoad(){
super.viewDidLoad()
let url = URL(string: "http://www.booking.com") // instead of "http://www.google.com", you put the website link you want, but with "http://" or "https://"
let urlRequest = URLRequest(url: url!)
webView.loadRequest(urlRequest)
}
}

So at the end you need to implement rss reader if your website supports.
https://www.raywenderlich.com/2636/rss-reader-tutorial-for-ios-how-to-make-a-simple-rss-reader-iphone-app
If website does not support then you could implement below ways.
Open that URL in UIWebView or Copy though each text and image on your server and make API to display on app.

Related

WKWebView is not getting intialized in swift 4 xcode 10

Could anyone help me in understanding why my webView is not getting initialized here. I am getting the following error because webView is nil.
Fatal error: Unexpectedly found nil while unwrapping an Optional value
What exactly I am missing here?
import UIKit
import WebKit
class MemoriesViewController: UIViewController, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear( animated )
let urlString:String = "https://www.apple.com"
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
webView.load(urlRequest)
}
}
}
Screenshot for better clarity where the error is actually happening:
Add Webkit.Frameworks to Linked Framework and Libraries
You can then add the Outlet for WKWebView and use your code
import UIKit
import WebKit
class MyWebViewController: UIViewController {
#IBOutlet weak var myWV: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let urlString:String = "https://www.apple.com"
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
myWV.load(urlRequest)
}
}
In Xcode 10, you can foll bellow the step:
Click the project
Go to build phase
Go to Link Binary with Libraries
Click add plus sing under Link Binary with Libraries
open a windows & search Webkit.framework
add & enjoy it
Show bellow image
The problem was my output was not connected to the storyboard. That's why #IBOutlet was not getting initialized.

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

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?

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"

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.

Modify URL Before Loading Request in UIWebView

I created a native app from my web app using UIWebView.
My webapp is a basic CMS with some static pages, some category pages and and some detail pages.
Now I need to check if the request comes from the web app or the native app.
To do this I have two possibilities:
First solution: Modify URL and check URL on server side:
#IBOutlet var WebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
loadWebPage()
}
func loadWebPage() {
let url = NSURL(string: "http://localhost:8888?apptype=native")
let request = NSURLRequest(URL: url!)
WebView.loadRequest(request)
}
When I check on server side if the the parameter exists on the first page it works, but when I switch to other pages (categories etc.) the condition fails.
Second solution: I tried to set cookies:
#IBOutlet var WebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
loadWebPage()
}
func loadWebPage() {
let url = NSURL(string: "http://localhost:8888")
let request = NSURLRequest(URL: url!)
var cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies as! [NSHTTPCookie]
var reqCookies:[AnyObject] = []
for aCookie in cookies {
reqCookies += [aCookie]
}
var headers = NSHTTPCookie.requestHeaderFieldsWithCookies(reqCookies)
WebView.loadRequest(request)
}
But how do I add cookies in this example in the response so I can retrieve them on server side?
Are there any other solutions or how can I modify my existing ones?
Implement the UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType:.
When the user taps a link in the webview, this delegate method is called. Take the request.url of NSURLRequest passed into the delegate method and create a new NSURLRequest with ?apptype=native appended to the request.url property.

Resources