2016-08-15 60 views
27

因此顯然OpenURL已在iOS 10中折舊。有沒有人有任何關於爲什麼或可以解釋接下來要做什麼的文檔?我看着在蘋果網站上已經發現有關的OpenURL一些東西,這就是他們現在說的內容:iOS10中的OpenURL

UIApplication.shared().open(url: URL, options: [String: AnyObject], completionHandler: ((Bool) -> Void)?) 

沒有人有任何證據表明這是雨燕3.0採用的OpenURL新的方式?另外,分別在參數options:completionHandler:中分別使用什麼值?

+0

@LeoDabus這是說「打開是不可用的」 –

+0

@TomRoggero http://stackoverflow.com/questions/39402696/swift-3-open-link/39460744#39460744 –

回答

40

您也可以使用條件檢查,如果你正在使用的應用程序兼容iOS10代碼更新您:

func open(scheme: String) { 
    if let url = URL(string: scheme) { 
     if #available(iOS 10, *) { 
     UIApplication.shared.open(url, options: [:], 
      completionHandler: { 
       (success) in 
        print("Open \(scheme): \(success)") 
      }) 
    } else { 
     let success = UIApplication.shared.openURL(url) 
     print("Open \(scheme): \(success)") 
    } 
    } 
} 

用法:

open(scheme: "tweetbot://timeline") 

Source

+0

這個示例僅用於Swift 3.0。這很棒! –

+0

謝謝,我已經適應了這一點,現在正在使用它。 –

+0

使用這個let success = UIApplication.shared()。openURL(url)而不是這個let success = UIApplication.shared.openURL(url) – niravdesai21

5

新的UIApplication方法openURL:options:completionHandler :,它異步執行並在主隊列上調用指定的完成處理程序(此方法替換openURL :)。

這是在其他框架的變化>在UIKit的https://developer.apple.com/library/prerelease/content/releasenotes/General/WhatsNewIniOS/Articles/iOS10.html

+0

這種問題的答案但不完全。是'openURL:options:completionHandler:'我們應該使用的一個新函數,還是我在我的問題中提出的建議?你仍然在使用'UIApplication.shared()...' – KSigWyatt

+0

從我讀到的,它肯定表明'openURL:options:com pletionHandler:'函數是'openUrl'的替代品。並非所有文檔都已更新,[某些](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Inter-AppCommunication/Inter-AppCommunication.html#//apple_ref/ doc/uid/TP40007072-CH6-SW8)仍然指向不推薦的代碼。 – Yasir

+0

這是你想發佈的鏈接:https://developer.apple.com/library/prerelease/content/releasenotes/General/WhatsNewIniOS/Articles/iOS10.html#//apple_ref/doc/uid/TP40017084-DontLinkElementID_34 –

8

空字典選項將導致相同的行爲的OpenURL

否則:

+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+ 
| UIApplicationOpenURLOptionsSourceApplicationKey | NSString containing the bundle ID of the originating application                    | 
+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+ 
| UIApplicationOpenURLOptionsAnnotationKey  | property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController's annotation property | 
+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+ 
| UIApplicationOpenURLOptionsOpenInPlaceKey  | bool NSNumber, set to YES if the file needs to be copied before use                   | 
+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+ 

從UIApplication.h

// Options are specified in the section below for openURL options. An empty options dictionary will result in the same 
// behavior as the older openURL call, aside from the fact that this is asynchronous and calls the completion handler rather 
// than returning a result. 
// The completion handler is called on the main queue. 
- (void)openURL:(NSURL*)url options:(NSDictionary<NSString *, id> *)options completionHandler:(void (^ __nullable)(BOOL success))completion NS_AVAILABLE_IOS(10_0) NS_EXTENSION_UNAVAILABLE_IOS(""); 

UIKIT_EXTERN UIApplicationOpenURLOptionsKey const UIApplicationOpenURLOptionsSourceApplicationKey NS_SWIFT_NAME(sourceApplication) NS_AVAILABLE_IOS(9_0); // value is an NSString containing the bundle ID of the originating application 
UIKIT_EXTERN UIApplicationOpenURLOptionsKey const UIApplicationOpenURLOptionsAnnotationKey NS_SWIFT_NAME(annotation) NS_AVAILABLE_IOS(9_0); // value is a property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController's annotation property 
UIKIT_EXTERN UIApplicationOpenURLOptionsKey const UIApplicationOpenURLOptionsOpenInPlaceKey NS_SWIFT_NAME(openInPlace) NS_AVAILABLE_IOS(9_0); // value is a bool NSNumber, set to YES if the file needs to be copied before use 
1

如果您的應用程序還支持iOS 9或更低,只要繼續使用舊openURL。如果您的部署目標是iOS 10,那麼您只應移至新的目標。

26

速戰速決:

// Objective-C 
UIApplication *application = [UIApplication sharedApplication]; 
[application openURL:URL options:@{} completionHandler:nil]; 

// Swift 
UIApplication.shared.open(url, options: [:], completionHandler: nil) 

完整的回答:

http://useyourloaf.com/blog/openurl-deprecated-in-ios10/

學分:基思·哈里森(useyourloaf.com)

-2

讓實際:

[String: AnyObject] = ["xxx key": "xxx value" as AnyObject, "yyy key": "yyy value" as AnyObject] 

UIApplication.shared.open(URL(string: "http:google.com")!, options: actual, completionHandler: {(true) -> Swift.Void in 
print("Refresh") 
}) 

其中xxx和yyy是您要打印的任何字符串或將它們留爲空白。

1

在加載url之前,您需要先進行一些檢查。請檢查下面的代碼。

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"https://www.gmail.com"]]){ 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.gmail.com"] options:@{} completionHandler:^(BOOL success) { 
           //completion codes here 
          }]; 
} 

我希望這會有所幫助。