Coder Social home page Coder Social logo

snapkitsample-ios's Introduction

snapkit-sample-ios

This is an unofficial demo app of the Snap Kit produced by SnapChat.

Installation

You can install SnapSDK via Cocoapods.

pod 'SnapSDK', :subspecs => ['SCSDKLoginKit', 'SCSDKCreativeKit', 'SCSDKBitmojiKit']

Login Kit

First of all, you should check this document. https://docs.snapchat.com/docs/login-kit/

Following this document, you can implement SnapChat login.

You can fetch displayName & avatar (bitmoji avatar) so far.

1.Call Login Method when the button tapped

@IBAction func loginButtonTapped(_ sender: Any) {
    SCSDKLoginClient.login(from: self, completion: { success, error in
        
        if let error = error {
            print(error.localizedDescription)
            return
        }
        
        if success {
            self.fetchSnapUserInfo()
        }
    })
}

2.After login, fetch user datas

One of the interesting things is that Snap Kit is using the GraphQL not the RestAPI.

If you don't know the GraphQL, you should check this https://graphql.org/

And this codes, I made UserEntity which is inheriting Decodable because using it is easier to parse JSON I think. But of cource this is not the only way to parse JSON, choose the way what you like.

private func fetchSnapUserInfo(){
    let graphQLQuery = "{me{displayName, bitmoji{avatar}}}"
    
    SCSDKLoginClient
        .fetchUserData(
            withQuery: graphQLQuery,
            variables: nil,
            success: { userInfo in
                
                if let userInfo = userInfo,
                    let data = try? JSONSerialization.data(withJSONObject: userInfo, options: .prettyPrinted),
                    let userEntity = try? JSONDecoder().decode(UserEntity.self, from: data) {
                    
                    DispatchQueue.main.async {
                        self.goToLoginConfirm(userEntity)
                    }
                }
        }) { (error, isUserLoggedOut) in
            print(error?.localizedDescription ?? "")
    }
}

3.Some Adveices

There're some difficult points, so I note about that here.

Equalize Info.plist's SCSDKRedirectUrl with Redirect URLs on SnapChat Developer Portal (https://kit.snapchat.com/portal/)

If you didn't equalize that, the SnapChat App would show the error.

Don't use - in your app's url scheme.

If you use - like snap-client, the SnapChat App would show the error. I don't know the reason, but it seems to be the rule of SnapChat Developer Potal.

Creative Kit

Following this document https://docs.snapchat.com/docs/creative-kit/

This is so easy to code.

You can share a photo or video attaching a sticker, url, and caption.

Sample Code

import SCSDKCreativeKit

let snapshot = sceneView.snapshot() // Any image is OK. In this codes, SceneView's snapshot is passed.
let photo = SCSDKSnapPhoto(image: snapshot)
let snap = SCSDKPhotoSnapContent(snapPhoto: photo)

