Runkeeper登录

时间:2016-11-24 05:06:01

标签: ios swift

我在我的应用程序中使用runkeeper将其连接到并从中获取数据。点击一个按钮,它会将我引导到我在runkeeper的web应用程序上登录的浏览器,在获取访问令牌和user_Id后点击服务并返回到我的应用程序。现在再次,如果我点击我的应用程序中的按钮获取连接,它会直接要求我重定向回我的应用程序,但我想要的是每次我点击连接时,它应该要求登录。我知道我必须清除缓存和cookie,但不知道如何。

@IBAction func btnRunKeeperClicked(sender: AnyObject) {
   MSYRunKeeper.shareInstance.loginWithRunKeeper { (result, success) in
    print(result)
    if success{
        print(result)
        let accessToken = result["accessToken"] as? String ?? ""
        self.hitServiceToGetDataFromRunkeeper(accessToken)
    }else{
        print("error...")
    }

    }
 }

func hitServiceToGetDataFromRunkeeper(accessToken:String){

    showActivityIndicator(true, inViewConroller: self, animated: true)
    let dict = HelperClass.userDefaultForAny("User_Detail")
    var userID = ""
    var serviceKey = ""
    if(dict != nil){
        userID = (dict!["userID"] as? String)!
        serviceKey = (dict!["service_key"] as? String)!
    }

    var paramDictionary = NSMutableDictionary()
    paramDictionary = ["method":"runkeeperLogin","service_key":serviceKey,"userID":userID,"runkeeperAccessToken":accessToken,"isRunkeeperConnect":"1"]

    print_debug(paramDictionary)

    FSServicesClass.sharedInstance.postWithParamater(paramDictionary, sBlock: { (result) in
        if(NSDictionary(dictionary: result).valueForKey("success")?.integerValue == 1){
            showActivityIndicator(false, inViewConroller: self, animated: true)

            self.btnRunKeeperConnected.setTitle("Connected", forState: .Normal)
            if self.btnFitBitConnected.titleLabel?.text == "Connected"{
                self.btnFitBitConnected.setTitle("Disconnected", forState: .Normal)
            }

            print(result)

            let dict = HelperClass.userDefaultForAny("User_Detail")
            HelperClass.removeFromUserDefaultForKey("User_Detail")

            let dict2 = updateUserDetailForConnetectdAppsAndDevices(dict!, isfitbitConnected: "0", isRunkeeperConnected: "1")

            HelperClass.saveToUserDefault(dict2, key: "User_Detail")
            popAlertMessageController(self, title: "Alert", message: NSDictionary(dictionary: result).valueForKey("errstr") as? String ?? "")

        }else{
            showActivityIndicator(false, inViewConroller: self, animated: true)
            popAlertMessageController(self, title: "Alert", message: NSDictionary(dictionary: result).valueForKey("errstr") as? String ?? "")

        }
        }, fBlock: {(ErrorResult) in
            showActivityIndicator(false, inViewConroller: self, animated: true)
            popAlertMessageController(self, title: "Please try again", message: NSDictionary(dictionary: ErrorResult).valueForKey("errstr")  as? String ?? "")
            //print(ErrorResult)
    })

}

2 个答案:

答案 0 :(得分:0)

您可以通过以下方式从runkeeper的UIWebView中删除Cookie

let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
for cookie: NSHTTPCookie in storage.cookies! {
        storage.deleteCookie(cookie)
}
NSUserDefaults.standardUserDefaults().synchronize()

OR

let cookieJar : NSHTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
    for cookie in cookieJar.cookies! as [NSHTTPCookie]{
        NSLog("cookie.domain = %@", cookie.domain)

        if cookie.domain == "www.Your runkeeper url.com" ||
            cookie.domain == "api.Your runkeeper api .com"{
            cookieJar.deleteCookie(cookie)
        }
    }

还会从用户默认值中删除runkeeper令牌。

答案 1 :(得分:0)

授权用户 首先尝试连接到RunKeeper。如果用户以前授权应用程序和访问令牌仍然可用,则连接将立即发生,无需任何干预:

  [[AppData sharedAppData].runKeeper tryToConnect:self];

如果用户未授予授权或访问令牌已丢失/删除,则将调用您的委托方法needsAuthentication。在此方法中,您可以通过OAuth请求授权。

  - (void)needsAuthentication {
    [[AppData sharedAppData].runKeeper tryToAuthorize];
  }

对于目的登录,它使用OAuth,因此一旦登录成功,它将存储令牌和信用,因此您需要手动清除钥匙串优先权。如下所示:

删除帐户

从商店中删除帐户及其代币:

[[NXOAuth2AccountStore sharedStore]  removeAccount:account];

请注意,如果您使用UIWebView请求访问服务,如上所述,该令牌可能已缓存在[NSHTTPCookieStorage sharedHTTPCookieStorage]

您可以使用以下命令从Cookie缓存中删除身份验证令牌:

for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
    if([[cookie domain] isEqualToString:@"myapp.com"]) {
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
}
[[NSUserDefaults standardUserDefaults] synchronize];

myapp.com是您收到身份验证令牌的域名 - 在请求时分配给商店的authorizedURL。为方便起见,您可能希望将令牌的域存储在account.userData中,以便在需要删除cookie时随时可用。