首页 > 解决方案 > 如何从设备连接到 Stripe 服务器?

问题描述

我正在使用 Stripe 和启用了 sinatra 和 json 的 ruby​​ 脚本在 iOS 上对信用卡收费。收费适用于 iPhone 6 模拟器,但不适用于运行 9.3.5 的 iPad mini 设备。

我尝试将 baseURL 字符串更改为我的 ip 地址,以便应用程序可以连接到服务器htpp://192.168.1.11,但仍然出现错误。我什至尝试过类似的东西http://192.168.1.11:4567。如何让物理应用程序连接到服务器?

//original baseURL for the app
enum Constants {
...
static let baseURLString = "http://localhost:4567"
...
}

iPhone 6 模拟器

在此处输入图像描述

iPad mini 9.3.5 版

在此处输入图像描述

原始教程使用 Swift 4、iOS 11、Xcode 9,但我将部署目标和 podfile 更改为 iOS9。

platform :ios, '9.0'

target 'RWPuppies' do
use_frameworks!

pod 'Alamofire', '~> 4.5'
pod 'AlamofireImage', '~> 3.3'
pod 'Stripe'
pod 'Cards'


end

红宝石脚本

#1
require 'sinatra'
require 'stripe'
require 'json'

#2
Stripe.api_key = 'YOUR_TEST_SECRET_KEY'

#3
get '/' do
status 200
return "RWPuppies back end has been set up correctly"
end

 #4
post '/charge' do
#5
payload = params
if request.content_type.include? 'application/json' and params.empty?
payload = indifferent_params(JSON.parse(request.body.read))
end

begin
#6
charge = Stripe::Charge.create(
  :amount => payload[:amount],
  :currency => payload[:currency],
  :source => payload[:token],
  :description => payload[:description]
 )
#7
rescue Stripe::StripeError => e
status 402
return "Error creating charge: #{e.message}"
end
#8
status 200
return "Charge successfully created"

end

解决了:

  1. 打开我的 Projects info.plist 文件并添加了一个名为 NSAppTransportSecurity 作为字典的密钥。添加了一个名为 NSAllowsArbitraryLoads 的子项作为布尔值并将其值设置为 YES 如何在 iOS 9 中启用 App Transport Security 的情况下加载 HTTP URL?

  2. 将 baseURLString 设置为http://192.168.1.11:4567

  3. 从终端运行脚本ruby web.rb -o 0.0.0.0 无法从同一网络上的另一台计算机访问本地 Sinatra 服务器

标签: iosswiftiphone

解决方案


推荐阅读