英文:
xamarin iOS webView post
问题
I created a custom webViewRenderer in Xamarin in this way.
This is the class in Xamarin project
public class PostWebView : WebView {
public static readonly BindableProperty PostUrlProperty =
BindableProperty.Create(nameof(PostUrl), typeof(string), typeof(PostWebView), string.Empty, BindingMode.OneWay);
public string PostUrl {
get { return (string)GetValue(PostUrlProperty); }
set { SetValue(PostUrlProperty, value); }
}
public byte[] PostData { get; set; } = new byte[0];
}
and this is the custom renderer in Android project
[assembly: ExportRenderer(typeof(PostWebView), typeof(PostWebViewRenderer))]
namespace AppWebView.Droid {
// Custom renderer on the native project
public class PostWebViewRenderer : WebViewRenderer {
public PostWebViewRenderer(Context ctx) : base(ctx) { }
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) {
if (e.PropertyName == "PostUrl") {
if (Control == null)
return;
var ctlWebView = (Android.Webkit.WebView)Control;
var view = Element as PostWebView;
ctlWebView.PostUrl(view.PostUrl, view.PostData);
}
}
}
}
So, now I'm trying to do the same in the iOS project
[assembly: ExportRenderer(typeof(WebView), typeof(AppWebView.iOS.PostWebViewRenderer))]
namespace AppWebView.iOS {
// Xamarin.Forms.Platform.iOS.WebViewRenderer
public class PostWebViewRenderer : ViewRenderer<PostWebView, WKWebView> {
private WKWebView wkWebView;
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) {
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == PostWebView.PostUrlProperty.PropertyName)
{
var aWebView = Element as PostWebView;
if (aWebView.PostUrl != "")
{
if (Control == null)
{
var config = new WKWebViewConfiguration();
wkWebView = new WKWebView(Frame, config);
SetNativeControl(wkWebView);
}
var request = new NSMutableUrlRequest(new NSUrl(new NSString(aWebView.PostUrl)));
// request.Body = NSData.FromData(NSData.FromArray(aWebView.PostData));
request.Body = NSData.FromString(aWebView.StringData);
request.HttpMethod = "POST";
// enable?...
// request["Content-Length"] = request.Body.Length.ToString();
// request["Content-Type"] = "application/x-www-form-urlencoded charset=utf-8";
Control.LoadRequest(request);
}
}
}
}
}
So, in XAML I put my own control
<local:PostWebView x:Name="postWebView1" Source="" HeightRequest="600" WidthRequest="100" />
in code-behind (it's only a test...) I create my request with parameters
byte[] byteParams = await GetParamsAsync();
postWebView1.PostData = byteParams;
postWebView1.StringData = HttpUtility.UrlEncode(byteParams);
postWebView1.PostUrl = "https://..../DispatcherServlet";
and this is GetParamsAsync
public async Task<byte[]> GetParamsAsync() {
var dataContent = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("a", "avalue"),
new KeyValuePair<string, string>("b", "bvalue"),
new KeyValuePair<string, string>("c", "cvalue"),
new KeyValuePair<string, string>("d", "dvalue")
});
var s = await dataContent.ReadAsByteArrayAsync();
return s;
}
In Android emulator all works fine using PostData property.
In iOS emulator the webView is showing the page but the way I "post" the parameters doesn't work... I tried in several different ways (such as a new property StringData) with no luck.... I don't understand how can I do...
英文:
I created a custom webViewRenderer in Xamarin in this way.
This is the class in Xamarin project
public class PostWebView : WebView {
public static readonly BindableProperty PostUrlProperty =
BindableProperty.Create(nameof(PostUrl), typeof(string), typeof(PostWebView), string.Empty, BindingMode.OneWay);
public string PostUrl {
get { return (string)GetValue(PostUrlProperty); }
set { SetValue(PostUrlProperty, value); }
}
public byte[] PostData { get; set; } = new byte[0];
}
and this is the custom renderer in Android project
[assembly: ExportRenderer(typeof(PostWebView), typeof(PostWebViewRenderer))]
namespace AppWebView.Droid {
//Custom renderer on the native project
public class PostWebViewRenderer : WebViewRenderer {
public PostWebViewRenderer(Context ctx) : base(ctx) { }
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) {
if (e.PropertyName == "PostUrl") {
if (Control == null)
return;
var ctlWebView = (Android.Webkit.WebView)Control;
var view = Element as PostWebView;
ctlWebView.PostUrl(view.PostUrl, view.PostData);
}
}
}
}
So, now I'm trying to do the same in the iOS project
[assembly: ExportRenderer(typeof(WebView), typeof(AppWebView.iOS.PostWebViewRenderer))]
namespace AppWebView.iOS {
// Xamarin.Forms.Platform.iOS.WebViewRenderer
public class PostWebViewRenderer : ViewRenderer<PostWebView, WKWebView> {
private WKWebView wkWebView;
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) {
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == PostWebView.PostUrlProperty.PropertyName)
{
var aWebView = Element as PostWebView;
if (aWebView.PostUrl != "")
{
if (Control == null)
{
var config = new WKWebViewConfiguration();
wkWebView = new WKWebView(Frame, config);
SetNativeControl(wkWebView);
}
var request = new NSMutableUrlRequest(new NSUrl(new NSString(aWebView.PostUrl)));
//request.Body = NSData.FromData(NSData.FromArray(aWebView.PostData));
request.Body = NSData.FromString(aWebView.StringData);
request.HttpMethod = "POST";
// enable?...
//request["Content-Length"] = request.Body.Length.ToString();
//request["Content-Type"] = "application/x-www-form-urlencoded charset=utf-8";
Control.LoadRequest(request);
}
}
}
}
}
So, in XAML I put my own control
<local:PostWebView x:Name="postWebView1" Source="" HeightRequest="600" WidthRequest="100" />
in code-behind (it's only a test...) I create my request with parameters
byte[] byteParams = await GetParamsAsync();
postWebView1.PostData = byteParams;
postWebView1.StringData = HttpUtility.UrlEncode(byteParams);
postWebView1.PostUrl = "https://..../DispatcherServlet";
and this is GetParamsAsync
public async Task<byte[]> GetParamsAsync() {
var dataContent = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("a", "avalue"),
new KeyValuePair<string, string>("b", "bvalue",
new KeyValuePair<string, string>("c", "cvalue"),
new KeyValuePair<string, string>("d", "dvalue")
});
var s = await dataContent.ReadAsByteArrayAsync();
return s;
}
In Android emulator all works fine using PostData property.
In iOS emulator the webView is showing the page but the the way I "post" the parameters doesn't work... I tried in several different way (such as a new property StringData) with no luck.... I don't understand how can I do...
答案1
得分: 1
最后我以这种方式解决了问题:
var request = new NSMutableUrlRequest(
new NSUrl(new NSString(aWebView.PostUrl))
);
request.Body = NSData.FromArray(aWebView.PostBytesData);
request.HttpMethod = "POST";
request["Content-Length"] = request.Body.Length.ToString();
request["Content-Type"] = "application/x-www-form-urlencoded charset=utf-8";
英文:
Finally I solved in this way
var request = new NSMutableUrlRequest(
new NSUrl(new NSString(aWebView.PostUrl))
);
request.Body = NSData.FromArray(aWebView.PostBytesData);
request.HttpMethod = "POST";
request["Content-Length"] = request.Body.Length.ToString();
request["Content-Type"] = "application/x-www-form-urlencoded charset=utf-8";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论