我使用Facebook SDK和Twitter SDK进行登录和注册。
但他们都没有从一种常用方法打开网址。就像我为Facebook编写的代码一样,
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
{
if(url.scheme == "fb1651015905222312")
{
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
return true
}
这很好用,对于twitter,我必须对上面的方法进行评论,并且必须将其写成,
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
if Twitter.sharedInstance().application(app, openURL:url, options: options) {
return true
}
return true
}
这适用于Twitter。
问题是我需要编写一个常用方法来在appDelegate中打开他们的URL。那我怎么克服它?
提前致谢。
注意:我们无法在app delegate中编写这两种方法。
答案 0 :(得分:26)
最后我找到了这个问题的解决方案。
Swift 3
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if Twitter.sharedInstance().application(app, open:url, options: options) {
return true
}
let appId = SDKSettings.appId
if url.scheme != nil && url.scheme!.hasPrefix("fb\(appId)") && url.host == "authorize" { // facebook
return SDKApplicationDelegate.shared.application(app, open: url, options: options)
}
return false
}
对于Swift< 3 强>
这是允许我为Facebook和Twitter编写网址的方法。
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
if Twitter.sharedInstance().application(app, openURL:url, options: options) {
return true
}
let sourceApplication: String? = options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String
return FBSDKApplicationDelegate.sharedInstance().application(app, openURL: url, sourceApplication: sourceApplication, annotation: nil)
}
感谢所有试图回答我问题的人。
感谢。
答案 1 :(得分:12)
这适用于 Swift 3 和 Swift 4 。只需复制并粘贴:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if Twitter.sharedInstance().application(app, open: url, options: options) {
return true
}
let sourceApplication: String? = options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String
return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: sourceApplication, annotation: nil)
}
答案 2 :(得分:2)
UPDATED SWIFT 3
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let sourceApplication: String? = options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String
return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: sourceApplication, annotation: nil)
}
答案 3 :(得分:1)
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
if ([Twitter.sharedInstance application:app openURL:url options:options])
{
return YES;
}
return [self application:app openURL:url sourceApplication:[options objectForKey:UIApplicationOpenURLOptionsSourceApplicationKey] annotation:[options objectForKey:UIApplicationOpenURLOptionsAnnotationKey]];
}
答案 4 :(得分:0)
这是我尝试使用Objective
进行Twitter和Facebook的openURL- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
if ([[url scheme] isEqualToString:FACEBOOK_SCHEME])
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:nil];
if ([[url scheme] isEqualToString:TWITTER_SCHEME])
return [[Twitter sharedInstance] application:application openURL:url options:options];
return NO;
}
当然还有这些:
#define TWITTER_SCHEME @"Twitter-key-from-plistfile"
#define FACEBOOK_SCHEME @"fb-key-from-plistfile"
答案 5 :(得分:0)
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool
{
// Check for twitter
if TWTRTwitter.sharedInstance().application(app, open: url, options: options)
{
return true
}
// If twitter failed, check for facebook
return ApplicationDelegate.shared.application(app, open: url, options: options)
}