我正在尝试使用 Shyambhat 的 InstagramKit(来自 DEV 分支),但我无法使 receivedValidAccessTokenFromURL
方法正常工作。
目前我正在使用 swift 3.0,代码如下(用于测试目的)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
engine = InstagramEngine.shared();
let iScope: InstagramKitLoginScope = [.comments, .followerList, .likes, .relationships];
let iAuthURL = engine.authorizationURL(for: iScope);
loginWebView.loadRequest(URLRequest(url: iAuthURL));
loginWebView.delegate = self;
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let url = request.url;
try! engine.receivedValidAccessToken(from: url!)
return true;
}
我在 info.plist 中设置了以下内容
InstagramAppClientId: 'myid'
InstagramAppRedirectURL: 'http://example.com'
出于某种原因,Instagram 不再允许重定向 URL 为 app://。 当我对 request.url 进行调试时。这向我展示了以下内容
https://api.instagram.com/oauth/authorize/?client_id=myid&redirect_uri=http%3A//example.com&response_type=token&scope=comments%20relationships%20likes%20follower_list
我希望这里有人有这个库的经验并且可以帮助我。
最佳答案
我已经弄清楚了。我所做的如下。
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
do {
if let url = request.url {
if (String(describing: url).range(of: "access") != nil){
try engine.receivedValidAccessToken(from: url)
if let accessToken = engine.accessToken {
NSLog("accessToken: \(accessToken)")
}
}
}
} catch let err as NSError {
print(err.debugDescription)
}
return true
}
我检查 url 是否包含访问 token ,如果包含,那么我会让引擎实际获取访问 token 。发生错误的原因是它试图获取访问 token ,而您第一次打开应用程序时该 token 不可用。
关于ios - InstagramKit 收到的ValidAccessTokenFromURL 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39735288/