Build status

Mobile Buy SDK for iOS

Shopify’s Mobile Buy SDK makes it simple to sell physical products inside your mobile app. With a few lines of code, you can connect your app with the Shopify platform and let your users buy your products using Apple Pay or their credit card.

Documentation

Official documentation can be found on the Mobile Buy SDK for iOS page.

API Documentation

API docs (.docset) can be generated with the Documentation scheme or viewed online at Cocoadocs: http://cocoadocs.org/docsets/Mobile-Buy-SDK/.

The SDK includes a pre-compiled .docset that can be used in API documentation browser apps such as Dash.

Installation

Download the latest version

Dynamic Framework Installation

  1. Drag the Mobile Buy SDK.xcodeproj into your existing project
  2. Add the Buy target as a Target Dependancy in the Build Phases of your project's target
  3. Add the Buy (second target on the list is the Dynamic framework) target in the Embedded Binaries section in Build Phases

See the Sample Apps for an example of Dynamic Framework usage.

Static Framework Installation

If you would like to not include the Mobile Buy SDK Project within your existing project, you can link directly to the Buy.framework.

  1. Open the Mobile Buy SDK.xcodeproj and build the Static Universal Framework scheme
  2. Drag the Buy.framework that was just created from Mobile Buy SDK Sample Apps into the Linked Frameworks and Libraries section for the target you want to add the framework to. Check Copy items if needed so the framework is copied to your project
  3. In the Build Settings tab, add -all_load to Other Linker Flags

Quick Start

Import the module

@import Buy;

Initialize the BUYClient with your credentials from the Mobile App Channel

BUYClient *client = [[BUYClient alloc] initWithShopDomain:@"yourshop.myshopify.com"
                                                   apiKey:@"aaaaaaaaaaaaaaaaaa"
                                                    appId:@"99999"];

Storefront API

After initializing the client with valid shop credentials, you can begin fetching collections.

[client getCollectionsPage:1 completion:^(NSArray<BUYCollection *> * _Nullable collections, NSUInteger page, BOOL reachedEnd, NSError * _Nullable error) {
    if (collections && !error) {
        // Do something with collections
    } else {
        NSLog(@"Error fetching collections: %@", error.userInfo);
    }
}];

Having a collection, we can then retrieve an array of products within that collection:

BUYCollection *collection = collections.firstObject;

[client getProductsPage:1 inCollection:collection.identifier completion:^(NSArray<BUYProduct *> * _Nullable products, NSUInteger page, BOOL reachedEnd, NSError * _Nullable error) {
    if (products && !error) {
        // Do something with products
    } else {
        NSLog(@"Error fetching products in collection %@: %@", collection.identifier, error.userInfo);
    }
}];

Customer API

Customer Login

You can allow customers to login with their existing account using the SDK. First, we'll need to create the BUYAccountCredentials object used to pass authentication credentials to the server.

NSArray *credentialItems = @[
                             [BUYAccountCredentialItem itemWithEmail:@"john.smith@gmail.com"],
                             [BUYAccountCredentialItem itemWithPassword:@"password"],
                            ];
BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:credentialItems];

We can now use the credentials object to login the customer.

[client loginCustomerWithCredentials:credentials callback:^(BUYCustomer * _Nullable customer, BUYCustomerToken * _Nullable token, NSError * _Nullable error) {
    if (customer && token && !error) {
        // Do something with customer and store token for future requests
    } else {
        NSLog(@"Failed to login customer: %@", error.userInfo);
    }
}];
Customer Creation

Creating a customer account is very similar to login flow but requres a little more info in the credentials object.

NSArray *credentialItems = @[
                             [BUYAccountCredentialItem itemWithFirstName:@"John"],
                             [BUYAccountCredentialItem itemWithLastName:@"Smith"],
                             [BUYAccountCredentialItem itemWithEmail:@"john.smith@gmail.com"],
                             [BUYAccountCredentialItem itemWithPassword:@"password"]
                            ];
BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:credentialItems];

After we obtain the customers first name, last name and password in addition to the email and password fields, we can create an account.

[client createCustomerWithCredentials:credentials callback:^(BUYCustomer * _Nullable customer, BUYCustomerToken * _Nullable token, NSError * _Nullable error) {
    if (customer && token && !error) {
        // Do something with customer and store token for future requests
    } else {
        NSLog(@"Failed to create customer: %@", error.userInfo);
    }
}];

Integration Guide

Consult the Usage Section of the Integration Guide on how to create a cart, checkout and more with the SDK.

Building the SDK

Clone this repo or download as .zip and open Mobile Buy SDK.xcodeproj.

The workspace includes the Mobile Buy SDK project.

Mobile Buy SDK Targets and schemes

The Mobile Buy SDK includes a number of targets and schemes:

  • Buy: This is the Mobile Buy SDK dynamic framework. Please refer to the installation section above

  • Static Universal Framework: This builds a static framework from the Buy target using the build_universal.sh script in the Static Universal Framework target and copies the built framework in the /Mobile Buy SDK Sample Apps folder. This is a fat binary that includes arm and i386 slices. Build this target if you have made any changes to the framework that you want to test with the sample apps as the sample apps do not build the framework directly but embed the already built framework

  • Mobile Buy SDK Tests: Tests for the Mobile Buy SDK framework. See instructions below

  • Documentation: This generates appledoc documentation for the framework

Sample Apps

The repo includes 4 sample apps. Each sample apps embeds the dynamic framework and includes readme files with more information:

We suggest you take a look at the Advanced Sample App and test your shop with the sample app before you begin. If you run into any issues, the Advanced Sample App is also a great resource for debugging integration issues and checkout.

Product View

The Advanced Sample App includes an easy-to-use product view to make selling simple in any app. The ProductViewController displays any product, it's images, price and details and includes a variant selection flow. It will even handle Apple Pay and web checkout automatically:

Product View Screenshot

You can also theme the ProductViewController to better match your app and products being displayed:

Product View Screenshot

The Advanced Sample App includes a demo of the ProductViewController. Documentation on how to use the ProductViewController is also available here.

Unit Tests

To run the Mobile Buy SDK integration tests against an actual shop, you will need a Shopify shop that is publicly accessible (not password protected). Please note that the integration tests will create an order on that shop. This is to validate that the SDK works properly with Shopify. Modify the test_shop_data.json file to contain your shop's credentials and the required product IDs, gift cards, and discounts as necessary.

If the credentials in the test_shop_config.json are empty, running the integration tests will use mocked respoonses. The mocked responses are defined in mocked_responses.json. Do not check in credentials in this file.

Example test_shop_config.json

{   
    "domain":"yourshop.myshopify.com",
    "api_key":"abc123",
    "app_id":"8",
    "merchant_id":"merchant.com.yourcompany.app"
}

Alternatively, you can edit the Mobile Buy SDK Tests scheme and add the following arguments to the Environment Variables:

  • shop_domain: Your shop's domain, for example: abetterlookingshop.myshopify.com
  • api_key: The API provided when setting up the Mobile App channel on Shopify Admin: https://your_shop_id.myshopify.com/admin/mobile_app/integration
  • app_id: The App ID provided with the API Key above
  • gift_card_code_11, gift_card_code_25, gift_card_code_50: Three valid Gift Card codes for your shop
  • expired_gift_card_code: An expired Gift Card code
  • expired_gift_card_id: The ID for the expired Gift Card
  • product_ids_comma_separated: a comma seperated list of product IDs (2 is suitable) to use for the cart

Contributions

We welcome contributions. Follow the steps in CONTRIBUTING file.

Help

For help, please post questions on our forum, in the Shopify APIs & Technology section: https://ecommerce.shopify.com/c/shopify-apis-and-technology

License

The Mobile Buy SDK is provided under an MIT Licence. See the LICENSE file