我有一个用Objective-c编写的iOS应用程序,还有一个带有WooCommerce的Wordpress网站。如何连接它们? [英] I have an iOS app written in Objective-c and a Wordpress Website with WooCommerce. How can I connect them?

查看:49
本文介绍了我有一个用Objective-c编写的iOS应用程序,还有一个带有WooCommerce的Wordpress网站。如何连接它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个网站,并使用WooCommerce插件对其进行了设置以处理我的电子商务。该系统很棒。

I built a website and set it up with the WooCommerce plugin to handle my eCommerce. That system works great.

我构建了一个iOS应用程序,该应用程序根本没有连接到我的Wordpress网站。我显然是新手,但我正在尝试了解如何连接这两个系统。

I built an iOS app that is not connected to my Wordpress site at all. I am clearly a novice, but I am trying to understand how to connect these two systems.

用户名/密码,购买,运输等...。

Usernames/Passwords, Purchases, Shipping, etc....

有人可以指出我的意思吗?解决方案的方向,因为我的搜索空白。谢谢!

Can anyone point me in the right direction of a solution, because I am coming up blank on my search. Thanks!

推荐答案

步骤1)安装插件用于Wordpress身份验证。 (PS在下面的示例中使用了基本身份验证。)

Step 1) Install one of the plugins for Wordpress authentication. (P.S. I used basic authentication in the example below.)

步骤2)转到WooCommerce>设置> API>密钥/应用,然后添加一个新密钥,然后生成API密钥。它将为您创建consumer_key和consumer_secret。

Step 2) Go to WooCommerce > Settings > API > Keys/Apps, and add a new key and then generate API Key. It will create consumer_key and consumer_secret for you.

第3步)在WooCommerce设置中启用REST API。 (要在WooCommerce中启用REST API,请访问WooCommerce>设置> API选项卡,然后选中启用REST API复选框。)

Step 3) Enable REST API in the WooCommerce settings. (To enable the REST API within WooCommerce, visit the WooCommerce > Settings > API tab and tick the Enable REST API checkbox.) source

第4步)我假设您正在使用Swift开发应用程序。如果是这样,请参见下面的代码,例如获取用户详细信息和订单。不要忘记在您的项目中包含 Alamofire 框架。

Step 4) I assume that you are developing an app with Swift. If so see the code below for getting user details and orders for example. Don't forget to include Alamofire framework in your project.

// to get user info
let plainString = "WP_username:WP_password" as NSString
let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding)
let base64String = plainData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))


let headers = [
    "Authorization": "Basic " + base64String
]

Alamofire.request(.GET, "http://yourwebsite.com/wp-json/wp/v2/users/me?_envelope=1&context=embed", headers: headers)
    .responseJSON{ response in switch response.result {
    case .Success(let JSON):
        print("Success with JSON: \(JSON)")

    case .Failure(let error):
        print("Request failed with error: \(error)")
        }
}


// to get a customer's orders
Alamofire.request(.GET, "https://yourwebsite.com/wc-api/v3/customers/USER_ID/orders", parameters:    
["consumer_key":"ck_XXX", "consumer_secret":"cs_XXX"])
         .responseJSON{ response in switch response.result {
            case .Success(let JSON):
                let response = JSON as! NSDictionary
                print(response)

            case .Failure(let error):
                print("Request failed with error: \(error)")
          }
}    

这篇关于我有一个用Objective-c编写的iOS应用程序,还有一个带有WooCommerce的Wordpress网站。如何连接它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