UIWebView not loading URL in simulator - ios

I've been having trouble getting the UIWebView to load a specific url page, after running the app through simulator for the first time everything seems to work the url loads just fine. But since i've turned off my mac and tried running the app again through simulator it's not showing the set page.
I know the code that i'm using works because it was working the first time and i did not make any changes to it.
If its not the piece of coding i'm using then what could it be? I've read all the post that is in stackoverflow in regards to URL not loading in UIWebView tried everyones suggestion on those posts and still i could not seem to get the page to load.
here is what i've done, again i ran the very same code the first time and it worked but now it just sits on blank white page.
import UIKit
class infoViewController: UIViewController {
#IBOutlet var info: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
var url = NSURL(string: "http://iglesianicristo.net/directory/Australia%20West/Darwin/44844")
var request = NSURLRequest(URL:url!)
info.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

Related

Live Admob Ads have suddenly stopped displaying in my app

In June, Admob adverts worked perfectly and AdMob sent a letter with a verification PIN for me to verify my identity & payment details.
Around the beginning of July, nearly all live ads stopped displaying in my app. I am still making the same number of requests, but impressions are so low I have dropped to £0.00/£0.01 a day. All test ads work correctly.
This issue began around the time I renamed my app (only on the app store display), however; all links to my app in my AdMob account are correct so the name change appears to have made no difference on their front-end UI.
When I debug my app, I get a list of warnings in the output section:
[I-ACS025031] AdMob App ID changed. Original, new: (nil), AppId
My 'GADApplicationIdentifier' value in my info.plist is the same as the 'new' app id.
[I-ACS013003] User property name must start with a letter: _ap
I am not setting any user properties, no idea what this means.
What have I tried?
Setting up new ad units.
Reverting back to an older version of the app.
Contacted AdMob 'support' via a form. They told me my ad serving is being limited. They did not say for how long and it has been around 2/3 weeks (by 'limited', I don't think they meant completely stopped).
Checked for policy violations in my account; nothing is there.
Code I use to display ads:
I have created an 'AdMobDisplayer' class that allows me to set up and display ads; this is called by each view controller. For example, my banner ads code:
View Controller:
let adMobDisplayer = AdMobDisplayer()
#IBOutlet weak var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
self.bannerView = self.adMobDisplayer.setupAdBannerView(self.bannerView, viewController: self, adUnitId: Constants.timerTabBannerAdId)
self.adMobDisplayer.displayBannerAd(self.bannerView)
}
AdMobDisplayer:
func setupAdBannerView(_ bannerView: GADBannerView, viewController: UIViewController, adUnitId: String, bannerViewDelgate: GADBannerViewDelegate? = nil) -> GADBannerView {
if(checkIfAdsAreDisabled()) {
return bannerView
}
/// Creates a new GADBannerView to be displayed in a view controller
bannerView.adUnitID = adUnitId
/// bannerView.adUnitID = Constants.testBannerAdId
bannerView.rootViewController = viewController
if let delegate = bannerViewDelgate {
bannerView.delegate = delegate
}
return bannerView
}
func displayBannerAd(_ bannerView: GADBannerView) {
if(checkIfAdsAreDisabled()) {
return
}
///Creates a request and loads an advert from AdMob
let request = GADRequest()
request.testDevices = [ "My Device Id" ]
bannerView.load(request)
}
This should display a banner ad in the view. It worked when I first added adverts in, it works for test adverts, but intermittently/rarely for live adverts now.
Find the full application on my GitHub: https://github.com/AlexMarchant98/KeGal-Trainer
Thanks in advance for any help!
I recently worked on GADBannerView in my last app and had almost similar issue, in your case you may need to generate Admob ad id, from their website.
So I fixed this ages ago, but, my fix was to add the required 'NSAppTransportSecurity' keys into my info.plist.
https://developers.google.com/admob/ios/app-transport-security

WKWebView does not open https://itunes.apple.com/app/id123456 link

Here is a little problem I just noticed in one of my iOS apps.
I use Xcode Version 10.1 and Swift 4.2.
The app has a button which when pushed brings up a view controller, this VC is in charge of opening a link to the app itself in itunes. I have done this many times with no problems in the past.
But this time, a blank pages opens up and nothing else. I have tried to replace only the URL I am interested in by "https://www.google.com/" and it works perfectly as expected (i.e. the Google page shows up). Of course I have verified that my URL is correct.
Can anybody see what could be the issue?
Thanks in advance for any relevant tip.
Here is the code for the whole view controller.
import UIKit
import WebKit
class appStore_ViewController: UIViewController, WKNavigationDelegate {
let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let appStoreURL = URL(string: "https://itunes.apple.com/app/id\ (appDelegate.applicationID)?mt=8")
//let appStoreURL = URL(string: "https://www.google.com/") // This works as expected.
print("The link: \(appStoreURL!.absoluteString)") // This shows what is expected (a working URL).
webView.load(URLRequest(url: appStoreURL!))
}
}
You should see what happen while visiting the AppStore Url with safari browser in your iPhone. then you will find the it still show blank page but show an alert to open the Appstore.
If you want to do the same function in your app with webView. you have to handle the alert the by yourself.
You need to implement the decidePolicyForNavigationAction method of WKNavigationDelegate for this to work.
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)
(WKNavigationActionPolicy))decisionHandler {
/* Code to handle other schemes such as file, https, http, etc. if required
*/
if ([[[navigationAction.request URL] scheme] isEqual:#"itms-appss"])
{
[[UIApplication sharedApplication] openURL:[navigationAction.request URL]];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
WKWebview does not inherently support different schemes such as "itms-appss" and you need to check and implement the url scheme you want to support.
You also need to check if you are running in iPadOS and set WKWebpagePreference to use mobile content mode if you do so.

Clear Cache in a web view Swift 3.3

New to programming, learning from Youtube and from You Guys ! Thanks a lot.
After intentionally entering wrong credentials on the webpage login inside the web view , the webpage normally shows an error of login. I added a reset button on the app interface to reload the MAIN WebApp web view.
It reloads and shows the same webpage login error. (instead of reloading the main webpage cache free to allow entering the proper credentials again)
I tried to add clear cache function with no success.
#IBAction func RefreshApp(_ sender: UIButton) {
viewdidload()
}
In viewdidload, i have included :
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
with no success
This is working in Swift 3.1 , XCode 8.3.1 :
//Delete Cookies
if let cookies = HTTPCookieStorage.shared.cookies {
for cookie in cookies {
NSLog("\(cookie)")
}
}
let storage = HTTPCookieStorage.shared
for cookie in storage.cookies! {
storage.deleteCookie(cookie)
}

WebView shows blank white screen part from apple.com

I have added a web view to my app however i keep getting a blank white screen when running on simulator or even device. When i enter apple.com as the website it works perfect but for any other website it just shows a blank white screen.
import UIKit
class ViewController: UIViewController {
#IBOutlet var WebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
var URL = NSURL(string: "http://www.apple.com")
WebView.loadRequest(NSURLRequest(URL: URL!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Have you added autolayout to your webView? As the image below shows?
Just select your webView and click on Add New Constraints and set your constraints.
If this is not your issue, check the output window if it blocks requests that are not HTTPS, then you can add this code to your info.plist code.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Update
You´re getting the white screen because you´re missing the http:// in your URL.
In Plist of the app give App Transport Security Settings,Give dictionary and then under it Allow Arbitrary Loads And set bool to YES, as in iOS 9 you have to give https request, Or simply add https with url, it will work

UIWebViewDelegate: webViewDidFinishLoad not called during in-page navigation

I have an app where users can navigate a pile of locally stored HTML files. I have a UIWebView, configured up correctly with a UIWebViewDelegate. Usually, when the user follows a link, shouldStartLoadWithRequest is called, followed by webViewDidFinishLoad a bit later.
But, if the link is pointing to an anchor on the same page as the one which is currently displayed, only shouldStartLoadWithRequest is called. webViewDidFinishLoad does not fire.
In a sense, I see that this might be expected behaviour, because in-page navigation should not require a page reload. However, I really need a place to hook into the call stack after in-page navigation is complete. The optimal solution would let me know when any sort of navigation has ended, both from another page, in-page and forward/backward actions.
My best hackaround so far has been to call performSelector: withObject: afterDelay: at the end of my shouldStartLoadWithRequest method, but I'm not happy with this.
Does anyone know how I can solve this correctly? Any insight appreciated!
You can try to use NSURLConnectionDataDelegate, it allows you to handle incoming data. Maybe you can determine if the page is loaded manually by adding a sign to your html files.
NSURLConnectionDataDelegate Reference
Edit: gist.github.com/buranmert/7304047 I wrote a piece of code and it worked, that may not be the way you wanted it to work but maybe it helps. Every time user clicks a URL with anchor, it creates another connection and as connection finishes loading web view loads the data, that marks the point where web view finished loading the page. As you use only local html files, I do not think creating connections will create problems
What you are describing is intended behavior. Just as AJAX or resource requests are never passed to the delegate, only root page changes will ever hit webViewDidFinishLoad:. But I have good news, and it doesn't involve saving a bunch of money on car insurance.
Loads performed within an iFrame DO trigger the full delegate methods and this gives you a solution. You can use this mechanism to post a notification to the native code, just as is often done for console.log() as described in this post. Alternatively, Native Bridge would work well to call into your Objective C code from JavaScript.
Just check weather u got the delegate function DidStartLoading if it is called no doubt that DidFinish also should get called
Are you sure your shouldStartLoadWithRequest always returns an YES???
I always add
return YES;
at the end of shouldStartLoadWithRequest implementation.And that works for me.
By returning YES, it denotes that the webview has loaded and would call the webViewDidFinishLoad
if([webView isLoading]==1)
{
//your webview is loading
}
else
{
//your webview has loaded
}
Here is a Swift Implementation of Mert Buran's code incase anybody is looking for it. (Although NSURLConnection is deprecated as of iOS 9)
But it does not solve my problem. When i click on a jquery link that popups a video, it does not fire the webViewDidFinishLoad.
class WebViewController: UIViewController, UIWebViewDelegate, NSURLConnectionDelegate {
var menuURL: String?
var response: NSURLResponse?
var data = NSData()
// MARK: Properties
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
// From Web
let url = NSURL (string: menuURL!)
let urlRequest = NSURLRequest(URL: url!)
let connection = NSURLConnection(request: urlRequest, delegate: self)
self.data = NSData()
connection!.start()
// if this is false, page will be 'zoomed in' to normal size
webView.scalesPageToFit = false
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .LinkClicked && request.URL!.fragment != nil {
let connection = NSURLConnection(request: request, delegate: self)
connection!.start()
return false
}
return true
}
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
self.response = response
}
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
let oldData = NSMutableData(data: self.data)
oldData.appendData(data)
self.data = oldData
}
func connectionDidFinishLoading(connection: NSURLConnection) {
self.webView.loadData(self.data, MIMEType: self.response!.MIMEType!, textEncodingName: "utf-8", baseURL: self.response!.URL!)
self.data = NSData()
}
}

Resources