使用以下方法适用于99.5%的情况,但AppStore中的用户很少报告他们无法获取mac地址(我们使用标识符来验证每个服务器调用).
失败的案例基于iPhone 4S和IOS版本5.0.1.
大家好,
你知道哪种情况(除了低内存)它会失败吗?任何想法或讨论将不胜感激.谢谢.
/* Original source code courtesy John from iOSDeveloperTips.com */
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
- (NSString *)getMacAddress
{
int mgmtInfoBase[6];
char *msgBuffer = NULL;
NSString *errorFlag = NULL;
size_t length;
// Setup the management Information Base (mib)
mgmtInfoBase[0] = CTL_NET; // Request network subsystem
mgmtInfoBase[1] = AF_ROUTE; // Routing table info
mgmtInfoBase[2] = 0;
mgmtInfoBase[3] = AF_LINK; // Request link layer information
mgmtInfoBase[4] = NET_RT_IFLIST; // Request all …Run Code Online (Sandbox Code Playgroud) 需要为下面的代码编写单元测试,我想做类方法canMakePayments的mock,返回yes或no,到目前为止没有好的方法找到会员,canMakePayments是一个类方法(+),似乎所有的OCMock方法都用于实例方法( - ).
你们有任何建议或讨论将不胜感激.谢谢.
// SKPaymentQueue.h
// StoreKit
if ([SKPaymentQueue canMakePayments]){
....
}
else{
...
}
Run Code Online (Sandbox Code Playgroud) 我想为TnSettings做模拟,是的,如果代码通过以下方法工作,问题是我们需要为每个案例编写模拟代码,如果我们只模拟一次然后执行多个case,那么第二个将报告例外.我使用最新的OCMock V2.01.
我的问题是为什么OCMock有这样的限制?或者我不能正确使用它?
任何想法或讨论将不胜感激,提前感谢.
- (void) testFormattedDistanceValueWithMeters {
mockSettings = [OCMockObject mockForClass:[TnSettings class]];
mockClientModel = [TnClientModel createMockClientModel];
[[[mockClientModel expect] andReturn:mockSettings] settings];
[[[mockSettings expect] andReturn:[NSNumber numberWithInt:0]] preferencesGeneralUnits];
NSNumber *meters = [NSNumber numberWithDouble:0.9];
distance = [NSString formattedDistanceValueWithMeters:meters];
STAssertEqualObjects(distance, @"0.9", @"testformattedEndTimeForTimeInSeconds failed");
//------------- Another case -----------------
mockSettings = [OCMockObject mockForClass:[TnSettings class]];
mockClientModel = [TnClientModel createMockClientModel];
[[[mockClientModel expect] andReturn:mockSettings] settings];
[[[mockSettings expect] andReturn:[NSNumber numberWithInt:0]] preferencesGeneralUnits];
meters = [NSNumber numberWithDouble:100.9];
distance = [NSString formattedDistanceValueWithMeters:meters];
STAssertEqualObjects(distance, @"101", @"testformattedEndTimeForTimeInSeconds failed");
}
Run Code Online (Sandbox Code Playgroud) 我的iOS UIWebView页面基于Cordova开源框架,我想在其webview URL请求中添加一些自定义http标头,我的解决方案是将它们添加到以下UIWebView委托方法中.
调试显示标题已成功添加,但实际上请求不会将它们删除.使用Wireshark捕获网络数据包,发现只有标准标头可用,没有我的自定义标头.
我的测试基于模拟器(iOS 7.1),任何有此主题经验的人都请一起分享和讨论,提前感谢.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// Add customize http headers in UIWebView request
if([request isKindOfClass:[NSMutableURLRequest class]]) {
NSMutableURLRequest * mRequest = (NSMutableURLRequest *)request;
[mRequest setValue:@"1.1" forHTTPHeaderField:@"appVersion"];
[mRequest setValue:@"iPhone 4S" forHTTPHeaderField:@"deviceModel"];
}
return [super webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
}
Run Code Online (Sandbox Code Playgroud) 这是一些代码,我们在其中设置了一个NSHTTPURLResponse对象:
NSString * data = @"response successful";
NSUInteger length = [data length];
NSDictionary * headersDict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:length], @"Content-Length", nil];
NSHTTPURLResponse * response = [[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:@"1.1" headerFields:headersDict];
Run Code Online (Sandbox Code Playgroud)
headersDict如果我们将其作为参数,以下代码将在最后一行崩溃。
错误:testReportAppOpenToAdMobWithAppstoreId (AdTrackerTests) 失败:-[__NSCFNumber length]:无法识别的选择器发送到实例 0xf653f40
我不知道为什么会崩溃。调试显示headersDict没问题,我怀疑是苹果的bug。
有任何想法吗?
由于某些原因,我限制我的应用程序只能在横向模式下工作,但奇怪的是,当我从我的应用程序打开 iPod 音乐库时,它总是会以纵向模式跳出。
看到下面的代码,我很困惑是否是系统默认行为?我怎样才能让它以横向模式出现(与其他 UI 保持一致)?谢谢。
- (IBAction)getMusic:(id)sender {
MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
picker.delegate = self;
picker.allowsPickingMultipleItems = YES;
picker.prompt = NSLocalizedString (@"Add songs to play",nil);
[self presentModalViewController: picker animated: NO];
[picker release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);;
} else {
return YES;
}
}
Run Code Online (Sandbox Code Playgroud) 请参阅以下代码示例(ARC模式),SKProductsRequest如何保留自己以等待响应?我的意思是在ARC模式下你不能写[self retain],SKProductsRequest如何在start方法中保留self,然后在响应后释放self?如你所知,代表总是很弱.
SKProductsRequest就是这里的一个例子,现在我需要这样一个服务类,并且不知道如何在请求发出时保留自己然后在响应回来时释放自己,任何有想法的人请一起分享和讨论,在此先感谢.
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
productRequest = productsRequest;
productsRequest.delegate = self;
[productsRequest start];
Run Code Online (Sandbox Code Playgroud)
PS:关于objc_setAssociatedObject,需要保持保留关系的外部长实时对象.
objc_setAssociatedObject(externalLiveObj, &kRetainSelfKey, self, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
Run Code Online (Sandbox Code Playgroud) ios ×5
objective-c ×3
ocmock ×2
asynchronous ×1
cordova ×1
crash ×1
header ×1
http-headers ×1
iphone ×1
mac-address ×1
mocking ×1
retain ×1
uiwebview ×1
unit-testing ×1
xcode ×1