Change WebView url from AppDelegate
You are force unwrapping url
which is not a valid URL. I would suggest adding a guard
statement to prevent the crash if a invalid URL is created:
func load_url(server_url: String) {
guard let url = URL(string: server_url) else {
print("Invalid URL")
return
}
let request = URLRequest(url: url)
my_web_view.loadRequest(request)
}
As you are obtaining the URL in the AppDelegate
you cannot simply update the UIWebView
from this class. You will need to call a function in the my_web_view
's parent class which updates the URL
.
// App Delegate
var serverURL: String?
func load_url(server_url: String) {
serverURL = server_url
let notificationName = Notification.Name("updateWebView")
NotificationCenter.default.post(name: notificationName, object: nil)
}
// View Controller
override func viewDidLoad() {
let notificationName = Notification.Name("updateWebView")
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.updateWebView), name: notificationName, object: nil)
updateWebView()
}
func updateWebView() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let serverURL = appDelegate.serverURL
guard let url = URL(string: serverURL) else {
print("Invalid URL")
return
}
let request = URLRequest(url: URL)
my_web_view.loadRequest(request)
}
How to control uiwebview from appdelegate.swift
Okay, I discovered to use local notification to solve this problem.
I changed the code is like this :
AppDelegate.swift:
NSNotificationCenter.defaultCenter().postNotificationName("applicationWillResignActive", object: nil)
added this line to the "viewDidLoad" func in ViewController.swift :
NSNotificationCenter.defaultCenter().addObserver(self, selector: "funcNameToFire:", name:"applicationWillResignActive", object: nil)
and added this function somewhere into the ViewController.swift:
func funcNameToFire(notification: NSNotification){
let url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("bar", ofType: ".html")!)!
let request = NSURLRequest(URL: url)
webView.loadRequest(request)
}
Can't Change URL in WebView
In the appDelegate, instead of doing:
ViewController* myScript = [[ViewController alloc] init];
[myScript setWebViewURL:@"http://google.com"];
Here you are creating a new instance of your view controller and by this way your webView will be nil, hence you are not seeing the expected result.
You should post a notification in app delegate:
[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshWebView" object:nil];
and in your viewController, listen to this notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadWebView) name:@"refreshWebView" object:nil];
and in your view controller, define method:
-(void)reloadWebView{
NSString *urlAddress = @"your-url";
NSURL *essaUrl = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:essaUrl];
[self.myWebView loadRequest:requestObj];
}
Related Topics
Monitoring App Switching on Os X
Avcapturevideodataoutput Captureoutput Not Being Called
Swift 2.0 'Inout' Function Parameters and Computed Properties
Swift Switch Statement Considered All Cases of Int, But Compiler Still Display Error
Using Delegates on Generic Protocol
Swift: Set Insertion Point in Nstextfield
Converting Url to String and Back Again
Swift: How to Reload Row Height in Uitableviewcell Without Reloading Data
Swipe Animation for Remove Cell in Uicollectionview - Swift 2.0
Programmatically Disabling Screenshot in App
Use Multiple Codingkeys for a Single Property
Using Applescript with Apple Events in MACos - Script Not Working
How to Rotate Sprites Around a Joint
Nstimer.Scheduledtimerwithtimeinterval in Swift Playground
Find Multiple Quoted Words in a String with Regex