swift 中的 Instagram 重定向 url 问题
Instagram redirect url issue in swift
我正在对我的 iOS 应用实施 Instagram 登录。我已成功登录,但后来无法访问我的应用程序,谁能告诉我哪个是 instagram 的有效重定向 url?这是我的代码:
代码
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
do {
if let url = request.url {
print(url)
if String(describing: url).range(of: "#access_token") != nil {
try InstagramEngine.shared().receivedValidAccessToken(from: url)
if let accessToken = InstagramEngine.shared().accessToken {
print("accessToken: \(accessToken)")
//start
let URl = "https://api.instagram.com/v1/users/self/?access_token=\(accessToken)"
let parameter = [ "access_token" : accessToken ]
// let URl = "https://api.instagram.com/v1/users/self/"
// appInstance.showLoader()
Alamofire.request(URl, method: .get, parameters: parameter, encoding: JSONEncoding.default, headers: nil)
.responseJSON { response in
//appInstance.hideLoader()
print(response.result.value!)
if response.result.isSuccess {
let dict = response.result.value!
do {
var result : [String : AnyObject] = [String : AnyObject]()
let data = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions.prettyPrinted)
if let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue){
print(json)
result["result"] = json
if hhSocialDelegate != nil{
hhSocialDelegate.getInstagramLoginResponse(userData : result)
_ = self.navigationController?.popViewController(animated: true)
}
}
}catch let err as NSError{
print(err.debugDescription)
_ = self.navigationController?.popViewController(animated: true)
}
}
}
}
}
}
} catch let err as NSError {
print(err.debugDescription)
_ = self.navigationController?.popViewController(animated: true)
}
return true
}
这是我调用 instagram 的代码 api 请帮助我解决登录后如何返回我的应用程序的问题。
您需要在您的应用 Info.plist 中提及要使用的 URI 方案。完成后,您可以使用像 com.myappid.name:/myCallback
这样的 URI 方案
Info.plist-> Setting URI Scheme
试试这个。例如,您想打开一个 Instagram 应用程序:
或者也阅读这篇 https://github.com/shyambhat/InstagramKit/issues/237
var instagramHooks = "instagram://user?username=johndoe"
var instagramUrl = NSURL(string: instagramHooks)
if UIApplication.sharedApplication().canOpenURL(instagramUrl!)
{
UIApplication.sharedApplication().openURL(instagramUrl!)
} else {
//redirect to safari because the user doesn't have Instagram
UIApplication.sharedApplication().openURL(NSURL(string: "http://instagram.com/")!)
}
我正在对我的 iOS 应用实施 Instagram 登录。我已成功登录,但后来无法访问我的应用程序,谁能告诉我哪个是 instagram 的有效重定向 url?这是我的代码:
代码
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
do {
if let url = request.url {
print(url)
if String(describing: url).range(of: "#access_token") != nil {
try InstagramEngine.shared().receivedValidAccessToken(from: url)
if let accessToken = InstagramEngine.shared().accessToken {
print("accessToken: \(accessToken)")
//start
let URl = "https://api.instagram.com/v1/users/self/?access_token=\(accessToken)"
let parameter = [ "access_token" : accessToken ]
// let URl = "https://api.instagram.com/v1/users/self/"
// appInstance.showLoader()
Alamofire.request(URl, method: .get, parameters: parameter, encoding: JSONEncoding.default, headers: nil)
.responseJSON { response in
//appInstance.hideLoader()
print(response.result.value!)
if response.result.isSuccess {
let dict = response.result.value!
do {
var result : [String : AnyObject] = [String : AnyObject]()
let data = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions.prettyPrinted)
if let json = NSString(data: data, encoding: String.Encoding.utf8.rawValue){
print(json)
result["result"] = json
if hhSocialDelegate != nil{
hhSocialDelegate.getInstagramLoginResponse(userData : result)
_ = self.navigationController?.popViewController(animated: true)
}
}
}catch let err as NSError{
print(err.debugDescription)
_ = self.navigationController?.popViewController(animated: true)
}
}
}
}
}
}
} catch let err as NSError {
print(err.debugDescription)
_ = self.navigationController?.popViewController(animated: true)
}
return true
}
这是我调用 instagram 的代码 api 请帮助我解决登录后如何返回我的应用程序的问题。
您需要在您的应用 Info.plist 中提及要使用的 URI 方案。完成后,您可以使用像 com.myappid.name:/myCallback
这样的 URI 方案Info.plist-> Setting URI Scheme
试试这个。例如,您想打开一个 Instagram 应用程序:
var instagramHooks = "instagram://user?username=johndoe"
var instagramUrl = NSURL(string: instagramHooks)
if UIApplication.sharedApplication().canOpenURL(instagramUrl!)
{
UIApplication.sharedApplication().openURL(instagramUrl!)
} else {
//redirect to safari because the user doesn't have Instagram
UIApplication.sharedApplication().openURL(NSURL(string: "http://instagram.com/")!)
}