简体   繁体   English

如何从我在iOS中制作的应用程序内部打开新应用程序(Facebook)

[英]How can I open a new application (Facebook) from inside an app I made in iOS

如何从最近创建的应用程序中打开Facebook应用程序?

You need support from the application you wish to open. 您需要您要打开的应用程序的支持。 If an application is configured to handle a custom URL scheme, you can use UIApplication 's openURL method to open it. 如果将应用程序配置为处理自定义URL方案,则可以使用UIApplicationopenURL方法将其打开。

Luckily for you, the Facebook application does support a custom URL scheme. 幸运的是,Facebook应用程序确实支持自定义URL方案。 Details here . 详细信息在这里 Note that this isn't a documented interface, so the Facebook app could take this functionality away in an update. 请注意,这不是文档化的界面,因此Facebook应用程序可以在更新中取消此功能。 To handle this correctly, plus to handle the case where the user doesn't have the app installed, you should check using UIApplication 's handleURL method. 为了正确处理此问题,并处理用户未安装应用程序的情况,应使用UIApplicationhandleURL方法进行检查。

Depending on what you are doing, using Facebook's API might be a better approach for you to take. 根据您的工作,使用Facebook的API可能是您更好的选择。

You open other applications if they have an URL scheme, an apps URL scheme may change with any update or even be removed completely, so always check to see if the app is installed/the URL scheme hasn't be changed or removed. 如果其他应用程序具有URL方案,则您可以打开它们,一个应用程序的URL方案可能会随着任何更新而更改,甚至被完全删除,因此请务必检查该应用程序是否已安装/ URL方案没有更改或删除。

UIApplication is used to do these 2 things. UIApplication用于完成这两件事。

You need to first get the sharedApplication 您需要先获取sharedApplication

[UIApplication sharedApplication]

Then use canOpenURL:(NSURL *)url to see if the app can be opened. 然后使用canOpenURL:(NSURL *)url查看是否可以打开该应用程序。 This won't check if the URL is valid for that app, so it might still fail if this returns YES . 这不会检查该URL是否对该应用程序有效,因此,如果返回YES ,它可能仍然会失败。 But the other app will definately open if YES is returned. 但是,如果返回YESYES则另一个应用将完全打开。

And then use openURL:(NSURL *)url to actually open it. 然后使用openURL:(NSURL *)url实际打开它。

eg: 例如:

//myURL is already set up
if ([[UIApplication sharedApplication] canOpenURL:myURL]) {
    if (![[UIApplication sharedApplication] openURL:myURL]) {
        //There was an error opening the URL
    }
}

http://handleopenurl.com/ Is the place to look up URL schemes for apps. http://handleopenurl.com/是查找应用程序URL方案的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM