Richa Srivastava Richa Srivastava - 2 months ago 18
iOS Question

sfsafariviewcontroller overlay with home button not working

I am using

SFSafariViewController
to show webpage as I require cookies to work. I don't want the safari search bar so I have a overlay to hide it and some buttons to perform some task. I do want the functionality of done button though. As we have overlay, done button is hidden and not working, so I tried this below code. But this is not working as well -

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!)

}
}


And My plist looks like this -

<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>


But On click of Home button, nothing happens. It is not giving any error or warning message. Also I know on click on Home button my current code will give a alert to open the app again, but i don't want that also. I want exactly the same functionality as "done" button of the sfsafariviewcontroller. Can we make that work and have my overlay as well?

Thanks in Advance! And any suggestions are most welcome.

Answer

Why don't you just call the presentViewcontroller dismiss function. I used swift 3 to test this source code. It's working for me properly and I can go back to my applications.

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

Comments