12、xamarin form中实现H5 网页唤醒微信支付的方法
在微信的支付中有种支付叫微信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 网页唤醒微信支付的方法的更多相关文章
- 在Ubuntu 12.04系统中安装配置OpenCV 2.4.3的方法
在Ubuntu 12.04系统中安装配置OpenCV 2.4.3的方法 对于,在Linux系统下做图像识别,不像在windows下面我们可以利用Matlab中的图像工具箱来实现,我们必须借助Ope ...
- 前端如何在h5页面调用微信支付?
在微信服务号开发的时候经常会遇到微信支付的功能实现,通过实际经验自己总结了一下,前端在H5页面调起微信支付有两种办法,一是利用内置对象,二是通过引用微信的js sdk,亲测都能支付成功,从写法上来看用 ...
- 微信小程序web-view(webview) 嵌套H5页面 唤起微信支付的实现方案
场景:小程序页面有一个web-view组件,组件嵌套的H5页面,要唤起微信支付. 先讲一下我的项目,首先我是自己开发的一个H5触屏版的商城系统,里面含有购物车,订单支付等功能.然后刚开始,我们公众号里 ...
- H5网页唤醒app,判断app安装
在阅读本文之前你首先应该对js有基本对掌握,并且对Scheme,intent有一定的理解.更多的是代码 上午给朋友做了一个产品引导页,但是需要判断ios系统的TestFlight是否安装,进行了goo ...
- PHP中获取某个网页或文件内容的方法
1. 通过file_get_contents()函数$contents = file_get_contents('http://demo.com/index.php');echo $contents; ...
- h5网页在微信里打开 右上角分享到微信好友或者朋友圈
首先你需要一个分享接口地址,然后在自定义图片 标题 描述 如下: <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js& ...
- h5调起微信支付
后台需要进行生成签名获取参数. 前台代码: function onBridgeReady(appId,timeStamp,nonceStr,package1,paySign,signType,open ...
- 【微信H5支付】微信公众号里H5网页点击调取微信支付
最近在公众号里开发了下单支付H5网页,需要在H5里调用微信支付界面.开发思路和代码整理如下: todo...
- [5] 微信公众号开发 - 微信支付功能开发(网页JSAPI调用)
1.微信支付的流程 如下三张手机截图,我们在微信网页端看到的支付,表面上看到的是 "点击支付按钮 - 弹出支付框 - 支付成功后出现提示页面",实际上的核心处理过程是: 点击支付按 ...
随机推荐
- 2018.07.08 NOIP模拟 第K小数(二分)
第K小数 题目背景 SOURCE:NOIP2016-AHSDFZ T1 题目描述 有两个正整数数列,元素个数分别为 N 和 M .从两个数列中分别任取一个数相乘,这样一共可以得到 N*M 个数,询问这 ...
- 2018.07.06 POJ2536 Gopher II(二分图匹配)
Gopher II Time Limit: 2000MS Memory Limit: 65536K Description The gopher family, having averted the ...
- cmake-include_directories
include_directories: Add include directories to the build. include_directories([AFTER|BEFORE] [SYSTE ...
- post异步请求
//创建url NSURL *url = [[NSURL alloc] initWithString:@"http://api.hudong.com/iphonexml.do"]; ...
- day04(权限修饰符,内部类,局部内部类,匿名内部类)
权限修饰符, Public >protected >default > private public 公共权限 随便都可以访问 protected 子类可以访问权限 (子类 ...
- (KMP 暴力)Corporate Identity -- hdu -- 2328
http://acm.hdu.edu.cn/showproblem.php?pid=2328 Corporate Identity Time Limit: 9000/3000 MS (Java/Oth ...
- hbase使用MapReduce操作4(实现将 HDFS 中的数据写入到 HBase 表中)
实现将 HDFS 中的数据写入到 HBase 表中 Runner类 package com.yjsj.hbase_mr2; import com.yjsj.hbase_mr2.ReadFruitFro ...
- LeetCode137:Single Number II
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
- 通过css使用background-color为背景图添加遮罩效果
一个div同时设置background-color和background-image的话,color是处于img层下方的,无法实现遮罩效果,所以需要再创建一个div作为其子div,然后设置子div的背 ...
- NLayerAppV3--基础结构层(Cross-Cutting部分)
回顾:NLayerAppV3是一个使用.net 2.1实现的经典DDD的分层架构的项目. NLayerAppV3是在NLayerAppV2的基础上,使用.net core2.1进行重新构建的:它包含了 ...