【问题标题】:How to handle internal redirects in iOS5?如何在 iOS5 中处理内部重定向?
【发布时间】:2012-06-22 07:23:01
【问题描述】:

我正在使用 Titanium Mobile 开发 iPhone 应用程序。现在我想通过附属链接从我的应用程序链接到 App Store。当然,这会先打开 Safari,然后是烦人的 App Store。我从 Apple 那里找到了这篇如何在内部处理重定向的指南 (https://developer.apple.com/library/ios/qa/qa2008/qa1629.html)。由于我使用的是 Titanium,我想我将只实现一个本机模块,然后从 Titanium 中使用它。

到目前为止一切顺利,但我无法让它工作。到目前为止,这是我所拥有的(我不了解 Objective-C,所以其中一些可能很愚蠢):

DeCompendiiReferralDelegate.h

#import <UIKit/UIKit.h>

@interface DeCompendiiReferralDelegate : NSObject <NSURLConnectionDataDelegate>

@property (retain) NSURL* iTunesURL;

-(void)openReferralUrl:(NSURL *)referralURL;

@end

DeCompendiiReferralDelegate.m

#import "DeCompendiiReferralDelegate.h"

@implementation DeCompendiiReferralDelegate

@synthesize iTunesURL;

- (void)openReferralUrl:(NSURL *)referralURL {
    NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:referralURL] delegate:self startImmediately:YES];
    [con release];
}

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response {
    self.iTunesURL = [response URL];
    if( [self.iTunesURL.host hasSuffix:@"itunes.apple.com"])
    {
        [connection cancel];
        [self connectionDidFinishLoading:connection];
        return nil;
    }
    else
    {
        return request;
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {    
    [[UIApplication sharedApplication] openURL:self.iTunesURL];
}

@end

我从 DeCompendiiReferralModule.m 调用此代码。调试显示调用了 openReferralUrl,但从未打开 URL。任何想法我做错了什么?

编辑

只是为了确保我在设置它时没有做一些愚蠢的事情,我是这样称呼这段代码的:

DeCompendiiReferralDelegate *referralDelegate = [[DeCompendiiReferralDelegate alloc] init];
[referralDelegate openReferralUrl:[NSURL URLWithString:urlString]];

【问题讨论】:

    标签: objective-c ios delegates titanium nsurlconnection


    【解决方案1】:

    release你的连接马上。您应该仅在连接完成后执行此操作。

    【讨论】:

    • 感谢您的提示。不过,我只是从 Apple 指南中复制了代码,他们立即释放了连接。我删除了线路并再次尝试,但没有任何变化......
    • 所以登录connectionDidFinishLoading 看看你是否能到达那里。另外,你不应该自己打电话给connectionDidFinishLoading...
    • 我只是省略了代码示例的日志记录语句。我到处都有日志记录。 connection:willSendRequest:redirectResponse 和 connectionDidFinishLoading 永远不会被调用。
    • @Mundi 一旦启动请求,NSURLConnection 就会返回自身,而他的 NSURLConnection 会立即启动,因此如果您只想听委托方法,则可以安全释放。
    • 那么您可能没有在@interface 声明中正确地将您的委托协议设置为&lt; NSURLConnectionDelegate&gt;...
    猜你喜欢
    • 2021-01-28
    • 2021-09-13
    • 2020-03-16
    • 2014-09-03
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    相关资源
    最近更新 更多