【问题标题】:handleOpenURL won't trigger iOS Swift 2handleOpenURL 不会触发 iOS Swift 2
【发布时间】:2015-09-30 22:33:41
【问题描述】:

我正在尝试为我正在开发的应用获取 Instagram 身份验证令牌,我有该应用正在打开 safari,我能够登录,然后它将我发送回该应用,但当它将我转回该应用时,handleOpenURL 未在 AppDelegate 中触发。这是我的代码示例:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

....
    func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {
        InstagramAPIManager.sharedInstance.processOAuthStep1Response(url)
        print(url)
        print("handleOpenURL")
        return true
    }

}

这是触发 safari 打开的我的 API Manager 代码:

import Foundation
import Alamofire

class InstagramAPIManager {
    static let sharedInstance = InstagramAPIManager()

    func startOAuth2Login() {
        let clientID: String = "abcdefg"
        let authPath:String = "https://api.instagram.com/oauth/authorize/?client_id=\(clientID)&redirect_uri=grokInstagram://?aParam=paramVal&response_type=code"
        if let authURL:NSURL = NSURL(string: authPath)
        {
            UIApplication.sharedApplication().openURL(authURL)
        }
    }
....

}

任何帮助将不胜感激!

【问题讨论】:

  • 您知道handleOpenURL: 已被弃用,对吧?在 iOS 9 中,您应该使用 func application(_ app: UIApplication, openURL url: NSURL, options options: [String : AnyObject]) -> Bool
  • 我不知道,谢谢!
  • 效果很好,非常感谢!
  • 那么我会给出答案。很高兴它成功了!
  • 知道如果安装在设备上,我将如何从 Instagram 应用获取 OAuth2 令牌?哦,还有,你写了两次“选项”,_抛出了一个错误,但 Xcode 修复了它。

标签: ios swift2 instagram-api


【解决方案1】:

在 iOS 9 中,application:handleOpenURL: 已弃用。相反,您应该使用:

func application(app: UIApplication, 
    openURL url: NSURL, options: [String : AnyObject]) -> Bool {

【讨论】:

    【解决方案2】:

    正确的语法似乎是

    func application(_ app: UIApplication,
                              open url: URL,
                              options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
        return true
    }
    

    根据https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623112-application

    【讨论】:

      猜你喜欢
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 2018-05-28
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      相关资源
      最近更新 更多