? ?小的第一帖,具體需求是應用首次訪問系統(tǒng)相冊,會彈出授權界面,點擊確認后,馬上刷新數(shù)據(jù)源顯示圖片出來。
貼一下代碼:
```objc
if(IS_GREATER_THAN_IOS8) //ios8以上
{
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusDenied)
{
NSLog(@"用戶拒絕當前應用訪問相冊,我們需要提醒用戶打開訪問開關");
}else if (status == PHAuthorizationStatusRestricted)
{
NSLog(@"家長控制,不允許訪問");
}else if (status == PHAuthorizationStatusNotDetermined)
{
//第一次訪問相冊,彈出授權界面。點擊后定時器。這里加個定時器,判斷有沒有權限。思路是點擊后,會執(zhí)行定時器里的方法,如果點了確認,可以加載數(shù)據(jù)。點不允許,數(shù)據(jù)就加載不出來。
_time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(TODO(寫你自己的代碼)) userInfo:nil repeats:YES];
}else if (status == PHAuthorizationStatusAuthorized){
// 獲取所有資源的集合,并按資源的創(chuàng)建時間排序
TODO...(寫你自己的代碼)
}
}
```
ToDo
在掃描添加結果的時候停掉定時器。
if(_time)
{
[_time invalidate];
}
測試方法:
修改bundleId就可以生成新的應用,實現(xiàn)測試目的。
不懂可以私聊。
感覺之前的方法還是有問題,現(xiàn)在完善下。
-(void)GetALLphotosUsingPohotKit
{
if ([PHPhotoLibrary respondsToSelector:@selector(authorizationStatus)])
{
if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized)
{
[self readSystemPhoto];
}
else
{
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized)
{
[self readSystemPhoto];
}
}];
}
}
else
{
[self readSystemPhoto];
}
}
這樣的話不管有沒權限都可以訪問,有權限直接走else訪問,沒有權限彈出授權窗口,點擊同意后會回調,然后也可以馬上訪問,不需要定時器啥的。