gpt4 book ai didi

ios - NSURLConnection sendAsynchronousRequest 抛出无效的 URL

转载 作者:行者123 更新时间:2023-11-29 02:46:08 26 4
gpt4 key购买 nike

每次用户打开应用程序时,我都必须加载 API 的刷新 token 。

在我的 appDelegate.m 中,每次应用程序打开时我都会调用此代码:

if(success){ //if the url was successfully retrieved
_webview=[[UIWebView alloc]init];
NSString *url=success;
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[_webview loadRequest:nsrequest];
NSLog(@"refreshtoken: %@", success); // logs "website.com/token"
}

但是,webview 不加载请求。在我的 appDelegate.h 中,我正在创建此属性:@property (nonatomic) UIWebView *webview;

为什么这不起作用?

更新

正如答案中指出的,我现在正在使用

`[NSURLConnection sendAsynchronousRequest:nsrequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error){
NSLog(@"%@",[error localizedDescription]);

}
else{
NSLog(@"data: %@", data);
}
}];
`

而不是[_webview loadRequest:nsrequest];

但是,当记录错误时,我收到以下响应:

unsupported URL

这是我传递的 url:

unicmedia.nl/redacted/startredacted.php?userid=redacted&refreshtoken=redacted

更新#1

url 没有以 http://开头,这就是发生错误的原因。

最佳答案

这个想法是做一个http请求吗?在应用程序委托(delegate)中,最好使用 NSURLConnection 来完成此操作。在发布的代码中创建的 Web View 在超出范围时会立即被释放(在 ARC 下)。如果您需要使用 Web View ,请在应用程序委托(delegate)中为其保留一个强大的属性。

NSURLRequest *request = // form your request
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// check error, handle result
}];

关于ios - NSURLConnection sendAsynchronousRequest 抛出无效的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25106509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com