2016-09-30 97 views
1

我使用SFSafariViewController來顯示網頁,因爲我需要cookie才能工作。我不想要safari搜索欄,所以我有一個覆蓋層來隱藏它和一些按鈕來執行一些任務。我確實想要完成按鈕的功能。正如我們覆蓋,完成按鈕被隱藏,不工作,所以我試了下面的代碼。但是,這是行不通的,以及 -sfsafariviewcontroller覆蓋主頁按鈕不起作用

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> 

但首頁點擊按鈕,沒有任何反應。它沒有提供任何錯誤或警告消息。另外我知道點擊主頁按鈕我目前的代碼會給出警告再次打開應用程序,但我不想那樣也。我希望和sfsafariviewcontroller的「完成」按鈕具有完全相同的功能。我們是否可以完成這項工作並覆蓋我的覆蓋層?

在此先感謝!任何建議都是最受歡迎的。

+0

'SFSafariViewController'禁用地址欄。它仍然存在,但是你不能以任何用戶的身份輸入任何東西,所以你可能根本不需要隱藏它。 –

回答

1

爲什麼不直接調用presentViewcontroller解除函數。我用swift 3來測試這個源代碼。它正在爲我工​​作,我可以回到我的應用程序。

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

+0

是的!我不知道爲什麼我想到那個......謝謝你。你真棒! –