这是我在stackoverflow上的第一篇文章.我是iOS开发人员新手,我不是母语为英语的人,所以我会尽力解释我的问题.
问题:
我已经在我的AppDelegate窗口中添加了两个视图,我想使用以下方法从一个窗口切换到另一个窗口:
UIView transitionFromView:toView:
Run Code Online (Sandbox Code Playgroud)
第一个视图(MainScreenView)有自己的ViewController.在MainScreenView .xib文件中,我有一个按钮,其动作调用我的AppDelegate中实现的方法"goShow".在那个方法中,我UIView transitionFromView:toView:用来转换到第二个视图.到目前为止一切正常.我的第二个视图(一个scrollview)在我的AppDelegate中以编程方式声明,并且里面有一堆图片(picturesViewController),并且在这些视图之上有一个UIPinchGestureRecognizer.
我正在使用手势识别器来翻回我的MainScreenView.这就是问题所在.当我在滚动视图上执行捏合手势时,会MainScreenView.view立即显示在动画之前,因此翻转动画看起来不对.
我正在使用的代码是:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
mainScreen = [[MainScreenViewController alloc] initWithNibName:@"MainScreenViewController" bundle: [NSBundle mainBundle]];
CGRect frame = self.window.bounds;
int pageCount = 10;
scrollView = [[UIScrollView alloc] initWithFrame:frame];
scrollView.contentSize = CGSizeMake(320*pageCount, 480);
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = FALSE;
scrollView.showsVerticalScrollIndicator = FALSE;
scrollView.delegate = self;
[...] 'While' adding pictures to de scrollView
UIPinchGestureRecognizer *twoFingerPinch = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(goBackToMain)] autorelease];
[scrollView addGestureRecognizer:twoFingerPinch];
[self.window addSubview: scrollView]; …Run Code Online (Sandbox Code Playgroud) cocoa-touch uiviewcontroller uiviewanimationtransition ios appdelegate
在我的app委托中,我创建了一个数据模型并将其注入我从故事板获取的根视图控制器,同时从一开始就需要用户的凭据.稍后,在访问某些数据模型方法时,我需要验证用户的密码并重试触发密码重新验证的请求.
最明显的是将此功能构建到可能需要请求此信息的每个视图控制器中,但我想尽可能地避免这种情况,因为它使控制器不那么通用,也使测试更难.在我看来,控制器一定不知道他们给出的模型的内部工作原理.
将此功能添加到模型中对我来说也不合适:管理用户交互完全超出了MVC模型的责任.
谁应该负责显示与相应视图控制器的模态对话框,让用户输入他的凭据?
我正在使用NSCache,我NSCache用来存储图像。我在 上显示图像UITableView。每当我首先添加图像时,它们都会调整大小,然后添加到表格中,然后添加到NSCache. 一切正常。
但是每当我关闭应用程序并再次打开时应用程序进入后台时,我的缓存将是空的,我的应用程序再次调整图像大小然后显示它,因此一开始我会看到一个空表。
我不明白为什么会这样。这是预期的行为NSCache吗?.如果是,那么我们如何改善用户体验,以便使用不会看到滞后。
这是@ipmcc 向我建议的代码
这里的类别是我的实体名称(我使用的是 coreData)
// A shared (i.e. global, but scoped to this function) cache
static NSCache* imageCache = nil;
// The following initializes the cache once, and only once
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
imageCache = [[NSCache alloc] init];
});
// Generate a cache key sufficient to uniquely identify the image we're looking for
NSString* cacheKey = [NSString stringWithFormat: @"%@", category.name];
// Try fetching …Run Code Online (Sandbox Code Playgroud) 我遇到了Appdelegate方法OpenURL的问题.
我已经设置了导入的UTI和文档类型.但是当从邮件附件打开我的应用程序时,当我实施该方法时,应用程序会立即崩溃.
折旧的handleOpenURL有效,但不是OpenURL吗?
目前我在实现中没有代码,我只是返回true.
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool {
return true
}
Run Code Online (Sandbox Code Playgroud)
崩溃说 Thread 1: EXC_BAD_ACCESS (code-1, address-0x0)
我真的不想使用已弃用的方法.
我可以使用URL Schemeapp 1中的机制来获取runtime应用2中的背景吗?
我的意思是避免app 2午餐?
在应用程序1我做:
NSURL *myUrl = [NSURL URLWithString:@"myScheme://"];
if ([[UIApplication sharedApplication] canOpenURL:myUrl]) {
[[UIApplication sharedApplication] openURL:myUrl];
}
Run Code Online (Sandbox Code Playgroud)
在app 2中,这种方法:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation;
Run Code Online (Sandbox Code Playgroud)
我可以避免午餐app 2,并让这个方法(或任何其他相关方法)在后台运行?
提前致谢!
根据iOS开发者库
应用程序委托是您编写自定义应用程序级代码的地方.与所有类一样,AppDelegate类在应用程序的两个源代码文件中定义:在接口文件AppDelegate.h和实现文件AppDelegate.m中.
但是,在Xcode 6.3中,似乎只有AppDelegate.swift,而不再是.h和.m扩展名.我想了解.swift如何替换.h和.m扩展名.
我想在整个应用程序的UINavigationBar底部设置一些阴影.这是我尝试过但它不起作用:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().layer.shadowOffset = CGSizeMake(0, 3)
UINavigationBar.appearance().layer.shadowRadius = 3.0
UINavigationBar.appearance().layer.shadowColor = UIColor.yellowColor().CGColor
UINavigationBar.appearance().layer.shadowOpacity = 0.7
}
Run Code Online (Sandbox Code Playgroud)
请告诉我该怎么做?
更新: 通过UINavigationController的子类化解决
import UIKit
class ShadowUINavigationController: UINavigationController {
override func viewWillAppear(animated: Bool) {
let darkColor: CGColorRef = UIColor(hex: 0x212121).CGColor
let lightColor: CGColorRef = UIColor.clearColor().CGColor
let navigationBarBottom: CGFloat = self.navigationBar.frame.origin.y + self.navigationBar.frame.size.height + 20
println(self.navigationBar.frame.origin.y)
println(self.navigationBar.frame.size.height)
println(navigationBarBottom)
let newShadow: CAGradientLayer = CAGradientLayer()
newShadow.frame = CGRectMake(0, navigationBarBottom, self.view.frame.size.width, 1)
newShadow.colors = [darkColor, lightColor]
self.view.layer.addSublayer(newShadow)
super.viewWillAppear(animated)
} …Run Code Online (Sandbox Code Playgroud) 我的根视图是标签栏控制器,我想在收到特定通知时在特定选项卡上打开应用程序.如果我使用presentViewController,标签栏会消失.有没有具体的方法来做到这一点?
我对许多stackoverflow问题和网站进行了一些研究,试图弄清楚iOS推送通知如何影响AppDelegate生命周期方法以及何时触发哪种方法(而不是).该研究的主要焦点是"标准"iOS推送通知(带alert字段)和静默(通过content-available设置1)以及AppDelegate application:didReceiveRemoteNotification和application:didFinishLaunchingWithOptions方法.
我不想针对不同的场景问很多问题,而是试着写下我尝试过的不同测试用例的陈述,然后问你:
Is there any statement that is wrong and if yes, which one and why?
如果发送标准推送通知,则在推送通知到达时,没有任何方法被触发,应用程序在后台保持不活动状态.一旦点击推送通知并且应用程序因此而被打开,application:didReceiveRemoteNotification方法被调用并且application:didFinishLaunchingWithOptions不会被调用.我在将应用程序放到后台以及应用程序在后台运行超过一个小时之后立即测试了这种情况 - 相同的行为.我想如果由于某些原因iOS决定在后台杀死我的应用程序,这个测试用例就像场景2,从下面的声明1,对吧?
如果发送静默推送通知,则在推送通知到达的那一刻,application:didReceiveRemoteNotification方法被调用并且application:didFinishLaunchingWithOptions不被调用.
如果发送标准推送通知,则在推送通知到达时,没有任何方法被触发,应用程序仍然被杀死.一旦点击推送通知并且应用程序因此而被打开,application:didReceiveRemoteNotification方法被调用并且application:didFinishLaunchingWithOptions不会被调用.
如果发送静默推送通知,则无法触发任何方法,因为无法将静默推送通知发送到被杀死的应用程序.在发送通知后打开应用程序后,application:didFinishLaunchingWithOptions作为正常流程的一部分进行调用,并且没有任何推送通知信息.application:didReceiveRemoteNotification没有被召唤.
如果您可以想到其他一些我可能忘记提及的现实生活场景,我会非常感激地了解它们以及在这些情况下会发生什么.
干杯
感谢Sandeep Bhandari的更新和其他方案.我忘记在原来的问题中提到我正在探索应用程序到达目前not处于前台的应用程序的情况.
将Sandeep的场景添加到列表中:
如果发送标准推送通知,application:didReceiveRemoteNotification将调用方法.application:didFinishLaunchingWithOptions不会被打电话.
如果发送静默推送通知,application:didReceiveRemoteNotification则会调用方法.application:didFinishLaunchingWithOptions不会被打电话.
如果发送标准推送通知,application:didReceiveRemoteNotification将调用方法.application:didFinishLaunchingWithOptions不会被打电话.
如果发送静默推送通知,application:didReceiveRemoteNotification则会调用方法. …
我试图Silent Push Notification在我的应用程序中定期实现,即每天一次将数据上传到服务器.
但是在iOS 11.2.6上,它始终无法调用委托方法
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))
Run Code Online (Sandbox Code Playgroud)
completionHandler 无声推送通知.
在设备日志中,我可以看到收到了静默推送通知,但是应用程序启动被取消DuetActivitySchedulerDaemon(根据日志).
你能帮我解决一下这个问题吗?以下是设备的日志.
Apr 11 21:44:59 iPhone- SpringBoard(UserNotificationsServer)[2696] <Notice>: [com.***.***-myapp] Received remote notification request 91E8-DC24 [ hasAlertContent: 0, hasSound: 0 hasBadge: 0 hasContentAvailable: 1 hasMutableContent: 0 ]
Apr 11 21:44:59 iPhone- SpringBoard(UserNotificationsServer)[2696] <Notice>: [com.***.***-myapp] Deliver push notification request 91E8-DC24.
Apr 11 21:44:59 iPhone- SpringBoard(UserNotificationsServer)[2696] <Notice>: [com.***.***-myapp] Passing content-available push to Duet.
Apr 11 21:44:59 iPhone- SpringBoard(DuetActivityScheduler)[2696] <Notice>: SUBMITTING: <private>.
Apr 11 …Run Code Online (Sandbox Code Playgroud) appdelegate ×10
ios ×7
swift ×4
cocoa-touch ×1
ios7 ×1
ios8 ×1
iphone ×1
nscache ×1
objective-c ×1
shadow ×1
silentpush ×1
storyboard ×1
url-scheme ×1