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.
Related
I have implemented the iOS Intune SDK in my app. The app applies the MAM policies and is working properly. I am trying to implement the Webview content restrictions using the IntuneMAMWebViewPolicyDelegate and isExternalURL as described here
The isExternalURL function is never called.
This is the view containing my WKWebView
import UIKit
import WebKit
import IntuneMAMSwift
class WebviewPage: UIViewController {
let webViewPolicyDelegage = WebViewPolicyClass.init();
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
IntuneMAMPolicyManager.instance().setWebViewPolicyDelegate(webViewPolicyDelegage, forWebViewer: webView!)
let webaddress = "https://www.google.com"
if let url = URL(string: webaddress) {
let urlRequest = URLRequest(url: url)
self.webView.load(urlRequest)
}
}
}
This is my delegate implementation
import IntuneMAMSwift
class WebViewPolicyClass: NSObject, IntuneMAMWebViewPolicyDelegate {
func isExternalURL(_ url: URL) -> Bool {
// TODO: Check if URL is external
return true
}
}
The app MAM policy is configured to require the managed Edge browser.
Any ideas why the delegate method is not called?
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"
My app has a basic webview controller to perform some operations. This view in the storyboard is not much besides a wrapper around a UIWebView. The controller itself has various public functions that can be called to load pages in the webview, like so:
class WebViewController: UIViewController, UIWebViewDelegate {
// MARK: Properties
#IBOutlet var webView: UIWebView!
// MARK: UIViewController
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
loadHomePage()
}
// MARK: Public
public func loadHomePage() {
navigateWebView(to: HOME_PAGE)
}
public func loadSettingsPage() {
navigateWebView(to: SETTINGS_PAGE)
}
public func loadSignOutPage() {
navigateWebView(to: SIGN_OUT_PAGE)
}
// MARK: Private
private func navigateWebView(to url: String) {
let request = URLRequest(url: URL(string: url)!)
webView.loadRequest(request)
}
I'm trying to write unit tests that verify that the proper URL is sent to the loadRequest function of the webview. Note that I don't actually care about loading the URL; this is just a unit test, so all I really want to test is that loadSettingsPage sends a URLRequest with the SETTINGS_PAGE URL to the webview to load, for example.
I tried something like this, with no success:
_ = webViewController.view // Calls viewDidLoad()
XCTAssertEqual(webViewController.webView.request?.url?.absoluteString, HOME_PAGE)
The value of the first part of the assertEqual was nil.
I assume I need to mock out the webView somehow but I'm not sure how to go about that. Any suggestions?
As a follow-up, I'd also like to be able to test when things like webView.reload() and webview.goBack() are called, so any pointers there would be appreciated as well. Thanks!
Scenario:
Creating a social network app type application, when user type the web address or valid url in the text area my app should identify the valid url,then I have to do the following tasks,
1) If it is a valid web url then automatically show the preview of the web contents in the preview section.
2) Share button should not be enabled until the preview is fully loaded, if not there are some chances that URL alone may be posted instead of the web contents.
What I have tried?
Created a Text view and a preview WebUI view, I used following code to identify the valid URL, if valid url then I am showing the web view and loading the web page.
Code what I tried:
class CommentsViewController: UIViewController {
#IBOutlet weak var comments: UITextField!
#IBOutlet weak var preview: UIWebView!
#IBOutlet weak var btnShare: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
preview.isHidden = true
btnShare.isEnabled = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func valueModified(_ sender: Any) {
//Load web preview only after Check for Valid Web URL
if verifyUrl(urlString: comments.text) == true {
preview.isHidden = false
preview.loadRequest(URLRequest(url: URL(string: comments.text!)!))
btnShare.isEnabled = true
}
}
//Check for Valid Web URL
func verifyUrl (urlString: String?) -> Bool {
//Check for nil
if let urlString = urlString {
// create NSURL instance
if let url = NSURL(string: urlString) {
// check if your application can open the NSURL instance
return UIApplication.shared.canOpenURL(url as URL)
}
}
return false
}
}
What I need exactly?
I do not want to load the web page, I have to load the preview of the page.
Share button should be hidden until the preview is fully loaded.
Screen shot of my code output:
Screen shot of what I am expecting:
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.