xamarin.ios - Implement error handling and script messages receiving in one WebViewRenderer in Xamarin IOs -
now have code web view renderer like:
public sealed class mywebviewrenderer : viewrenderer<webview, wkwebview>, iwkscriptmessagehandler { private const string scriptmessagehandlername = "native"; private wkusercontentcontroller usercontroller; private wkwebview m_nativeview; protected override void onelementchanged(elementchangedeventargs<webview> e) { base.onelementchanged(e); var visualwebview = (imwebview)element; if (control == null && e.newelement != null) { this.usercontroller = new wkusercontentcontroller(); var config = new wkwebviewconfiguration { usercontentcontroller = this.usercontroller }; this.usercontroller.addscriptmessagehandler(this, scriptmessagehandlername); m_nativeview = new wkwebview(this.frame, config) { weaknavigationdelegate = new imnavigationdelegate()}; setnativecontrol(m_nativeview); } public void didreceivescriptmessage(wkusercontentcontroller usercontentcontroller, wkscriptmessage message) { ... } }
this class allows me receive messages js. want receive message page loading failed. i've found way - use xamarin.forms.platform.ios.webviewrenderer
base class renderer , use
public sealed class mywebviewrenderer : xamarin.forms.platform.ios.webviewrenderer { protected override void onelementchanged(visualelementchangedeventargs e) { base.onelementchanged(e); var visualwebview = (imwebview)element; if (this.viewcontroller == null && e.newelement != null) { base.delegate = new customnavigationdelegate() { control = visualwebview }; } ... } public class customnavigationdelegate : uiwebviewdelegate { public imwebview control { get; set; } public override void loadfailed(uiwebview webview, nserror error) { ... } } }
load failing capture working well, code couldn't messages js. there way both loadfailed event , messages js code in 1 class?
i recommend continue using renderer inheriting wkwebview
. can use didfailnavigation
virtual method in wknavigationdelegate
callbacks when there error while navigating. can use below
public sealed class mywebviewrenderer : viewrenderer<webview, wkwebview>, iwkscriptmessagehandler { private const string scriptmessagehandlername = "native"; private wkusercontentcontroller usercontroller; private wkwebview m_nativeview; protected override void onelementchanged(elementchangedeventargs<webview> e) { base.onelementchanged(e); var visualwebview = (imwebview)element; if (control == null && e.newelement != null) { this.usercontroller = new wkusercontentcontroller(); var config = new wkwebviewconfiguration { usercontentcontroller = this.usercontroller }; this.usercontroller.addscriptmessagehandler(this, scriptmessagehandlername); m_nativeview = new wkwebview(this.frame, config) { weaknavigationdelegate = new navigationdelegate()}; setnativecontrol(m_nativeview); } } public void didreceivescriptmessage(wkusercontentcontroller usercontentcontroller, wkscriptmessage message) { } } public class navigationdelegate : wknavigationdelegate { public override void didfailnavigation( wkwebview webview, wknavigation navigation, nserror error ) { // if navigation fails, gets called } }
Comments
Post a Comment