最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - How can I be redirected from my app to share view in snapchat? - Stack Overflow

programmeradmin5浏览0评论

I spend some time yesterday to find a way how to deep link to the share view in Snapchat, but I couldn't find any solution yet. After hours of trying, I found some deeplinks that are working: snapchat://map, snapchat://memories, snapchat://creativekit/preview/1, snapchat://creativekit/camera/1. I will leave the code below:

enum CreativeKitLiteKeys {
    static let clientID = "com.snapchat.creativekit.clientID"
    static let backgroundImage = "com.snapchat.creativekit.backgroundImage"
    static let backgroundVideo = "com.snapchat.creativekit.backgroundVideo"
    static let stickerImage = "com.snapchat.creativekit.stickerImage"
    static let payloadMetadata = "com.snapchat.creativekit.payloadMetadata"
    static let lensUUID = "com.snapchat.creativekit.lensUUID"
    static let appName = "com.snapchat.creativekit.appName"
    static let caption = "com.snapchat.creativekit.captionText"
    static let launchData = "com.snapchat.creativekit.lensLaunchData"
}

enum ShareDestination: String {
    case preview = "snapchat://creativekit/preview/1" // This is working
    case camera = "snapchat://creativekit/camera/1" // This is working
    case text = "snapchat://text=; // This is not working
    case text2 = "snapchat://send?text=; // This is not working 
}

enum ShareMediaType {
    case image
    case video
}

public struct Identifiers {
    static let CLIENT_ID = "4xe23deb-b83l-404w-1010-aee213a7c5f1"
}

func createImageData(_ image: UIImage) -> Data? {
    return image.jpegData(compressionQuality: 1.0)
}

func shareToPreview(clientID: String, mediaType: ShareMediaType, mediaData: Data, caption: String?)
{
    // Pass media content to the pasteboard
    var dict: [String: Any] = [ CreativeKitLiteKeys.clientID: clientID ]
    switch mediaType {
    case .image:
        dict[CreativeKitLiteKeys.backgroundImage] = mediaData
    case .video:
        dict[CreativeKitLiteKeys.backgroundVideo] = mediaData
    }
   
    if let caption = caption
    {
        dict[CreativeKitLiteKeys.caption] = caption
    }
    
    createAndOpenShareUrl(clientID:clientID, shareDest: ShareDestination.preview, dict:dict)
    
}

func createAndOpenShareUrl(clientID:String, shareDest: ShareDestination, dict:[String:Any])
{
    guard var urlComponents = URLComponents(string: shareDest.rawValue),
        let url = urlComponents.url,
        UIApplication.shared.canOpenURL(url) else {
        return
    }
    
    let items = [ dict ]
    
    let expire = Date().addingTimeInterval(5*60)
    let options = [ UIPasteboard.OptionsKey.expirationDate: expire ]
    UIPasteboard.general.setItems(items, options: options)
    
    let queryItem = URLQueryItem.init(name: "checkcount",
                                      value: String(format: "%ld",
                                      UIPasteboard.general.changeCount))
    
    let clientIdQueryItem = URLQueryItem.init(name: "clientId", value: clientID)
    
    var appDisplayName = Bundle.main.infoDictionary!["CFBundleDisplayName"] as? String
    if (appDisplayName == nil) {
        appDisplayName = Bundle.main.infoDictionary?[kCFBundleNameKey as String] as? String
    }
    let appDisplayNameQueryItem = URLQueryItem.init(name: "Gossip Widget", value: appDisplayName)
 
    
    // Create and Open the final Share URL
    urlComponents.queryItems = [
        queryItem,
        clientIdQueryItem,
        appDisplayNameQueryItem
    ]
    if let finalURL = urlComponents.url {
        UIApplication.shared.open(finalURL, options: [:],
                                  completionHandler: nil)
    }
}

This is the test view:

struct ShareToPreviewView: View {
    @State private var sourceType: UIImagePickerController.SourceType = .photoLibrary
    @State private var selectedImage: UIImage?
    @State private var showImagePicker = false
    @State private var caption  = "Hello!"
    
    var body: some View {
        ScrollView{
            VStack (alignment: .leading) {
                if selectedImage != nil {
                    let resized = resizeImage(image: selectedImage!, newWidth: 200)
                    Image(uiImage: resized!)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 100, height: 100)
                        .clipShape(Circle())
                } else {
                    Image(systemName: "photo")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 100, height: 100)
                        .clipShape(Circle())
                }
                Spacer()
                Text("1. Select a photo to share to preview")
                Button("Select photo") {
                    showImagePicker = true
                }
                .sheet(isPresented: self.$showImagePicker) {
                    ImagePickerView(selectedImage: self.$selectedImage, sourceType: self.sourceType)
                }
                Text("2. (Optional) Set a caption")
                TextField("Caption", text: $caption)
                    .textFieldStyle(.roundedBorder)
                    .ignoresSafeArea(.keyboard, edges: .bottom)
                Spacer()
                HStack()
                {
                    Spacer()
                    Button(action:{
                        shareToPreview(
                            clientID: Identifiers.CLIENT_ID,
                            mediaType: ShareMediaType.image,
                            mediaData: self.selectedImage!.pngData()!,
                            caption: $caption.wrappedValue)
                    })
                    {
                        Text("Share to Preview")
                    }
                    .disabled(self.selectedImage == nil)
                    Spacer()
                }
            }
        }
    }
}

This: snapchat://creativekit/preview/1 deeplink is working and is looking like this: Working deeplinking. You can select a photo and type a text in your app and will redirect you with the chosen photo and text and you can send it.

What I would like to do is to share a link / text, but I want the user to be redirected here instead -> Desired deeplinking. I know is possible because I saw in other apps too.

I tried the following: snapchat://text=, snapchat://send?text=, snapchat://snap?text=. For all of these, I'm getting an error in snapchat: "Uh oh, something went wrong"

I hope that someone knows the correct deeplinking. Thanks !

Later edit: I saw a lot of people dealing with these kind of issues when they tried to deeplink to snapchat, up voting this question would really help.

发布评论

评论列表(0)

  1. 暂无评论