如何在HTTP请求上设置模块特定的自定义标头?(How can I set module specific custom headers on a HTTP request?)
我希望能够在我的应用程序触发的HTTP请求上设置自定义标头。 此标头必须指示已触发请求的子模块。
我已经尝试过创建一个拦截器和一个装饰器(在每个模块中),但是这两个选项都放在全局角度$ http服务上,从而覆盖了彼此。
唯一有效的选项是在调用$ http服务时指定标题,但这不是我想要的。 目标是在每个模块上编写拦截器或装饰器,捕获来自该模块的所有请求,相应地设置标头。
关于如何解决这个问题的任何想法?
I would like to be able to set a custom header on the HTTP requests my application triggers. This header must indicate from which submodule the request has been fired.
I already tried creating an interceptor and a decorator (in each module) but both options are placed on the global angular $http service and thus overriding eachother.
The only option that works is specifying the header when calling the $http service but that is not really what I want. The goal is to write a interceptor or decorator on each module that catches all requests from that module, setting a header accordingly.
Any ideas on how to solve this?
原文:https://stackoverflow.com/questions/28108269
最满意答案
以下是在应用程序级别全局共享数据的最佳方法。 单身人士课程是一把钥匙。 Singleton仅初始化一次,其余时间返回共享数据。
@interface Singleton : NSObject @property (nonatomic, retain) NSMutableArray * globalArray; +(Singleton*)singleton; @end @implementation Singleton @synthesize globalArray; +(Singleton *)singleton { static dispatch_once_t pred; static Singleton *shared = nil; dispatch_once(&pred, ^{ shared = [[Singleton alloc] init]; shared.globalArray = [[NSMutableArray alloc]init]; }); return shared; } @end
以下是访问/使用共享数据的方法。
NSMutableArray * sharedData = [Singleton singleton].globalArray;
Following is best approach to share data Globally at Application level. Singleton Class is a key. Singleton is only initialised once, rest of times shared data is returned.
@interface Singleton : NSObject @property (nonatomic, retain) NSMutableArray * globalArray; +(Singleton*)singleton; @end @implementation Singleton @synthesize globalArray; +(Singleton *)singleton { static dispatch_once_t pred; static Singleton *shared = nil; dispatch_once(&pred, ^{ shared = [[Singleton alloc] init]; shared.globalArray = [[NSMutableArray alloc]init]; }); return shared; } @end
Following is the way to access/use shared data.
NSMutableArray * sharedData = [Singleton singleton].globalArray;
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
这是可能的(而且非常受欢迎)。 寻找“容器视图控制器”。 Apple文档 It is possible (and quite popular). Look for "container view controllers". Apple documentation
-
定义全局数组(Defining global array)[2024-03-24]
因为你已经在头文件中声明了数组静态,所以每个编译单元(即预处理的.cpp文件)都获得了自己的数组副本 - 几乎肯定不是你想要的,并且肯定是你得到了“定义但是没用过“错误。 相反,您可能希望在头文件中使用它: extern MyStruct_t *MyStructArray; ...然后正好在1.cpp文件中: MyStruct_t MyStructArray[] = { ...}; Because you've declared the array static in a header file, ea ... -
如果我理解你的问题,你试图将你的对象作为引用调用传递,并希望像C ++ / C一样工作。 但是Obj-C虽然相似但却有一些不同的方式。 您必须使用**(双指针)取消引用该对象,这在NSError案例中很常见,这是非常罕见的。 第二种方法是:将方法包装为块。 然后将变量放在与块相同的词法范围内,并通过__block存储类型表示它。 第三种方法是直接访问你的对象,使其成为一个ivar / property甚至是一个单独的东西。 If I understood your problem, you are tryi ...
-
请记住,Objective-C是C的一个严格超集,因此您可以像在常规C中一样声明全局变量。首先将它们声明在某个文件之外的任何函数中,然后在其他文件中使用C extern关键字来将它们变量。 如果你想用C变量做更多的事情,而使用Objective-C对象,你可以将它们存储在应用程序的委托中。 只需像往常一样将它们设置在那里,然后每当需要访问变量时: // Assuming your app delegate is of class YourAppDelegate and // has an NSString* ...
-
使用extern而不是define。 在.h中: extern NSString* SHKFacebookAppID; .m: NSString* SHKFacebookAppID = @"1234567890"; Use extern instead of define. in .h: extern NSString* SHKFacebookAppID; in .m: NSString* SHKFacebookAppID = @"1234567890";
-
您需要在开始推送新控制器之前或在完成动画制作之后修改数组。警告报告的问题是您在动画中间更改了导航控制器状态,因此它可以'保证一切正常。 You need to modify the array either before starting the push of the new controller or after it has completed animating in. The problem reported by the warning is that you have changed the ...
-
以下是在应用程序级别全局共享数据的最佳方法。 单身人士课程是一把钥匙。 Singleton仅初始化一次,其余时间返回共享数据。 @interface Singleton : NSObject @property (nonatomic, retain) NSMutableArray * globalArray; +(Singleton*)singleton; @end @implementation Singleton @synthesize globalArray; +(Singleton *)sin ...
-
最后做到了,不必为不同的类添加任何属性。 刚刚测试过这个班。 继承人代码: -(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { if ([viewController isKindOfClass:[MainTableViewController ...