12、xamarin form中實現H5 網頁喚醒微信支付的方法
阿新 • • 發佈:2018-05-22
dstar set renderer AC adf app led int 安卓
在微信的支付中有種支付叫微信H5支付。方便用戶在網頁中輕松喚起微信進行支付。
當然微信不推薦大家使用這樣的方式喚起微信支付。建議app還是使用正常的微信支付sdk即可
服務端與其他的建議參考微信支付官網進行適配我這裏只討論如何在xamarin forms 中嵌入Html5 實現支付
這裏面有個問題就是如果沒有安裝微信app 是需要進行一些判斷的
開始建立相關的代碼
打開 MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:WeChatPayDemo" x:Class="WeChatPayDemo.MainPage"> <StackLayout> <WebView x:Name="wb" IsVisible="True" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> </WebView> </StackLayout> </ContentPage>
public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); } protected override void OnAppearing() {base.OnAppearing(); wb.Source = new UrlWebViewSource { Url = "http://wxpay.wxutil.com/mch/pay/h5.v2.php" }; } }
然後安卓項目裏面增加
WebRenderer.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Webkit; using Android.Widget; using Xamarin.Forms; using Xamarin.Forms.Internals; using Xamarin.Forms.Platform.Android; using AWebView = Android.Webkit.WebView; [assembly: Xamarin.Forms.ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(WeChatPayDemo.Droid.WebRenderer))] namespace WeChatPayDemo.Droid { public class WebRenderer : ViewRenderer<Xamarin.Forms.WebView, AWebView> { Context _context; public WebRenderer(Context context) : base(context) { _context = context; } protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e) { base.OnElementChanged(e); if (Control == null) { var webView = new AWebView(_context); webView.SetWebViewClient(new WebViewClient(_context)); webView.Settings.JavaScriptEnabled = true; SetNativeControl(webView); } } protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); if (Control != null && e.PropertyName == "Source") { var src = Element.Source as UrlWebViewSource; Control.LoadUrl(src.Url); } } } public class WebViewClient : Android.Webkit.WebViewClient { private Context _context; public WebViewClient(Context context) { _context = context; } public override bool ShouldOverrideUrlLoading(AWebView view, IWebResourceRequest request) { if (request.Url.Scheme == "weixin") { Intent intent = new Intent() { }; intent.SetAction(Intent.ActionView); intent.SetData(request.Url); this._context.StartActivity(intent); return true; } return base.ShouldOverrideUrlLoading(view, request); } [Obsolete] public override bool ShouldOverrideUrlLoading(AWebView view, string url) { if (url.StartsWith("weixin")) { Intent intent = new Intent() { }; intent.SetAction(Intent.ActionView); intent.SetData(Android.Net.Uri.Parse(url)); this._context.StartActivity(intent); return true; } return base.ShouldOverrideUrlLoading(view, url); } } }
在iOS裏面增加
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Foundation; using UIKit; using Xamarin.Forms.Platform.iOS; [assembly: Xamarin.Forms.ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(WeChatPayDemo.iOS.WebRenderer))] namespace WeChatPayDemo.iOS { public class WebRenderer : WebViewRenderer { protected override void OnElementChanged(VisualElementChangedEventArgs e) { base.OnElementChanged(e); if (NativeView != null && e.NewElement != null) SetupControlSettings(); } private void SetupControlSettings() { var webView = ((UIWebView)NativeView); if (webView == null) return; webView.Delegate = new CustomWebDelegate(); } } public class CustomWebDelegate : UIWebViewDelegate { public override bool ShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType) { if (request.Url.Scheme== "weixin") { UIApplication.SharedApplication.OpenUrl(request.Url); } return true; } public override void LoadingFinished(UIWebView webView) { } public override void LoadFailed(UIWebView webView, NSError error) { } } }
記得配置 iOS 的網絡訪問權限 和安卓的訪問權限 至此 web裏面喚起 微信支付完成
12、xamarin form中實現H5 網頁喚醒微信支付的方法