sfsafariviewcontroller覆盖与主页按钮不起作用

时间:2016-09-30 14:42:26

标签: ios swift sfsafariviewcontroller

我正在使用SFSafariViewController来显示网页,因为我需要使用Cookie。我不想要safari搜索栏,所以我有一个覆盖来隐藏它和一些按钮来执行一些任务。我确实想要完成按钮的功能。由于我们有覆盖,完成按钮是隐藏的,不起作用,所以我尝试了下面的代码。但这并不奏效 -

self.presentViewController(sfController, animated: true) {
        let bounds = UIScreen.mainScreen().bounds

        // It can be any overlay. May be your logo image here inside an imageView.
        let overlay = UIView(frame: CGRect(x: 0, y: 0, width: bounds.size.width, height: 65))
        let completedBtn = UIButton()
        let favBtn = UIButton()
        let homeBtn = UIButton()

        homeBtn.setTitle("Home",forState: .Normal)
        homeBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        homeBtn.frame = CGRectMake(20, 25, 200, 35)
        homeBtn.backgroundColor = UIColor(netHex: 0x6F9824)
        homeBtn.addTarget(self, action: #selector(DetailsViewController.HomeBtnAction), forControlEvents: UIControlEvents.TouchUpInside)

        completedBtn.setTitle("Completed",forState: .Normal)
        completedBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        completedBtn.frame = CGRectMake((bounds.size.width - 210), 25, 200, 35)
        completedBtn.backgroundColor = UIColor(netHex: 0x6F9824)
        completedBtn.addTarget(self, action: #selector(DetailsViewController.CompletedAction), forControlEvents: UIControlEvents.TouchUpInside)

        favBtn.setTitle("Favourite", forState: .Normal)
        favBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        favBtn.frame = CGRectMake((bounds.size.width - 420), 25, 200, 35)
        favBtn.backgroundColor = UIColor(netHex: 0x6F9824)
        favBtn.addTarget(self, action: #selector(DetailsViewController.FavouriteAction), forControlEvents: UIControlEvents.TouchUpInside)

        overlay.backgroundColor = UIColor(netHex: 0x435C16)

        overlay.addSubview(favBtn)
        overlay.addSubview(completedBtn)
        overlay.addSubview(homeBtn)
        sfController.view.addSubview(overlay)
}

On click of Home button i have this code - 
func HomeBtnAction(){
        let app = "MyApp://"
        let appUrl = NSURL(string: app)
        if UIApplication.sharedApplication().canOpenURL(appUrl!)
        {
            UIApplication.sharedApplication().openURL(appUrl!)

        }
    }

我的plist看起来像这样 -

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>MyApp</string>
    </array>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>Test.com.MyApp</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>MyApp</string>
            </array>
        </dict>
    </array>

但是点击Home按钮,没有任何反应。它没有给出任何错误或警告信息。此外,我知道点击主页按钮我的当前代码将提供再次打开应用程序的警报,但我也不希望这样。我想要与sfsafariviewcontroller的“done”按钮完全相同的功能。我们可以做那个工作并有我的叠加层吗?

提前致谢!任何建议都是最受欢迎的。

1 个答案:

答案 0 :(得分:1)

为什么不直接调用presentViewcontroller解除函数。我使用swift 3来测试这个源代码。它正确地为我工作,我可以回到我的应用程序。

func HomeBtnAction(){
        print("Button Pressed")
        self.dismiss(animated: true, completion: nil)
    }