launch Youtube channel on your App Swift Code

余生长醉 提交于 2019-12-12 21:19:40

问题


I have spent some days to find the Swift code for youtube Channel to open from my app. but i couldn't find At all, can some one help me please!! i need the Code in Swift.


回答1:


Update for Swift 3 and iOS 10+

OK, here is how to do it in Swift 3. Basically, there are two easy steps to achieve this:

First, you have to modify Info.plist to list Youtube with LSApplicationQueriesSchemes. Simply open Info.plist as a Source Code, and paste this:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>youtube</string>
</array>

After that, you can open any youtube URL inside Youtube application by simply substituting https:// with youtube://. Here is a complete code, you can link this code to any button you have as an Action:

@IBAction func YoutubeAction() {

    let YoutubeUser =  "Your Username"
    let appURL = NSURL(string: "youtube://www.youtube.com/user/\(YoutubeUser)")!
    let webURL = NSURL(string: "https://www.youtube.com/user/\(YoutubeUser)")!
    let application = UIApplication.shared

    if application.canOpenURL(appURL as URL) {
        application.open(appURL as URL)
    } else {
        // if Youtube app is not installed, open URL inside Safari
        application.open(webURL as URL)
    }

}



回答2:


I hope this suits your purpose:

var url  = NSURL(string: "https://youtube.com/your-channel")

if UIApplication.sharedApplication().canOpenURL(url!) == true  {
    UIApplication.sharedApplication().openURL(url!)
}


来源:https://stackoverflow.com/questions/28464392/launch-youtube-channel-on-your-app-swift-code

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