这个问题一开始困扰了我一段时间
原因是Swift继承@protocol的时候颜色会和Swift里NSObjectProtocol的颜色不一样
我以为不能继承了
首先在Bridge-Header.h里(没有的话,创建一个Objective-C的类,会提示创建,再删除OC的类就可以了)
1 |
#import "WeiboSDK.h" |
然后在Swift里就能正确出现代码提示了
继承protocol这么写
1 |
class AppDelegate: UIResponder, UIApplicationDelegate,WeiboSDKDelegate |
注意一点,要把实现也写上,否则Swift编译是不通过的
根据OC版本的SDK,翻译成Swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool { // Override point for customization after application launch. WeiboSDK.enableDebugMode(true); WeiboSDK.registerApp(APPID); return true } func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool { return WeiboSDK.handleOpenURL(url, delegate: self) } func didReceiveWeiboRequest(request: WBBaseRequest!) { if (request.isKindOfClass(WBProvideMessageForWeiboRequest)) { //TODO: sth } } func didReceiveWeiboResponse(response: WBBaseResponse!) { if (response.isKindOfClass(WBSendMessageToWeiboResponse)) { var message = "响应状态:(response.statusCode.toRaw())n响应UserInfo数据:(response.userInfo)n原请求UserInfo数据:(response.requestUserInfo)" var alert = UIAlertView(title: "发送结果", message: message, delegate: nil, cancelButtonTitle: "确定") alert.show() } else if (response.isKindOfClass(WBAuthorizeResponse)) { var message = "响应状态: (response.statusCode.toRaw())nresponse.userId: ((response as WBAuthorizeResponse).userID)nresponse.accessToken: ((response as WBAuthorizeResponse).accessToken)n响应UserInfo数据: (response.userInfo)n原请求UserInfo数据: (response.requestUserInfo)" var alert = UIAlertView(title: "认证结果", message: message, delegate: nil, cancelButtonTitle: "确定") alert.show() } } |
最后在合适的地方调用
1 2 3 4 5 6 7 8 |
// MARK: - 微博登录按钮 @IBAction func onWeiboLoginClick(sender: AnyObject) { var request: WBAuthorizeRequest! = WBAuthorizeRequest.request() as WBAuthorizeRequest request.redirectURI = "https://api.weibo.com/oauth2/default.html" request.scope = "all" WeiboSDK.sendRequest(request) } |
微博SDK集成登录功能就做好了