// Sticker
let sticker = SCSDKSnapSticker(stickerImage: #imageLiteral(resourceName: "snap-ghost"))
snap.sticker = sticker
        
// Caption
snap.caption = "Snap on Snapchat!"
        
// URL
snap.attachmentUrl = "https://www.snapchat.com"
        
let api = SCSDKSnapAPI(content: snap)
api.startSnapping { error in
            
    if let error = error {
        print(error.localizedDescription)
    } else {
        // success
    
    }
}

If you use SCSDKVideoSnapContent, you can share the videos.

Bitmoji Kit

Fetch avatar image

You can fetch your avatar image by coding like below codes.

import SCSDKBitmojiKit

// fetch your avatar image.
SCSDKBitmojiClient.fetchAvatarURL { (avatarURL: String?, error: Error?) in
    DispatchQueue.main.async {
        if let avatarURL = avatarURL {
            self.iconView.load(from: avatarURL)
        }
    }
}

Show bitmoji pickerview

The SCSDKBitmojiStickerPickerViewController is prepared in SCSDKBitmojiKit.

You can add the picker view as child viewController. It's very easy.

@IBAction func bitmojiButtonTapped(_ sender: Any) {
    // Make bitmoji background view
    let viewHeight: CGFloat = 300
    let screen: CGRect = UIScreen.main.bounds
    let backgroundView = UIView(
        frame: CGRect(
            x: 0,
            y: screen.height - viewHeight,
            width: screen.width,
            height: viewHeight
        )
    )
    view.addSubview(backgroundView)
    bitmojiSelectionView = backgroundView
    
    // add child ViewController
    let stickerPickerVC = SCSDKBitmojiStickerPickerViewController()
    stickerPickerVC.delegate = self
    addChildViewController(stickerPickerVC)
    backgroundView.addSubview(stickerPickerVC.view)
    stickerPickerVC.didMove(toParentViewController: self)
}

Inherit SCSDKBitmojiStickerPickerViewControllerDelegate

If you Inherit SCSDKBitmojiStickerPickerViewControllerDelegate, you can track events when the piker is selected, and the serach field is focused.

In this demo codes, it makes the AR stamps by the URL of bitmoji and place to the sceneView.

extension CameraViewController: SCSDKBitmojiStickerPickerViewControllerDelegate {
    
    func bitmojiStickerPickerViewController(_ stickerPickerViewController: SCSDKBitmojiStickerPickerViewController, didSelectBitmojiWithURL bitmojiURL: String) {
        
        bitmojiSelectionView?.removeFromSuperview()
        
        if let image = UIImage.load(from: bitmojiURL) {
            DispatchQueue.main.async {
                self.setImageToScene(image: image)
            }
        }
    }
    
    func bitmojiStickerPickerViewController(_ stickerPickerViewController: SCSDKBitmojiStickerPickerViewController, searchFieldFocusDidChangeWithFocus hasFocus: Bool) {
        
    }
}

Reference

snapkitsample-ios's People

Contributors

kboy-silvergym avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

snapkitsample-ios's Issues

about configuration error

i am facing issues that,
2018-12-01 17:43:23.652496+0530 SnapChat[6660:154997] [Session] Session (0x7faa36609890): did fail with error: Error Domain=com.apple.arkit.error Code=100 "Unsupported configuration." UserInfo={NSLocalizedDescription=Unsupported configuration., NSLocalizedFailureReason=The provided configuration is not supported on this device.}

what configuration i have to set and where?

Getting issue of multiple command

Multiple commands produce '/Users/techugo/Library/Developer/Xcode/DerivedData/Stickers-gblsyhfmyhpfcwcwcuyuihowssnd/Build/Products/Debug-iphonesimulator/Stickers.app/Frameworks/SCSDKCoreKit.framework':

  1. That command depends on command in Target 'Stickers' (project 'Stickers'): script phase “[CP] Embed Pods Frameworks”
  2. That command depends on command in Target 'Stickers' (project 'Stickers'): script phase “[CP] Embed Pods Frameworks”
  3. That command depends on command in Target 'Stickers' (project 'Stickers'): script phase “[CP] Embed Pods Frameworks”

About Pod installation, getting error

[!] CocoaPods could not find compatible versions for pod "SnapSDK/SCSDKLoginKit":
In Podfile:
SnapSDK/SCSDKLoginKit

Specs satisfying the SnapSDK/SCSDKLoginKit dependency were found, but they required a higher minimum deployment target.

Error: Invalid redirect_uri

Hi,
I am using the Snapkit login with the development environment client key, everything works well.
But when I try to use the production environment client key, I always get the error "Something went wrong - Invalid redirect_uri".
I don't change redirect uri between development and production environment.

My App on Snapkit dashboard was approved. I also tried on the snapkit demo project also. But I get the same issue.

Here is my redirect uri: snapfriends://com.snapfriends.app/login

Do you have any ideas to solve this issue? Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.