Search code examples
iosuiwebviewxamarin.ioslinkbutton

ASP.Net LinkButton and iOS UIWebView


I have found out that embedded UIWebView's do not cause postbacks if the button is a asp.net LinkButton, but they work fine with Button's. Safari causes postback as normal for LinkButton's.

Are there any workarounds to this?

Code snippet:

vwWebView.LoadRequest (new NSUrlRequest (new NSUrl (url)));

(we're using monotouch but this seems an iOS issue)


Solution

  • It turns out that the default user agent for the UIWebView was making IIS not send the javascript to the content items. The solution is to manually set the user agent of the webview to that of mobile safari.

    Here's the code that makes it happen:

    static public void SetUserAgent(UIWebView webview) {
        var agent = "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25";
        var webDocumentView = new NSObject(Messaging.IntPtr_objc_msgSend(webview.Handle,
                                                                         (new Selector("_documentView").Handle)));
        var webView = webDocumentView.GetNativeField("_webView");
        Messaging.void_objc_msgSend_IntPtr(webView.Handle, 
                                           (new Selector("setCustomUserAgent:")).Handle,                        
                                           (new NSString(agent)).Handle);
    }
    

    This post provided the solution: http://blog.reinforce-lab.com/2010/06/monotouchuiwebviewhowtochangeuseragent.html

    The following post doesn't work. It only cutomizes the first request, but not of susequent browsing: http://monotouch.2284126.n4.nabble.com/seting-UIWebview-user-agent-objectiveC-question-td3807602.html