Open activity without showing UIActivityViewController

末鹿安然 提交于 2019-12-03 11:58:21

问题


I want to be able to have social icons in a scrollView, which when clicked, functions the same way it would if I had clicked on them after presenting a UIActivityViewController. I don't want to present a UIActivityViewController.

I know it shouldn't be possible, but an app called Yodel does this (or something similar) and I want to do the same.

Clicking on the icons in Yodel works exactly the same as it would in a UIActivityViewController, so it seems they have somehow put it inside a container view(?)

Here's a GIF showing it

Using Whatsapp as an example I've tried the following:

  1. Using the Whatsapp API https://www.whatsapp.com/faq/en/iphone/23559013 with URL scheme

        var whatsappURL = NSURL(string: "whatsapp://send?text=Hello%2C%20World!")!
        if UIApplication.sharedApplication().canOpenURL(whatsappURL) {
            UIApplication.sharedApplication().openURL(whatsappURL)
        }
    
  2. Using UIActivityViewController, but I want them all to be in a scrollView as seen in the picture above.

  3. Using a UIWebView to open up the URL Scheme in hopes that it would stay inside the UIWebView.

    var whatsappURL = NSURL(string: "whatsapp://send?text=Hello%2C%20World!")!
    if UIApplication.sharedApplication().canOpenURL(whatsappURL) {
        UIApplication.sharedApplication().openURL(whatsappURL)
    }
    
    self.view.addSubview(webview)
    webview.loadRequest(NSURLRequest(URL: NSURL(string: "whatsapp://send?text=Hello%2C%20World!")!))
    

All of these solutions opens up the other app - I want to still stay in my app.

Any idea how to accomplish this? Help would be greatly appreciated :)


回答1:


Actually I did exactly the same, but this solution need you to also implement side server for continues support for new apps that pops on AppStore (without update your app for everytime you need/want to add/remove new app to/from the list)

1st: You need to decide what are you going to share on other apps

2nd: you need to find out the URL Scheme that apps work with,

example for instagram -> "instagram://user?username=your_username"

3rd: now the logic behind the process

let's say i want to show facebook, twitter, slack and instagram,

let's set dictionary with relevant URLs:

["facebook":"facebook://blablabla", "twitter":"twitter://blabla", "slack":"slack://blabla", "instagram":"instagram://blabla"]

ALSO YOU NEED TO HOLD THE APP's ICONS (locally or server/firebase/cloud) - to display later on your scrollview(horizontaly collectionview or what ever you decide to use)

4rd: you need to check if that apps are installed on the device:

So just loop on your apps array/dictionary (or what ever you decide to hold your info and check for availability like that:

let instagramHooks = "instagram://user?username=your_username"
let instagramUrl = URL(string: instagramHooks)
if UIApplication.shared.canOpenURL(instagramUrl! as URL)
{
 //The app are installed you can show her
 } else {
       //The app doesn't installed -> don't show it on the list
    }

After that you just need to put their icons to UICollectionView / UIScrollView and when user tap on icon you go and open that scheme:

UIApplication.shared.open(instagramUrl!)

About how the target app will show the dialog (like UIActivityViewController action or actually open the app i don't now), but I'm sure you can find here someone that help you with that

Hop you understand my steps, if not you can contact with me for more explanation




回答2:


Short: That’s not possible.

Every App runs inside their own "playground" and the Apps get entire focus of the device.

I assume that the User recreated the Interface of those Apps and got the Information out of an API. For example, we get the Content, from an Whatsapp API and recreate the UI, to make it feel like we're inside Whatsapp. But you should not do that, since you might get in conflict with copyright infringements.

But in general Apps don’t know about each other.

And the only possibility to run a 3rd party App, as you already know, is the URL Scheme. If you would want to run an App inside your App like in a UIWebView, you would need to emulate a whole iOS. So impossible.

However, it might be possible with jailbroken iPhones, but I have no idea about that.




回答3:


Yodel : They have made their own custom view, and they have picked some common apps like whatsapp,snapchat,messenger etc for which openurl common, they have checked each one by using "canOpenUrl" to check whether given app is available on user's mobile. If available then show its icon. When user clicks on it, it will open given app by using "openUrl".

Here, In yodel you can see that, it is not able to take all the available sharing apps, it is only able to predefined fix apps they have defined. For other apps, they have added "more button" at end, which on clicking will open UIActivityViewController .



来源:https://stackoverflow.com/questions/40074638/open-activity-without-showing-uiactivityviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!