1

我試圖讓我的應用程序從qr碼打開掃描的URL。我製作了一款qr碼掃描儀,但它僅將掃描後的qr碼的值複製到粘貼板。我試圖讓它在SFSafariViewController中打開它。 而不是「複製」選項,我想讓它「打開」,並實際上打開掃描的網址。打開從SFSafariViewController中的QR碼掃描的URL

這裏是我的代碼

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) { 
    if metadataObjects != nil && metadataObjects.count != 0 
    { 
     if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject 
     { 
      if object.type == AVMetadataObjectTypeQRCode 
      { 
       let alert = UIAlertController(title: "QR Code", message: object.stringValue, preferredStyle: .alert) 
       alert.addAction(UIAlertAction(title: "Retake", style: .default, handler: nil)) 
       alert.addAction(UIAlertAction(title: "Copy", style: .default, handler: { (nil) in 
        UIPasteboard.general.string = object.stringValue 

       })) 
       present(alert, animated: true, completion: nil) 

      } 
     } 
    } 

} 

回答

0

你的代碼顯示沒有嘗試使用SFSafariViewController的跡象。你有沒有試過類似下面的東西?

import SafariServices 

if let url = URL(string: object.stringValue) { 
    let browser = SFSafariViewController(url: url) 
} 

這不會使奇蹟般地出現,還是會有一些你需要做的自己呈現給用戶,如工作:

presentViewController(browser, animated: true, completion: nil) 
1

使用safariviewController是非常簡單的

首先你必須導入safariservices,然後提供一個url的safaricontroller

這個的基本代碼是

import SafariServices 

func loadSafari(url : String){ 
    guard let url = URL(string: url) else { return } 

    let safariController = SFSafariViewController(url: url) 
    present(safariController, animated: true, completion: nil) 
} 

將此代碼放在你的類並調用該函數你捕捉輸出內部

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) { 
if metadataObjects != nil && metadataObjects.count != 0 
{ 
    if let object = metadataObjects[0] as? AVMetadataMachineReadableCodeObject 
    { 
     if object.type == AVMetadataObjectTypeQRCode 
     { 

       UIPasteboard.general.string = object.stringValue 
       loadSafari(url: object.stringValue) 

      })) 
      present(alert, animated: true, completion: nil) 

     } 
    } 
} 

} 

點擊完成將駁回safariController,並讓用戶導航回到以前的視圖控制器。

我希望這會有所幫助。讓我知道事情的後續。

+0

工作!謝謝! 我剛剛離開那裏的「重拍」按鈕,並將「複製」更改爲「打開」,然後調用loadSafari函數。 再次感謝! – Ibrahim

+0

很高興幫助你@Ibrahim。 :) –