Soundcloud 的 JS SDK, SC.connect();身份验证后重定向到回调窗口。 一切都在桌面和移动设备上运行良好,除非您安装了 soundcloud APP(在 iOS 上);
回调文件:
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Connect with SoundCloud</title>
</head>
<body onload="window.opener.setTimeout(window.opener.SC.connectCallback, 1)">
<b style="text-align: center;">This popup should automatically close in a few seconds</b>
</body>
</html>
如果您安装了该应用程序,SC.connect() 会打开该应用程序以检查您是否已登录,然后再进行身份验证并将您重定向到回调。 然后 callback.html 的 window.opener 不再是你的窗口(因为应用程序打开了它)并且窗口不能调用方法和关闭。
控制台显示:
TypeError: null is not an object (evaluating 'window.opener.SC')
TypeError: null is not an object (evaluating 'window.opener.setTimeout')
有没有办法在不修改 soundcloud SDK 的情况下到达我的原始窗口?
最佳答案
我的解决方案是通过在模式中使用 iframe 来绕过应用程序的打开:
this.connect= function(){
var url = "https://soundcloud.com/connect?client_id="+conf.soundcloud.client_id+"&redirect_uri="+conf.url.base+"/soundcloud-callback.html"+"&response_type=token&scope=non-expiring";
$('#sc-connect-modal').modal('show');
$( "#sc-connect-modal .modal-body" ).html('<iframe height="500px" width="100%" frameBorder="0" src="'+url+'"></iframe>');
};
我的 soundcloud-callback.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Connect with SoundCloud</title>
</head>
<body onload="window.setTimeout(window.parent.scCallback(window.location.hash), 1);">
<b style="text-align: center;">This popup should automatically close in a few seconds</b>
</body>
</html>
回调函数:
$window.scCallback = function(response) {
console.log(response);
$('#sc-connect-modal').modal('hide');
response=response.replace('#','');
var token = getUrlParameter(response,'access_token');
$rootScope.user.sc_token=token;
soundcloud.generateAuthString();
SC.get("/me"+soundcloud.authString).then(function(response){
// ...
});
}
注意 getUrlParameter()
只是一个获取参数的自定义函数
关于javascript - Soundcloud JS SDK - 如果应用程序安装在 iOS 上,则 SC.Connect() 回调窗口无法关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34931556/